webcam
Browse files
app.py
CHANGED
|
@@ -2,48 +2,41 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
# Load face detector (OpenCV's built-in classifier)
|
| 6 |
face_cascade = cv2.CascadeClassifier(
|
| 7 |
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
| 8 |
)
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
"""
|
| 14 |
-
if frame is None:
|
| 15 |
return None
|
| 16 |
|
| 17 |
-
# Convert to
|
|
|
|
| 18 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 19 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 20 |
|
| 21 |
-
# Apply mask to each detected face
|
| 22 |
for (x, y, w, h) in faces:
|
| 23 |
-
# Zorro
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
|
| 36 |
-
|
| 37 |
-
return frame
|
| 38 |
-
|
| 39 |
-
# Create the Gradio interface
|
| 40 |
demo = gr.Interface(
|
| 41 |
-
fn=
|
| 42 |
-
inputs=gr.Image(sources=["webcam"],
|
| 43 |
-
outputs=gr.Image(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
)
|
| 48 |
|
| 49 |
demo.launch()
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
| 5 |
face_cascade = cv2.CascadeClassifier(
|
| 6 |
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
|
| 7 |
)
|
| 8 |
|
| 9 |
+
def mask_image(image):
|
| 10 |
+
"""Apply mask to a single image (not streaming)"""
|
| 11 |
+
if image is None:
|
|
|
|
|
|
|
| 12 |
return None
|
| 13 |
|
| 14 |
+
# Convert to BGR for OpenCV
|
| 15 |
+
frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 16 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 17 |
+
|
| 18 |
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 19 |
|
|
|
|
| 20 |
for (x, y, w, h) in faces:
|
| 21 |
+
# Zorro mask
|
| 22 |
+
cv2.rectangle(frame, (x, y + int(h*0.35)),
|
| 23 |
+
(x + w, y + int(h*0.65)), (0, 0, 0), -1)
|
| 24 |
+
# Eye holes
|
| 25 |
+
cv2.circle(frame, (x + int(w*0.35), y + int(h*0.5)),
|
| 26 |
+
int(w*0.1), (255, 255, 255), -1)
|
| 27 |
+
cv2.circle(frame, (x + int(w*0.65), y + int(h*0.5)),
|
| 28 |
+
int(w*0.1), (255, 255, 255), -1)
|
| 29 |
+
|
| 30 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 31 |
+
|
| 32 |
+
# Simple interface - user clicks "Capture" then "Submit"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
demo = gr.Interface(
|
| 34 |
+
fn=mask_image,
|
| 35 |
+
inputs=gr.Image(sources=["webcam"], type="numpy"),
|
| 36 |
+
outputs=gr.Image(),
|
| 37 |
+
title="Zorro Mask",
|
| 38 |
+
description="1. Allow camera access\n2. Click 'Capture'\n3. Click 'Submit'\n4. See the mask!",
|
| 39 |
+
examples=None
|
| 40 |
)
|
| 41 |
|
| 42 |
demo.launch()
|