| import gradio as gr |
| import cv2 |
| import numpy as np |
|
|
| |
| |
| face_cascade = cv2.CascadeClassifier( |
| cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' |
| ) |
|
|
| def apply_zorro_mask(frame: np.ndarray) -> np.ndarray: |
| """ |
| This function is called for every frame from your webcam. |
| It detects faces and draws a Zorro mask on them. |
| """ |
| if frame is None: |
| return None |
|
|
| |
| |
| frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) |
|
|
| |
| gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY) |
|
|
| |
| |
| faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(60, 60)) |
|
|
| |
| for (x, y, w, h) in faces: |
| |
| mask_height = int(h * 0.4) |
| mask_y = y + int(h * 0.2) |
| cv2.rectangle(frame_bgr, (x, mask_y), (x + w, mask_y + mask_height), (0, 0, 0), -1) |
|
|
| |
| eye_y = mask_y + int(mask_height * 0.5) |
| eye_radius = int(w * 0.1) |
| |
| cv2.circle(frame_bgr, (x + int(w * 0.35), eye_y), eye_radius, (255, 255, 255), -1) |
| |
| cv2.circle(frame_bgr, (x + int(w * 0.65), eye_y), eye_radius, (255, 255, 255), -1) |
|
|
| |
| cv2.putText(frame_bgr, "Z", (x + w - 30, y + h - 20), |
| cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3) |
|
|
| |
| frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB) |
| return frame_rgb |
|
|
| |
| |
| with gr.Blocks(title="Real-Time Zorro Mask Sandbox") as demo: |
| gr.Markdown("# 🎭 Real-Time Zorro Mask Sandbox") |
| gr.Markdown("Allow webcam access. The mask will be applied to your face in real-time!") |
|
|
| with gr.Row(): |
| |
| input_webcam = gr.Image(sources=["webcam"], streaming=True, label="Your Webcam Feed") |
| |
| output_video = gr.Image(label="Live Zorro Mask Output") |
|
|
| |
| |
| |
| input_webcam.stream( |
| fn=apply_zorro_mask, |
| inputs=input_webcam, |
| outputs=output_video, |
| time_limit=10, |
| stream_every=0.05 |
| ) |
|
|
| |
| demo.launch() |