import cv2 import numpy as np import gradio as gr # Load the Haar Cascade model face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') def detect_faces(image): # Convert image (PIL -> NumPy) img = np.array(image) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # Draw rectangles around detected faces for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3) return img, f"✅ Faces Detected: {len(faces)}" if len(faces) > 0 else "😕 No Faces Detected" # Build Gradio Interface title = "🧠 Face Detection App" description = """ Upload an image to detect faces automatically using OpenCV Haar Cascade. Works with multiple faces and outputs an annotated image! """ iface = gr.Interface( fn=detect_faces, inputs=gr.Image(type="pil", label="Upload Your Image"), outputs=[gr.Image(label="Detected Faces"), gr.Textbox(label="Result")], title=title, description=description, theme="soft", # You can try "gradio/soft", "gradio/dark", "gradio/base", etc. examples=[ ["https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cat.png"], ] ) iface.launch()