Dzsysop commited on
Commit
7bee81a
·
1 Parent(s): 50c3a05
Files changed (1) hide show
  1. app.py +24 -31
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 process_frame(frame):
11
- """
12
- Process each webcam frame: detect faces and overlay masks.
13
- """
14
- if frame is None:
15
  return None
16
 
17
- # Convert to grayscale for face detection
 
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-style mask (black band across eyes)
24
- mask_y = y + int(h * 0.35)
25
- mask_height = int(h * 0.3)
26
- cv2.rectangle(frame, (x, mask_y), (x + w, mask_y + mask_height), (0, 0, 0), -1)
27
-
28
- # Eye cutouts for the mask (so you can see!)
29
- eye_y = mask_y + int(mask_height * 0.5)
30
- cv2.circle(frame, (x + int(w * 0.3), eye_y), int(w * 0.12), (255, 255, 255), -1)
31
- cv2.circle(frame, (x + int(w * 0.7), eye_y), int(w * 0.12), (255, 255, 255), -1)
32
-
33
- # Optional: Add a "Z" mark
34
- cv2.putText(frame, "Z", (x + w - 20, y + h - 10),
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=process_frame,
42
- inputs=gr.Image(sources=["webcam"], streaming=True),
43
- outputs=gr.Image(type="numpy"),
44
- live=True,
45
- title="🎭 Zorro Mask Sandbox",
46
- description="Point your webcam at your face. The app detects faces and adds a Zorro-style mask in real-time!"
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()