Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,33 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
return image
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
webcam_interface = gr.Interface(
|
| 9 |
-
fn=
|
| 10 |
-
inputs=gr.
|
| 11 |
outputs="image",
|
| 12 |
-
title="Live Webcam
|
| 13 |
-
description="Displays the live feed from your webcam."
|
| 14 |
)
|
| 15 |
|
| 16 |
# Launch the Gradio app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
|
| 4 |
+
# Load the pre-trained Haar Cascade classifier for face detection
|
| 5 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
|
|
| 6 |
|
| 7 |
+
def detect_faces(image):
|
| 8 |
+
# Convert RGB image to OpenCV BGR format
|
| 9 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 10 |
+
|
| 11 |
+
# Convert to grayscale for face detection
|
| 12 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 13 |
+
|
| 14 |
+
# Perform face detection
|
| 15 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 16 |
+
|
| 17 |
+
# Draw rectangles around detected faces
|
| 18 |
+
for (x, y, w, h) in faces:
|
| 19 |
+
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 20 |
+
|
| 21 |
+
# Convert back to RGB for display
|
| 22 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 23 |
+
|
| 24 |
+
# Use gr.Video for webcam feed instead of gr.Image
|
| 25 |
webcam_interface = gr.Interface(
|
| 26 |
+
fn=detect_faces,
|
| 27 |
+
inputs=gr.Video(source="webcam", streaming=True),
|
| 28 |
outputs="image",
|
| 29 |
+
title="Live Webcam Face Detection",
|
| 30 |
+
description="Displays the live feed from your webcam and detects faces in real-time."
|
| 31 |
)
|
| 32 |
|
| 33 |
# Launch the Gradio app
|