Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| # 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, video): | |
| # Read the video frame-by-frame | |
| frame = video | |
| # Convert the frame to an OpenCV-compatible format | |
| if isinstance(frame, np.ndarray): | |
| # Convert to grayscale for face detection | |
| gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) | |
| # 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(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) | |
| return [frame] | |
| # Gradio interface setup for face detection on live video feed | |
| demo = gr.Interface( | |
| detect_faces, | |
| [gr.Video(sources=["webcam"])], | |
| ["video"], | |
| title="Live Webcam Face Detection", | |
| description="Displays the live feed from your webcam and detects faces in real-time." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |