Madras1 commited on
Commit
1910a34
·
verified ·
1 Parent(s): 14928b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+ from deepface import DeepFace
5
+
6
+ # Berta: Não precisamos mais instalar dependências manualmente aqui,
7
+ # isso vai num arquivo chamado requirements.txt no Space!
8
+
9
+ def detect_emotion(image):
10
+ """
11
+ Função que recebe uma imagem da webcam (numpy array),
12
+ detecta emoções e devolve a imagem desenhada.
13
+ """
14
+ if image is None:
15
+ return None
16
+
17
+ # O Gradio já entrega a imagem em RGB, o OpenCV usa BGR
18
+ # Mas o DeepFace aceita bem. Vamos converter para desenhar as caixas corretamente.
19
+ img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
20
+
21
+ try:
22
+ # Detecção
23
+ results = DeepFace.analyze(img_bgr,
24
+ actions=['emotion'],
25
+ detector_backend='opencv', # Ou yolov8 se instalar
26
+ enforce_detection=False,
27
+ silent=True)
28
+
29
+ # Desenhar resultados (Simplificado para o exemplo)
30
+ for face in results:
31
+ region = face['region']
32
+ x, y, w, h = region['x'], region['y'], region['w'], region['h']
33
+ emotion = face['dominant_emotion']
34
+ score = face['emotion'][emotion]
35
+
36
+ # Desenha retângulo
37
+ cv2.rectangle(img_bgr, (x, y), (x+w, y+h), (0, 255, 0), 2)
38
+ cv2.putText(img_bgr, f"{emotion} ({score:.1f}%)", (x, y-10),
39
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
40
+
41
+ except Exception as e:
42
+ print(f"Erro: {e}")
43
+ pass
44
+
45
+ # Converte de volta para RGB para exibir no Gradio
46
+ return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
47
+
48
+ # Interface Gráfica com Gradio
49
+ # Berta: Aqui está a mágica. O 'stream' ativa a webcam contínua.
50
+ iface = gr.Interface(
51
+ fn=detect_emotion,
52
+ inputs=gr.Image(sources=["webcam"], streaming=True),
53
+ outputs="image",
54
+ live=True,
55
+ title="Detector de Emoções do Gabrielzinho (Via Berta 🤖)",
56
+ description="Backend rodando DeepFace no Hugging Face Spaces."
57
+ )
58
+
59
+ iface.launch()