Skip to content

Commit

Permalink
[refactoring] Add skip mute alter reset #80
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEyoonghee committed Aug 24, 2022
1 parent 845f010 commit 55094c8
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 88 deletions.

This file was deleted.

52 changes: 44 additions & 8 deletions streamlit-fastapi-serving/fastapi/server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import io
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
from starlette.responses import Response
from pydantic import BaseModel

from inference.viodet_video import get_violencer, get_violence
from inference.blood_detection import main as blood_detection
# from models.yolov5.detect import main as blood_detection

from inference.kinetics_violence_localization import kinetics_violence_localization
from starlette.responses import Response

from utils.file_handler import make_image_from_video
from utils.pose_filtering import pose_blur
Expand All @@ -17,15 +15,17 @@
from utils.skip import skip
from utils.mute import mute
from utils.alternative import alternative
from utils.reset import reset_data


# set default threshold
threshold = 0.8
model_IFE, model_AFE = get_violencer()

app = FastAPI(
title="DeepLabV3 image segmentation",
description="""Obtain semantic segmentation maps of the image in input via DeepLabV3 implemented in PyTorch.
Visit this URL at port 8501 for the streamlit interface.""",
title="HIPIPE Video Violence Detectiopn and Filtering",
description="""Multimodal to get the video's violence score from the input.
To view the Streamlit interface, visit this URL on port 8501.""",
version="0.1.0",
)

Expand Down Expand Up @@ -67,7 +67,7 @@ async def get_violence_video():


@app.get("/violence_filtering")
async def get_filtering_video():
async def get_filtered_video():
"""Get violence video from video file"""
# images_path = 'data/images'
video_path = 'data/videos'
Expand All @@ -92,4 +92,40 @@ async def get_filtering_video():

some_file_path = f"data/output_videos/encoding_video.mp4"
file_like = open(some_file_path, mode="rb")
return StreamingResponse(file_like, media_type="video/mp4")
return StreamingResponse(file_like, media_type="video/mp4")


@app.get("/violence_skipping")
async def get_skipped_video():
"""Get violence video from video file"""
skip(threshold)

some_file_path = f"data/output_videos/not_violent_video.mp4"
file_like = open(some_file_path, mode="rb")
return StreamingResponse(file_like, media_type="video/mp4")


@app.get("/violence_muting")
async def get_muted_video():
"""Get violence video from video file"""
mute(threshold)

some_file_path = f"data/output_videos/muted_video.mp4"
file_like = open(some_file_path, mode="rb")
return StreamingResponse(file_like, media_type="video/mp4")


@app.get("/violence_altering")
async def get_altered_video():
"""Get violence video from video file"""
alternative(threshold)

some_file_path = f"data/output_videos/altered_video_final.mp4"
file_like = open(some_file_path, mode="rb")
return StreamingResponse(file_like, media_type="video/mp4")


@app.get("/reset_data")
async def get_reset_data_call():
reset_data()
return {"message": "reset data"}
12 changes: 5 additions & 7 deletions streamlit-fastapi-serving/fastapi/utils/alternative.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import streamlit as st
from collections import deque
from scipy import stats
import os
Expand Down Expand Up @@ -132,11 +131,10 @@ def alternative(threshold):

# Alternative image
gifs_path = [
"assets/alter_images/mococo_0.gif",
"assets/alter_images/mococo_1.gif",
"assets/alter_images/mococo_2.gif",
"assets/alter_images/mococo_3.gif",
"data/alter_images/mococo_0.gif",
"data/alter_images/mococo_1.gif",
"data/alter_images/mococo_2.gif",
"data/alter_images/mococo_3.gif",
]

alternative_video("data/output_videos/compatible_video.mp4", gifs_path, violent_scene)
st.video(f'data/output_videos/altered_video_final.mp4')
alternative_video("data/output_videos/compatible_video.mp4", gifs_path, violent_scene)
5 changes: 1 addition & 4 deletions streamlit-fastapi-serving/fastapi/utils/mute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import streamlit as st
from collections import deque
from scipy import stats
import os
Expand Down Expand Up @@ -64,6 +63,4 @@ def mute(threshold):

ffmpeg_cmd = ', '.join(vol_control_list)

os.system(f'ffmpeg -hide_banner -loglevel error -y -i data/output_videos/compatible_video.mp4 -af "{ffmpeg_cmd}" data/output_videos/muted_video.mp4')

st.video(f'data/output_videos/muted_video.mp4')
os.system(f'ffmpeg -hide_banner -loglevel error -y -i data/output_videos/compatible_video.mp4 -af "{ffmpeg_cmd}" data/output_videos/muted_video.mp4')
11 changes: 1 addition & 10 deletions streamlit-fastapi-serving/fastapi/utils/skip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from operator import not_
import numpy as np
import streamlit as st
from collections import deque
from scipy import stats
import os
Expand Down Expand Up @@ -61,7 +60,6 @@ def skip(threshold):
print(not_violent_scene)

if not_violent_scene:

# key_frame을 1 frame 마다
print('changing key frame interval...')
os.system(f'ffmpeg -hide_banner -loglevel error -y -i data/output_videos/compatible_video.mp4 -g 1 data/output_videos/key_frame.mp4')
Expand All @@ -77,11 +75,4 @@ def skip(threshold):
f.close()

print('concatenating unviolent videos...')
os.system('ffmpeg -hide_banner -loglevel error -y -f concat -safe 0 -i data/not_violent_videos/file_list.txt -c copy data/output_videos/not_violent_video.mp4')

st.video('data/output_videos/not_violent_video.mp4')
else:
st.write('No video. Please adjust the threshold.')



os.system('ffmpeg -hide_banner -loglevel error -y -f concat -safe 0 -i data/not_violent_videos/file_list.txt -c copy data/output_videos/not_violent_video.mp4')
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
151 changes: 95 additions & 56 deletions streamlit-fastapi-serving/streamlit/ui.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# streamlit run your_script.py --server.port 30001
from genericpath import isfile
from urllib import response
import streamlit as st
import streamlit.components.v1 as components
import hydralit as hy
import hydralit_components as hc

import json
import io, os, sys
import requests
from PIL import Image
from requests_toolbelt.multipart.encoder import MultipartEncoder

# TODO
# import asyncio
# import aiohttp
# import aiofiles


# from utils.reset import reset_data, reset_dir
# from utils.file_handler import make_image_from_video
# from utils.pose_filtering import pose_blur
Expand Down Expand Up @@ -38,15 +44,22 @@
# return r


def set_threshold_call(threshold, api):
def get_violence_video_call(api: str):
rest_api = backend + api
response = requests.get(rest_api)

return response


def post_threshold_call(threshold: float, api: str):
rest_api = backend + api
data = {'threshold': threshold}
response = requests.post(rest_api, data=json.dumps(data))
response = requests.post(rest_api, json=data)

return response


def violence_detection_call(video, api: str):
def post_violence_detection_call(video, api: str):
rest_api = backend + api
message = MultipartEncoder(fields={"video_file": (video.name, video, "video/mp4")})

Expand All @@ -56,6 +69,45 @@ def violence_detection_call(video, api: str):
return response


def get_filtered_video_call(api: str):
# "violence_filtering"
rest_api = backend + api
response = requests.get(rest_api)

return response


def get_skipped_video_call(api: str):
# "violence_skipping"
rest_api = backend + api
response = requests.get(rest_api)

return response


def get_muted_video_call(api: str):
# "violence_muting"
rest_api = backend + api
response = requests.get(rest_api)

return response


def get_alternative_video_call(api: str):
# "violence_altering"
rest_api = backend + api
response = requests.get(rest_api)

return response


def get_reset_data_call(api: str):
rest_api = backend + api
response = requests.get(rest_api)

return response


app = hy.HydraApp(
title='ZZOLFLIX',
favicon="🎬",
Expand Down Expand Up @@ -97,7 +149,6 @@ def home():

@app.addapp(title='Get Started', icon='📽')
def test():

hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
Expand Down Expand Up @@ -136,82 +187,70 @@ def test():
with col2:
# st.title("Violence Detection")
uploaded_file = st.file_uploader("Choose a Video", type=['mp4'])

# # video save path
# FILE_OUTPUT = 'data/videos'

# if uploaded_file:
# with open(os.path.join(FILE_OUTPUT, uploaded_file.name), "wb") as out_file: # open for [w]riting as [b]inary
# out_file.write(uploaded_file.read())

threshold = st.slider("Set a Threshold", min_value=0.00, max_value=1.0, step=0.05, value=0.8)

# Violence Detection
if st.button("Violence Detection"):
# with hc.HyLoader('Violence Detection... Please Wait...', hc.Loaders.standard_loaders,index=5):
# violence_detection(threshold)

# image = Image.open('data/figures/score_output_1.png')
# st.video(os.path.join(save_video_path, 'compatible_video.mp4'))
# st.image(image, caption="Score_Output")
if uploaded_file:
# set_threshold_call(threshold, "set_threshold")
response_vd = violence_detection_call(uploaded_file, "violence_detection")
# TODO async processing
# asyncio.run(asyncio.wait(post_threshold_call(threshold, "set_threshold")))

with hc.HyLoader('Violence Detection... Please Wait...', hc.Loaders.standard_loaders,index=5):
post_threshold_call(threshold, "set_threshold")
# violence detection
response_vd = post_violence_detection_call(uploaded_file, "violence_detection")
# download video
response_video = get_violence_video_call("violence_detection")

st.video(response_video.content)
# convert scored graph
vd_score_image = Image.open(io.BytesIO(response_vd.content)).convert("RGB")
st.image(vd_score_image, caption="Score_Output")
vd_score_image.save("data/figures/score_output_1.png") # TODO if not exist dir -> create
else:
# handle case with no video
st.write("Insert an video!")

# Violence Filtering
if st.button("Violence Filtering"):
# blur_target_images_path = os.path.join(images_path, os.listdir(images_path)[0])
# with hc.HyLoader('Violence Filtering... Please Wait...', hc.Loaders.standard_loaders,index=5):
# # pose_blur(blur_target_images_path)
# make_image_from_video(video_path, blurred_images_path)
# blood_detection()
# kinetics_violence_localization(threshold)

# make_figure_from_score(
# threshold,
# off_path="data/npys/off.npy",
# on_path="data/npys/on.npy",
# index_path="data/list/output_index.list",
# save_path="data/figures",
# )

# with hc.HyLoader('Save Video... Please Wait...', hc.Loaders.standard_loaders,index=5):
# encoding_video(blurred_images_path,
# audios_path,
# save_video_path)

# st.video(os.path.join(save_video_path, 'encoding_video.mp4'))
# image = Image.open('data/figures/score_output_1.png')
# st.image(image, caption="Score_Output")
pass

vd_score_image = Image.open('data/figures/score_output_1.png')
# TODO change check Violence Detection
if vd_score_image:
with hc.HyLoader('Violence Filtering... Please Wait...', hc.Loaders.standard_loaders,index=5):
post_threshold_call(threshold, "set_threshold")
# violence filtering and download video
response_video = get_filtered_video_call("violence_filtering")

st.video(response_video.content)
st.image(vd_score_image, caption="Score_Output")
else:
# handle case with no violence detection
st.write("Start by Violence Detection!")

# 영상 스킵
if st.button("Skip Violent Scene"):
with hc.HyLoader('Skip violent scenes... Please Wait...', hc.Loaders.standard_loaders,index=5):
# skip(threshold)
pass
post_threshold_call(threshold, "set_threshold")
response_video = get_skipped_video_call("violence_skipping")
st.video(response_video.content)

# 영상 스킵
# 영상 음소거
if st.button("Mute Violent Scene"):
with hc.HyLoader('Mute violent scenes... Please Wait...', hc.Loaders.standard_loaders,index=5):
# mute(threshold)
pass
post_threshold_call(threshold, "set_threshold")
response_video = get_muted_video_call("violence_muting")
st.video(response_video.content)

# 대체 이미지
if st.button("Alternative Violent Scene"):
with hc.HyLoader('Alternative violent scenes... Please Wait...', hc.Loaders.standard_loaders,index=5):
# alternative(threshold)
pass
post_threshold_call(threshold, "set_threshold")
response_video = get_alternative_video_call("violence_altering")
st.video(response_video.content)

# 데이터를 초기화 하는 버튼
# 데이터를 초기화 하는 버튼
if st.button("Reset All Data"):
# reset_data()
pass
get_reset_data_call("reset_data")

with col3:
pass
Expand Down

0 comments on commit 55094c8

Please sign in to comment.