Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +91 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2 as cv
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def blur_faces_image(image):
|
| 8 |
+
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
|
| 9 |
+
H, W = image.shape[:2]
|
| 10 |
+
|
| 11 |
+
mp_face_detection = mp.solutions.face_detection
|
| 12 |
+
|
| 13 |
+
with mp_face_detection.FaceDetection(min_detection_confidence=0.5, model_selection=0) as face_detection:
|
| 14 |
+
img_rgb = cv.cvtColor(image, cv.COLOR_BGR2RGB)
|
| 15 |
+
out = face_detection.process(img_rgb)
|
| 16 |
+
|
| 17 |
+
if out.detections is not None:
|
| 18 |
+
for detection in out.detections:
|
| 19 |
+
location_data = detection.location_data
|
| 20 |
+
bbox = location_data.relative_bounding_box
|
| 21 |
+
|
| 22 |
+
x1, y1, w, h = bbox.xmin, bbox.ymin, bbox.width, bbox.height
|
| 23 |
+
|
| 24 |
+
x1 = int(x1 * W)
|
| 25 |
+
y1 = int(y1 * H)
|
| 26 |
+
w = int(w * W)
|
| 27 |
+
h = int(h * H)
|
| 28 |
+
# Apply blur to the face region
|
| 29 |
+
face_roi = image[y1:y1 + h, x1:x1 + w:]
|
| 30 |
+
blurred_face = cv.blur(face_roi, (50, 50))
|
| 31 |
+
image[y1:y1 + h, x1:x1 + w] = blurred_face
|
| 32 |
+
|
| 33 |
+
return cv.cvtColor(image, cv.COLOR_BGR2RGB) # Convert BGR back to RGB for Gradio
|
| 34 |
+
|
| 35 |
+
def blur_faces_video(video_path, output_path="blurred_output.mp4"):
|
| 36 |
+
cap = cv.VideoCapture(video_path)
|
| 37 |
+
fps = cap.get(cv.CAP_PROP_FPS)
|
| 38 |
+
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
|
| 39 |
+
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
|
| 40 |
+
|
| 41 |
+
# Define video writer
|
| 42 |
+
fourcc = cv.VideoWriter_fourcc(*'mp4v')
|
| 43 |
+
out = cv.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 44 |
+
|
| 45 |
+
while cap.isOpened():
|
| 46 |
+
ret, frame = cap.read()
|
| 47 |
+
if not ret:
|
| 48 |
+
break
|
| 49 |
+
|
| 50 |
+
frame_rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
|
| 51 |
+
blurred_frame = blur_faces_image(frame_rgb)
|
| 52 |
+
frame_bgr = cv.cvtColor(blurred_frame, cv.COLOR_RGB2BGR)
|
| 53 |
+
|
| 54 |
+
out.write(frame_bgr)
|
| 55 |
+
|
| 56 |
+
cap.release()
|
| 57 |
+
out.release()
|
| 58 |
+
|
| 59 |
+
return output_path
|
| 60 |
+
def process_video_interface(video_file):
|
| 61 |
+
video_path = video_file["name"] if isinstance(video_file, dict) else video_file
|
| 62 |
+
return blur_faces_video(video_path)
|
| 63 |
+
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 65 |
+
gr.Markdown("# 🕵️♂️ Face Blur App")
|
| 66 |
+
|
| 67 |
+
with gr.Tab("Image Face Blur"):
|
| 68 |
+
with gr.Row():
|
| 69 |
+
image_input = gr.Image(label="Upload Image")
|
| 70 |
+
image_output = gr.Image(label="Blurred Output")
|
| 71 |
+
|
| 72 |
+
image_btn = gr.Button("Blur Faces")
|
| 73 |
+
image_btn.click(fn=blur_faces_image, inputs=image_input, outputs=image_output)
|
| 74 |
+
gr.Examples(
|
| 75 |
+
examples=["examples/testImg.png"],
|
| 76 |
+
inputs=image_input,
|
| 77 |
+
label="Try Example Image"
|
| 78 |
+
)
|
| 79 |
+
with gr.Tab("Video Face Blur"):
|
| 80 |
+
with gr.Row():
|
| 81 |
+
video_input = gr.Video(label="Upload Video")
|
| 82 |
+
video_output = gr.Video(label="Blurred Video Output")
|
| 83 |
+
|
| 84 |
+
video_btn = gr.Button("Blur Faces in Video")
|
| 85 |
+
video_btn.click(fn=process_video_interface, inputs=video_input, outputs=video_output)
|
| 86 |
+
gr.Examples(
|
| 87 |
+
examples=["examples/testVideo.mp4"],
|
| 88 |
+
inputs=video_input,
|
| 89 |
+
label="Try Example Video"
|
| 90 |
+
)
|
| 91 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mediapipe
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
gradio
|