TheChesireCat
commited on
Commit
·
2d67b07
1
Parent(s):
e5fd9c4
Vids and Photos
Browse files- app.py +131 -2
- poc.ipynb +0 -0
- requirements.txt +5 -1
app.py
CHANGED
|
@@ -1,4 +1,133 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import mimetypes
|
| 4 |
+
from deface.deface import anonymize_frame
|
| 5 |
+
from deface.centerface import CenterFace
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import imageio.v2 as imageio
|
| 8 |
+
import cv2
|
| 9 |
+
import tempfile
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
+
def streamlit_video_deface_cli(uploaded_file, keep_audio):
|
| 13 |
+
temp_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 14 |
+
processed_video_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 15 |
+
|
| 16 |
+
with open(temp_video_path, 'wb') as f:
|
| 17 |
+
f.write(uploaded_file.read())
|
| 18 |
+
|
| 19 |
+
if keep_audio:
|
| 20 |
+
command = f"deface {temp_video_path} --keep-audio -o {processed_video_temp_path}"
|
| 21 |
+
else:
|
| 22 |
+
command = f"deface {temp_video_path} -o {processed_video_temp_path}"
|
| 23 |
+
|
| 24 |
+
os.system(command)
|
| 25 |
+
|
| 26 |
+
return processed_video_temp_path
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def process_video_frame(frame, centerface, threshold, replacewith, mask_scale, ellipse, draw_scores, replaceimg=None, mosaicsize=20):
|
| 30 |
+
dets, _ = centerface(frame, threshold=threshold)
|
| 31 |
+
anonymize_frame(dets, frame, mask_scale=mask_scale, replacewith=replacewith, ellipse=ellipse, draw_scores=draw_scores, replaceimg=replaceimg, mosaicsize=mosaicsize)
|
| 32 |
+
return frame
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def streamlit_video_detect(uploaded_file, centerface, threshold, replacewith, mask_scale, ellipse, draw_scores, enable_preview, keep_audio, replaceimg=None, mosaicsize=20):
|
| 36 |
+
temp_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 37 |
+
processed_video_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 38 |
+
output_with_audio_temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 39 |
+
with open(temp_video_path, 'wb') as f:
|
| 40 |
+
f.write(uploaded_file.read())
|
| 41 |
+
|
| 42 |
+
vidcap = cv2.VideoCapture(temp_video_path)
|
| 43 |
+
|
| 44 |
+
# Get video properties
|
| 45 |
+
width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 46 |
+
height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 47 |
+
fps = vidcap.get(cv2.CAP_PROP_FPS)
|
| 48 |
+
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Create a video writer object
|
| 52 |
+
out = cv2.VideoWriter(processed_video_temp_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 53 |
+
|
| 54 |
+
# Progress Bar setup
|
| 55 |
+
progress_text = "Processing video. Please wait."
|
| 56 |
+
my_bar = st.progress(0, text=progress_text)
|
| 57 |
+
frames_processed = 0
|
| 58 |
+
|
| 59 |
+
while True:
|
| 60 |
+
ret, frame = vidcap.read()
|
| 61 |
+
if not ret:
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
processed_frame = process_video_frame(frame, centerface, threshold, replacewith, mask_scale, ellipse, draw_scores, replaceimg, mosaicsize)
|
| 65 |
+
out.write(processed_frame)
|
| 66 |
+
|
| 67 |
+
# Progress Bar update
|
| 68 |
+
frames_processed += 1
|
| 69 |
+
progress_percent = int((frames_processed / total_frames) * 100)
|
| 70 |
+
my_bar.progress(progress_percent, text=progress_text)
|
| 71 |
+
|
| 72 |
+
vidcap.release()
|
| 73 |
+
out.release()
|
| 74 |
+
|
| 75 |
+
# Empty the progress bar after completion
|
| 76 |
+
my_bar.empty()
|
| 77 |
+
|
| 78 |
+
# If keep_audio is checked, use FFmpeg to overlay the original audio on the processed video
|
| 79 |
+
if keep_audio:
|
| 80 |
+
st.write("Overlaying audio. Please wait...")
|
| 81 |
+
command = f"ffmpeg -i {processed_video_temp_path} -i {temp_video_path} -c:v copy -c:a aac -strict experimental {output_with_audio_temp_path}"
|
| 82 |
+
os.system(command)
|
| 83 |
+
return output_with_audio_temp_path
|
| 84 |
+
else:
|
| 85 |
+
return processed_video_temp_path
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def streamlit_image_detect(uploaded_file, centerface, threshold, replacewith, mask_scale, ellipse, draw_scores, enable_preview, keep_metadata, replaceimg=None, mosaicsize=20):
|
| 89 |
+
# Read the uploaded image into a numpy array
|
| 90 |
+
frame = imageio.imread(uploaded_file)
|
| 91 |
+
|
| 92 |
+
# For the sake of this example, I'm skipping the metadata part
|
| 93 |
+
|
| 94 |
+
dets, _ = centerface(frame, threshold=threshold)
|
| 95 |
+
anonymize_frame(dets, frame, mask_scale=mask_scale, replacewith=replacewith, ellipse=ellipse, draw_scores=draw_scores, replaceimg=replaceimg, mosaicsize=mosaicsize)
|
| 96 |
+
|
| 97 |
+
# Convert numpy array back to a PIL.Image so that we can display/save it easily in Streamlit
|
| 98 |
+
result_img = Image.fromarray(frame)
|
| 99 |
+
return result_img
|
| 100 |
+
|
| 101 |
+
def get_file_type(uploaded_file):
|
| 102 |
+
mime = mimetypes.guess_type(uploaded_file.name)[0]
|
| 103 |
+
if mime is None:
|
| 104 |
+
return None
|
| 105 |
+
if mime.startswith('video'):
|
| 106 |
+
return 'video'
|
| 107 |
+
if mime.startswith('image'):
|
| 108 |
+
return 'image'
|
| 109 |
+
return mime
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
st.title("Media Anonymizer")
|
| 113 |
+
|
| 114 |
+
uploaded_files = st.file_uploader("Upload media files", accept_multiple_files=True)
|
| 115 |
+
keep_audio = st.checkbox("Keep Audio")
|
| 116 |
+
|
| 117 |
+
for uploaded_file in uploaded_files:
|
| 118 |
+
file_type = get_file_type(uploaded_file)
|
| 119 |
+
|
| 120 |
+
if file_type == 'image':
|
| 121 |
+
# Initialize centerface or other configurations if needed
|
| 122 |
+
centerface = CenterFace()
|
| 123 |
+
result_img = streamlit_image_detect(uploaded_file, centerface, threshold=0.2, replacewith='blur', mask_scale=1.0, ellipse=True, draw_scores=False, enable_preview=False, keep_metadata=False)
|
| 124 |
+
st.image(result_img, caption=f'Anonymized {uploaded_file.name}', use_column_width=True)
|
| 125 |
+
elif file_type == 'video':
|
| 126 |
+
centerface = CenterFace()
|
| 127 |
+
processed_video_path = streamlit_video_detect(uploaded_file, centerface, threshold=0.2, replacewith='blur', mask_scale=1.0, ellipse=True, draw_scores=False, enable_preview=False, keep_audio=keep_audio)
|
| 128 |
+
st.video(processed_video_path, format='video/mp4', start_time=0)
|
| 129 |
+
else:
|
| 130 |
+
st.write(f"Unsupported file type for {uploaded_file.name}")
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
main()
|
poc.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
deface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
deface==1.5.0
|
| 2 |
+
imageio==2.31.1
|
| 3 |
+
opencv_python==4.5.5.64
|
| 4 |
+
Pillow==10.1.0
|
| 5 |
+
streamlit==1.27.2
|