Using DeepFace and OpenCV to detect emotion
This Python script utilizes OpenCV and DeepFace to perform real-time emotion detection through a webcam feed. The script continuously captures video frames from the webcam, analyzes the dominant emotion in each frame, and calculates an overall emotion score based on predefined emotion weights.
- OpenCV: A computer vision library for real-time image and video processing.
- DeepFace: A deep learning-based facial analysis library for emotion detection.
-
Initialization:
- Import necessary libraries (
cv2,json,DeepFace). - Set parameters, such as
frame_skipfor processing efficiency. - Open a webcam using OpenCV.
- Import necessary libraries (
-
Main Loop:
- Continuously capture frames from the webcam.
- Analyze every
frame_skip-th frame using DeepFace to obtain the dominant emotion. - Update the emotion scores based on predefined weights.
-
Display Frames:
- Display the video frames in a window using OpenCV.
- Press 'q' to stop the emotion detection loop.
-
Calculate Overall Emotion Score:
- Define positive and negative emotion lists.
- Sum the emotion scores for each category.
- Calculate the overall emotion score as the difference between positive and negative emotions.
-
Output:
- Display the overall emotion score in JSON format.
- The JSON includes an 'emotion_score' key with the calculated value.
-
Cleanup:
- Release the webcam when the script is terminated.
- Emotion scores are updated based on predefined weights for emotions like 'neutral,' 'happy,' 'surprise,' 'angry,' 'disgust,' 'fear,' and 'sad.'
- Positive emotions: 'happy,' 'surprise,' 'neutral.'
- Negative emotions: 'angry,' 'disgust,' 'fear,' 'sad.'
- The overall emotion score is calculated as the difference between the sum of positive emotion scores and the sum of negative emotion scores.
- Execute the script, and it will continuously display webcam frames with real-time emotion scores until the 'q' key is pressed.
use the test_video.py file to check how this work with real time or on video it will visulse the detection emotion on every frame.
some time many project need the score of emotion so you can use the score_emotion.py file, you can also change the scoreing to your required.
If you want to Integrate it to your Project Use the given file emotion_recognition.py
This Python script utilizes OpenCV and DeepFace to perform emotion detection on a video file. The script reads frames from the video, analyzes the dominant emotion in each frame using DeepFace, and calculates emotion scores for different categories.
video_path: Path to the video file for emotion detection.
- Returns a JSON-formatted string containing emotion scores for 'neutral,' 'happy,' 'surprise,' 'angry,' 'disgust,' 'fear,' and 'sad.'
-
Initialization:
- Define the function
detect_emotionsthat takes avideo_pathas input.
- Define the function
-
Video Processing Loop:
- Open the video file using OpenCV.
- Get the total number of frames in the video.
- Initialize a dictionary to store emotion scores for different categories.
-
Main Loop:
- Continuously read frames from the video.
- Analyze every 5th frame using DeepFace to obtain the dominant emotion.
- Update the emotion scores based on the dominant emotion in each frame.
- Break the loop if the 'q' key is pressed.
-
Emotion Score Calculation:
- Calculate emotion scores as a percentage of frames where each emotion is dominant.
- Format the scores to two decimal places.
-
Output:
- Return a JSON-formatted string containing emotion scores.
-
Script Execution:
- If the script is executed directly, specify the
video_pathand call thedetect_emotionsfunction. - Print the resulting emotion scores.
- If the script is executed directly, specify the
- Update the
video_pathvariable with the path to the desired video file. - Execute the script, and it will print JSON-formatted emotion scores based on the analysis of the video frames.