File size: 1,179 Bytes
69b8881
86f1f8f
69b8881
86f1f8f
 
69b8881
86f1f8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
834b2a4
69b8881
86f1f8f
834b2a4
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 with live=True for live webcam feed
webcam_interface = gr.Interface(
    fn=detect_faces,
    inputs=gr.Video(streaming=True, live=True),  # Use `live=True` instead of `source="webcam"`
    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()