import cv2 import numpy as np import gradio as gr from deepface import DeepFace # Berta: Não precisamos mais instalar dependências manualmente aqui, # isso vai num arquivo chamado requirements.txt no Space! def detect_emotion(image): """ Função que recebe uma imagem da webcam (numpy array), detecta emoções e devolve a imagem desenhada. """ if image is None: return None # O Gradio já entrega a imagem em RGB, o OpenCV usa BGR # Mas o DeepFace aceita bem. Vamos converter para desenhar as caixas corretamente. img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) try: # Detecção results = DeepFace.analyze(img_bgr, actions=['emotion'], detector_backend='opencv', # Ou yolov8 se instalar enforce_detection=False, silent=True) # Desenhar resultados (Simplificado para o exemplo) for face in results: region = face['region'] x, y, w, h = region['x'], region['y'], region['w'], region['h'] emotion = face['dominant_emotion'] score = face['emotion'][emotion] # Desenha retângulo cv2.rectangle(img_bgr, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(img_bgr, f"{emotion} ({score:.1f}%)", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) except Exception as e: print(f"Erro: {e}") pass # Converte de volta para RGB para exibir no Gradio return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) # Interface Gráfica com Gradio # Berta: Aqui está a mágica. O 'stream' ativa a webcam contínua. iface = gr.Interface( fn=detect_emotion, inputs=gr.Image(sources=["webcam"], streaming=True), outputs="image", live=True, title="Detector de Emoções do Gabrielzinho (Via Berta 🤖)", description="Backend rodando DeepFace no Hugging Face Spaces." ) iface.launch()