Inspiration
What it does
import cv2 from fer import FER
Initialize the FER face emotion recognizer
emotion_detector = FER()
Open the webcam (set 0 for default camera, or use a video file path)
cap = cv2.VideoCapture(0)
while True: # Capture frame-by-frame ret, frame = cap.read()
if not ret:
break
# Detect emotions in the current frame
emotion_results = emotion_detector.detect_emotions(frame)
# Loop through the detected faces and their emotions
for result in emotion_results:
bounding_box = result['box']
emotions = result['emotions']
# Draw bounding box around the face
x, y, w, h = bounding_box
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Get the most likely emotion
emotion_label = max(emotions, key=emotions.get)
confidence = emotions[emotion_label]
# Put the emotion label and confidence score on the frame
cv2.putText(frame, f'{emotion_label}: {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Face Mood Detector', frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Release the webcam and close windows
cap.release() cv2.destroyAllWindows()