import cv2 import gradio as gr import numpy as np # Load OpenCV's built-in face detector face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + "haarcascade_frontalface_default.xml" ) def recognize_faces(image): if image is None: return None # Gradio gives us an RGB image image = np.array(image) # Convert RGB to grayscale gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Detect faces faces = face_cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50) ) # Draw a box around each detected face for (x, y, w, h) in faces: cv2.rectangle( image, (x, y), (x + w, y + h), (0, 255, 0), 3 ) cv2.putText( image, "Face Detected", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2 ) return image app = gr.Interface( fn=recognize_faces, inputs=gr.Image( sources=["upload", "webcam"], type="numpy", label="Upload or Take a Photo" ), outputs=gr.Image( type="numpy", label="Face Detection Result" ), title="😊 Face Detection App", description="Upload a photo or use your webcam to detect faces." ) if __name__ == "__main__": app.launch()