Python_FaceCensoring / Face_Censoring.py
aashwinik's picture
Update Face_Censoring.py
1e8162b verified
import cv2
import streamlit as st
import tempfile
from ffmpeg import FFmpeg
try:
face_cascade = cv2.CascadeClassifier('XML/faces.xml')
except Exception:
st.write("Error loading cascade classifiers")
def censor_face(filePath):
#output_file = 'Output/censored_' + str(filePath.name) + '.mp4'
video=cv2.VideoCapture(filePath.name)
success, frame = video.read()
height = frame.shape[0]
width = frame.shape[1]
#st.write(height, width)
#gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#st.write("Frame: " + str(frame))
tfile2 = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
#tfile2.write("Censored.mp4")
output=cv2.VideoWriter(tfile2.name, cv2.VideoWriter_fourcc(*'mp4v2'), 30, (width, height))
while success:
faces = face_cascade.detectMultiScale(frame, 1.1, 4)
for (x,y, w, h) in faces: #multiple faces in a video
frame[y:y+h, x:x+w] = cv2.blur(frame[y:y+h, x:x+w], (50, 50))
#st.write("frame")
output.write(frame)
success, frame = video.read()
#st.write(tfile2.name)
#st.write(tfile2)
ffmpeg = (
FFmpeg()
.option("y")
.input(tfile2.name)
.output(
tfile2.name,
{"codec:v": "libx264"},
vf="scale=1280:-1",
preset="veryslow",
crf=24,
)
)
ffmpeg.execute()
result_video = open(tfile2.name, "rb")
vid_bytes = result_video.read()
#with open(tfile2.name, "rb") as file:
# btn = st.download_button(
# label="Download video",
# data=file,
# file_name=tfile2.name,
# mime="video/mp4"
# )
#video_file = open(tfile2.name, 'rb')
#video_bytes = video_file.read()
st.video(vid_bytes)
output.release()
video.release()
return tfile2