Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,36 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
demo = gr.Interface(
|
7 |
-
|
8 |
-
[gr.Video(sources=["webcam"])],
|
9 |
["video"],
|
|
|
|
|
10 |
)
|
11 |
|
12 |
if __name__ == "__main__":
|
13 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Load the pre-trained Haar Cascade classifier for face detection
|
6 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
7 |
|
8 |
+
def detect_faces(image, video):
|
9 |
+
# Read the video frame-by-frame
|
10 |
+
frame = video
|
11 |
+
|
12 |
+
# Convert the frame to an OpenCV-compatible format
|
13 |
+
if isinstance(frame, np.ndarray):
|
14 |
+
# Convert to grayscale for face detection
|
15 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
16 |
+
|
17 |
+
# Perform face detection
|
18 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
19 |
+
|
20 |
+
# Draw rectangles around detected faces
|
21 |
+
for (x, y, w, h) in faces:
|
22 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
23 |
+
|
24 |
+
return [frame]
|
25 |
+
|
26 |
+
# Gradio interface setup for face detection on live video feed
|
27 |
demo = gr.Interface(
|
28 |
+
detect_faces,
|
29 |
+
[gr.Image(), gr.Video(sources=["webcam"])],
|
30 |
["video"],
|
31 |
+
title="Live Webcam Face Detection",
|
32 |
+
description="Displays the live feed from your webcam and detects faces in real-time."
|
33 |
)
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
+
demo.launch()
|