Spaces:
Sleeping
Sleeping
fxing gradio
Browse files
app.py
CHANGED
@@ -32,13 +32,17 @@
|
|
32 |
# model.to(device)
|
33 |
# return model, processor, device
|
34 |
|
35 |
-
import gradio as gr
|
36 |
-
import cv2
|
37 |
import torch
|
38 |
-
from
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
#
|
41 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
|
42 |
mtcnn = MTCNN(keep_all=True, min_face_size=20, thresholds=[0.6, 0.7, 0.7], device=device)
|
43 |
|
44 |
def align_faces(frame, mtcnn, device):
|
@@ -62,46 +66,20 @@ def draw_annotations(frame, detections, names=None):
|
|
62 |
cv2.putText(frame, names[i], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
63 |
return frame
|
64 |
|
65 |
-
def
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
while True:
|
72 |
-
ret, frame = cap.read()
|
73 |
-
|
74 |
-
if not ret:
|
75 |
-
raise RuntimeError("Error: Failed to capture image")
|
76 |
-
|
77 |
-
# Align faces using MTCNN
|
78 |
-
aligned_faces, boxes = align_faces(frame, mtcnn, device)
|
79 |
-
|
80 |
-
# Draw annotations on the frame
|
81 |
-
annotated_frame = draw_annotations(frame, boxes)
|
82 |
-
|
83 |
-
_, buffer = cv2.imencode('.jpg', annotated_frame)
|
84 |
-
frame_bytes = buffer.tobytes()
|
85 |
-
|
86 |
-
yield frame_bytes
|
87 |
-
|
88 |
-
def video_frame_generator():
|
89 |
-
for frame in capture_frames():
|
90 |
-
yield frame
|
91 |
-
|
92 |
-
def gradio_interface():
|
93 |
-
with gr.Blocks() as demo:
|
94 |
-
with gr.Row():
|
95 |
-
webcam_output = gr.Video(source=video_frame_generator, streaming=True, label="Webcam Output")
|
96 |
-
stop_button = gr.Button("Stop")
|
97 |
-
|
98 |
-
def stop_streaming():
|
99 |
-
# Placeholder for stopping streaming if necessary
|
100 |
-
return "Streaming stopped."
|
101 |
-
|
102 |
-
stop_button.click(fn=stop_streaming, inputs=None, outputs=None)
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
|
|
|
32 |
# model.to(device)
|
33 |
# return model, processor, device
|
34 |
|
|
|
|
|
35 |
import torch
|
36 |
+
from mtcnn import MTCNN
|
37 |
+
import cv2
|
38 |
+
import numpy as np
|
39 |
+
import gradio as gr
|
40 |
+
from PIL import Image
|
41 |
|
42 |
+
# Set the device
|
43 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
44 |
+
|
45 |
+
# Initialize MTCNN
|
46 |
mtcnn = MTCNN(keep_all=True, min_face_size=20, thresholds=[0.6, 0.7, 0.7], device=device)
|
47 |
|
48 |
def align_faces(frame, mtcnn, device):
|
|
|
66 |
cv2.putText(frame, names[i], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
|
67 |
return frame
|
68 |
|
69 |
+
def process_image(image):
|
70 |
+
frame = np.array(image)
|
71 |
+
aligned_faces, boxes = align_faces(frame, mtcnn, device)
|
72 |
+
annotated_image = draw_annotations(frame, boxes)
|
73 |
+
return annotated_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Create the Gradio interface
|
76 |
+
iface = gr.Interface(
|
77 |
+
fn=process_image,
|
78 |
+
inputs=gr.inputs.Image(type="pil"),
|
79 |
+
outputs="image",
|
80 |
+
title="Face Detection with MTCNN",
|
81 |
+
description="Upload an image and the model will detect and align faces in it."
|
82 |
+
)
|
83 |
|
84 |
+
# Launch the interface
|
85 |
+
iface.launch()
|