webcam
Browse files- app.py +46 -57
- requirements.txt +3 -4
app.py
CHANGED
|
@@ -1,60 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
with gr.Tab("🤖 Генерация"):
|
| 50 |
-
prompt = gr.Textbox(label="Промпт")
|
| 51 |
-
generate_btn = gr.Button("Сгенерировать")
|
| 52 |
-
output = gr.Textbox(label="Ответ")
|
| 53 |
-
generate_btn.click(generate, prompt, output)
|
| 54 |
-
|
| 55 |
-
with gr.Tab("🩺 Диагностика"):
|
| 56 |
-
diagnose_btn = gr.Button("Запустить диагностику")
|
| 57 |
-
diagnose_output = gr.Textbox(label="Результат", lines=10)
|
| 58 |
-
diagnose_btn.click(diagnose, outputs=diagnose_output)
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
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()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
transformers
|
|
|
|
| 1 |
+
gradio==4.19.2
|
| 2 |
+
opencv-python==4.9.0.80
|
| 3 |
+
numpy==1.24.3
|
|
|