|
import cv2
|
|
from fer import FER
|
|
import numpy as np
|
|
|
|
|
|
def capture_webcam():
|
|
"""
|
|
Captures an image from webcam and returns the detected emotion and confidence score
|
|
Returns: tuple (emotion: str, confidence_score: float)
|
|
"""
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
|
|
if not cap.isOpened():
|
|
raise IOError("Cannot open webcam")
|
|
|
|
|
|
detector = FER(mtcnn=True)
|
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
if not ret:
|
|
cap.release()
|
|
return "neutral", 0.0
|
|
|
|
|
|
result = detector.detect_emotions(frame)
|
|
|
|
|
|
cap.release()
|
|
|
|
|
|
if not result:
|
|
return "neutral", 0.0
|
|
|
|
|
|
emotions = result[0]["emotions"]
|
|
|
|
|
|
max_emotion = max(emotions.items(), key=lambda x: x[1])
|
|
emotion_label = max_emotion[0]
|
|
confidence_score = max_emotion[1]
|
|
|
|
return emotion_label, round(confidence_score, 2)
|
|
|
|
|
|
|