FaceDetection / app.py
MarcosRodrigo's picture
Update app.py
86f1f8f verified
raw
history blame
1.14 kB
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()