vu0018 commited on
Commit
664cdae
·
verified ·
1 Parent(s): ea7316a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -49
app.py CHANGED
@@ -1,53 +1,36 @@
1
- """
2
- Hugging Face App: Face Detection in Video
3
- -----------------------------------------
4
- Uploads a video → detects faces → returns processed video.
5
- """
6
 
7
- import gradio as gr
8
  import cv2
9
  import torch
10
- import numpy as np
11
  import tempfile
 
12
  from transformers import AutoProcessor, AutoModelForObjectDetection
13
 
 
14
 
15
- MODEL_ID = "avaabedi/deepface-detector"
16
-
17
- # Load model + processor (only once)
18
  processor = AutoProcessor.from_pretrained(MODEL_ID)
19
  model = AutoModelForObjectDetection.from_pretrained(MODEL_ID)
20
  model.eval()
21
 
22
-
23
- def detect_faces_in_frame(frame):
24
- """Detect faces in a single frame using HF model."""
25
- inputs = processor(images=frame, return_tensors="pt")
26
  with torch.no_grad():
27
  outputs = model(**inputs)
 
 
28
 
29
- results = processor.post_process_object_detection(
30
- outputs,
31
- threshold=0.5
32
- )[0]
33
-
34
- return results["boxes"], results["scores"], results["labels"]
35
-
36
-
37
- def process_video(video_path):
38
- """Reads video, detects faces frame-by-frame, draws boxes, writes output video."""
39
  cap = cv2.VideoCapture(video_path)
40
  if not cap.isOpened():
41
- return "Error: cannot read video."
42
 
43
- fps = cap.get(cv2.CAP_PROP_FPS)
44
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
45
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
46
 
47
- # Output video file
48
  temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
49
  out_path = temp_out.name
50
-
51
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
52
  writer = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
53
 
@@ -56,37 +39,30 @@ def process_video(video_path):
56
  if not ret:
57
  break
58
 
59
- # Detect faces
60
- boxes, scores, labels = detect_faces_in_frame(frame)
 
 
61
 
62
- # Draw detections
63
  for box, score in zip(boxes, scores):
64
  x1, y1, x2, y2 = map(int, box.tolist())
65
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
66
  cv2.putText(frame, f"{score:.2f}", (x1, y1 - 5),
67
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
68
 
69
  writer.write(frame)
70
 
71
  cap.release()
72
  writer.release()
73
-
74
  return out_path
75
 
76
-
77
- # ------------------------------------------------
78
- # GRADIO UI
79
- # ------------------------------------------------
80
  with gr.Blocks() as demo:
81
- gr.Markdown("# 🎥 Face Detection in Video (Hugging Face)")
82
-
83
- video_input = gr.Video(label="Upload a video") # no type=
84
- process_btn = gr.Button("Detect Faces")
85
-
86
- video_output = gr.Video(label="Output Video")
87
 
88
- process_btn.click(fn=process_video,
89
- inputs=video_input,
90
- outputs=video_output)
91
 
92
- demo.launch(server_name="0.0.0.0", share=True)
 
 
1
+ # app.py — face detection in video using MediaPipe-Face-Detection model
 
 
 
 
2
 
 
3
  import cv2
4
  import torch
5
+ import gradio as gr
6
  import tempfile
7
+ import numpy as np
8
  from transformers import AutoProcessor, AutoModelForObjectDetection
9
 
10
+ MODEL_ID = "qualcomm/MediaPipe-Face-Detection"
11
 
 
 
 
12
  processor = AutoProcessor.from_pretrained(MODEL_ID)
13
  model = AutoModelForObjectDetection.from_pretrained(MODEL_ID)
14
  model.eval()
15
 
16
+ def detect_faces_in_frame(frame_rgb: np.ndarray, threshold: float = 0.5):
17
+ inputs = processor(images=frame_rgb, return_tensors="pt")
 
 
18
  with torch.no_grad():
19
  outputs = model(**inputs)
20
+ processed = processor.post_process_object_detection(outputs, threshold=threshold)[0]
21
+ return processed["boxes"], processed["scores"], processed["labels"]
22
 
23
+ def process_video(video_path: str, threshold: float = 0.5):
 
 
 
 
 
 
 
 
 
24
  cap = cv2.VideoCapture(video_path)
25
  if not cap.isOpened():
26
+ return None
27
 
28
+ fps = cap.get(cv2.CAP_PROP_FPS) or 24.0
29
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
30
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
31
 
 
32
  temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
33
  out_path = temp_out.name
 
34
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
35
  writer = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
36
 
 
39
  if not ret:
40
  break
41
 
42
+ # convert BGR → RGB for model
43
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
44
+
45
+ boxes, scores, labels = detect_faces_in_frame(frame_rgb, threshold)
46
 
 
47
  for box, score in zip(boxes, scores):
48
  x1, y1, x2, y2 = map(int, box.tolist())
49
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
50
  cv2.putText(frame, f"{score:.2f}", (x1, y1 - 5),
51
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
52
 
53
  writer.write(frame)
54
 
55
  cap.release()
56
  writer.release()
 
57
  return out_path
58
 
 
 
 
 
59
  with gr.Blocks() as demo:
60
+ gr.Markdown("# Video Face Detection")
61
+ video_in = gr.Video(label="Upload video")
62
+ process_button = gr.Button("Detect Faces")
63
+ video_out = gr.Video(label="Processed Video")
 
 
64
 
65
+ process_button.click(fn=process_video, inputs=video_in, outputs=video_out)
 
 
66
 
67
+ if __name__ == "__main__":
68
+ demo.launch(server_name="0.0.0.0", share=True)