Spaces:
Sleeping
Sleeping
roychao19477
commited on
Commit
·
d18269d
1
Parent(s):
6686600
First commit
Browse files
app.py
CHANGED
@@ -41,18 +41,57 @@ from models.pcs400 import cal_pcs
|
|
41 |
from ultralytics import YOLO
|
42 |
import supervision as sv
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
gr.Interface(fn=dummy_fn, inputs=gr.Video(source="webcam"), outputs="video").launch()
|
56 |
|
57 |
|
58 |
ckpt = "ckpts/SEMamba_advanced.pth"
|
|
|
41 |
from ultralytics import YOLO
|
42 |
import supervision as sv
|
43 |
|
44 |
+
import gradio as gr
|
45 |
+
import cv2
|
46 |
+
import os
|
47 |
+
import tempfile
|
48 |
+
from ultralytics import YOLO
|
49 |
+
from moviepy.editor import ImageSequenceClip
|
50 |
|
51 |
+
# Load face detector
|
52 |
+
model = YOLO("yolov8n-face.pt").cuda() # assumes CUDA available
|
53 |
|
54 |
+
@spaces.GPU
|
55 |
+
def extract_faces(video_file):
|
56 |
+
cap = cv2.VideoCapture(video_file)
|
57 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
58 |
+
frames = []
|
59 |
+
|
60 |
+
while True:
|
61 |
+
ret, frame = cap.read()
|
62 |
+
if not ret:
|
63 |
+
break
|
64 |
+
|
65 |
+
# Inference
|
66 |
+
results = model(frame, verbose=False)[0]
|
67 |
+
for box in results.boxes:
|
68 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
69 |
+
face_crop = frame[y1:y2, x1:x2]
|
70 |
+
if face_crop.size != 0:
|
71 |
+
resized = cv2.resize(face_crop, (224, 224))
|
72 |
+
frames.append(resized)
|
73 |
+
break # only one face per frame
|
74 |
+
|
75 |
+
cap.release()
|
76 |
+
|
77 |
+
# Save as video
|
78 |
+
tmpdir = tempfile.mkdtemp()
|
79 |
+
output_path = os.path.join(tmpdir, "face_only_video.mp4")
|
80 |
+
clip = ImageSequenceClip([cv2.cvtColor(f, cv2.COLOR_BGR2RGB) for f in frames], fps=fps)
|
81 |
+
clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None)
|
82 |
+
|
83 |
+
return output_path
|
84 |
+
|
85 |
+
iface = gr.Interface(
|
86 |
+
fn=extract_faces,
|
87 |
+
inputs=gr.Video(source="upload", label="Upload your video or record"),
|
88 |
+
outputs=gr.Video(label="Detected Face Only Video"),
|
89 |
+
title="Face Detector",
|
90 |
+
description="Upload or record a video. We'll crop face regions and return a face-only video."
|
91 |
+
)
|
92 |
+
|
93 |
+
iface.launch()
|
94 |
|
|
|
95 |
|
96 |
|
97 |
ckpt = "ckpts/SEMamba_advanced.pth"
|