import cv2 import gradio as gr def detect_faces(image): # Convert the image to grayscale for better detection gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Load the pre-trained face detection model from OpenCV face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") # Detect faces in the image faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5) # Draw rectangles around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) # Return the modified image and the number of faces detected return image, len(faces) iface = gr.Interface(fn=detect_faces, inputs=gr.components.Image(source="webcam", type="pil"), outputs=[gr.components.Image(type="pil"), gr.components.Label()], title="Face Detection", description="This app detects faces in real-time from your webcam.") iface.launch()