import gradio as gr import mediapipe as mp import cv2 import numpy as np # Initialize MediaPipe Face Detection mp_face_detection = mp.solutions.face_detection mp_drawing = mp.solutions.drawing_utils face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5) def detect_faces(image): # Convert the image from BGR (Gradio's default) to RGB image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) # Process the image and detect faces results = face_detection.process(image) # Draw face detections on the image if results.detections: for detection in results.detections: mp_drawing.draw_detection(image, detection) # Convert the image back to BGR image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) return image # Define the Gradio interface demo = gr.Interface( fn=detect_faces, inputs=gr.components.Image(shape=(480, 640), source="webcam"), outputs=gr.components.Image(type="numpy", label="Processed Image") ) demo.launch()