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