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()