This project repository provides two scripts for recognizing emotions from facial expressions in images and videos. The project utilizes a Convolutional Neural Network (CNN) built using TensorFlow's Keras API, OpenCV for image processing, and a pre-trained model to predict emotions.
The repository includes:
- A CNN model for emotion recognition.
- Scripts for processing images and videos to detect and display emotions.
To run the code, you need the following Python libraries:
numpy
opencv-python
(cv2
)matplotlib
tensorflow
google-colab
(forcv2_imshow
in Colab environments)
The model consists of multiple convolutional layers followed by max-pooling layers, dropout layers for regularization, and dense layers. It outputs a softmax probability over 7 possible emotions: Angry, Disgusted, Fearful, Happy, Neutral, Sad, and Surprised.
-
Model Definition and Weights Loading: The model is defined using TensorFlow's Keras API. Pre-trained weights are loaded from
model.h5
. -
Emotion Recognition Function (
emotion_recog
):- Detects faces using OpenCV's Haar Cascade Classifier.
- Converts detected face regions to grayscale.
- Resizes and normalizes the face images to 48x48 pixels.
- Predicts the emotion using the loaded model and labels it on the image.
-
Initialization:
- Capture video using
cv2.VideoCapture('video.mp4')
. - Read the first frame to determine video dimensions.
- Capture video using
-
Processing:
- For each frame, detect faces, predict emotions, and display the results.
- Save the processed frames into an output video file
output.avi
.
-
Display:
- Display the frames with detected emotions using
cv2_imshow
(compatible with Google Colab).
- Display the frames with detected emotions using