Dzsysop commited on
Commit
c1e5494
·
1 Parent(s): 55d1e22
Files changed (2) hide show
  1. app.py +46 -57
  2. requirements.txt +3 -4
app.py CHANGED
@@ -1,60 +1,49 @@
1
  import gradio as gr
2
- import requests
3
- import os
4
-
5
- # Функция диагностики
6
- def diagnose():
7
- token = os.environ.get("HF_TOKEN", "")
8
- lines = ["🔍 Диагностика HF API", "="*40]
9
-
10
- # Проверка токена
11
- lines.append(f"Токен: {'✅ Есть' if token else '❌ Нет'}")
12
-
13
- # Проверка моделей
14
- models = ["gpt2", "microsoft/phi-2"]
15
- for model in models:
16
- try:
17
- headers = {"Authorization": f"Bearer {token}"} if token else {}
18
- response = requests.post(
19
- f"https://api-inference.huggingface.co/models/{model}",
20
- headers=headers,
21
- json={"inputs": "test", "parameters": {"max_length": 1}},
22
- timeout=10
23
- )
24
- status = f"{model}: {'✅' if response.status_code == 200 else '❌ ' + str(response.status_code)}"
25
- except:
26
- status = f"{model}: Ошибка"
27
- lines.append(status)
28
-
29
- return "\n".join(lines)
30
-
31
- # Функция генерации
32
- def generate(prompt):
33
- try:
34
- response = requests.post(
35
- "https://api-inference.huggingface.co/models/gpt2",
36
- json={"inputs": prompt, "parameters": {"max_length": 100}},
37
- timeout=30
38
- )
39
- if response.status_code == 200:
40
- return response.json()[0]['generated_text']
41
- return f"Ошибка {response.status_code}"
42
- except:
43
- return "Ошибка соединения"
44
-
45
- # Интерфейс
46
- with gr.Blocks() as demo:
47
- gr.Markdown("# ИИ для игры")
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>=4.0
2
- requests>=2.28
3
- huggingface-hub
4
- transformers
 
1
+ gradio==4.19.2
2
+ opencv-python==4.9.0.80
3
+ numpy==1.24.3