import gradio as gr import cv2 import numpy as np # Load face detector (OpenCV's built-in classifier) face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' ) def process_frame(frame): """ Process each webcam frame: detect faces and overlay masks. """ if frame is None: return None # Convert to grayscale for face detection gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) # Apply mask to each detected face for (x, y, w, h) in faces: # Zorro-style mask (black band across eyes) mask_y = y + int(h * 0.35) mask_height = int(h * 0.3) cv2.rectangle(frame, (x, mask_y), (x + w, mask_y + mask_height), (0, 0, 0), -1) # Eye cutouts for the mask (so you can see!) eye_y = mask_y + int(mask_height * 0.5) cv2.circle(frame, (x + int(w * 0.3), eye_y), int(w * 0.12), (255, 255, 255), -1) cv2.circle(frame, (x + int(w * 0.7), eye_y), int(w * 0.12), (255, 255, 255), -1) # Optional: Add a "Z" mark cv2.putText(frame, "Z", (x + w - 20, y + h - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2) return frame # Create the Gradio interface demo = gr.Interface( fn=process_frame, inputs=gr.Image(sources=["webcam"], streaming=True), outputs=gr.Image(type="numpy"), live=True, title="🎭 Zorro Mask Sandbox", description="Point your webcam at your face. The app detects faces and adds a Zorro-style mask in real-time!" ) demo.launch()