This project uses a Convolutional Neural Network (CNN) to recognize emotions from webcam images in real-time. It is trained on the FER-2013 dataset, which includes seven emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral. The model is tested with OpenCV to perform real-time emotion recognition via the webcam.
Make sure you have the following libraries installed:
- TensorFlow
- OpenCV
- NumPy
You can install the required libraries with the following commands:
pip install tensorflow opencv-python numpy
- train.py: Script for training the emotion recognition model.
- main.py: Script for real-time emotion recognition using the webcam.
- model: Folder where the trained model is saved (e.g., emotion_detection_model.h5).
- Prepare the data: The FER-2013 dataset is automatically loaded via the datasets library. This dataset contains images and their associated emotion labels.
- Train the model: Use the train.py script to train the model. The dataset consists of 48x48 grayscale images, which the model uses for emotion prediction.
Example:
python train.py
After training, the model is saved as an .h5 file, e.g., emotion_detection_model.h5.
- Start the webcam script: Open the main.py script to test the model in real-time. The script uses OpenCV to access the webcam and predict emotions from detected faces in real-time.
- Run the test:
python main.py
This opens a window where the camera is streamed, and each detected face has its predicted emotion displayed above it. You can exit the application by pressing q.
train.py
- Loading the dataset: The FER-2013 dataset is loaded using the datasets library.
- Model Architecture: A simple Convolutional Neural Network (CNN) is created with Keras, specifically designed for emotion image classification.
- Training the model: The model is trained with the training data and validated on the test data.
main.py
- Webcam streaming: OpenCV is used to start the webcam and read frames continuously.
- Face detection: OpenCV’s pre-trained Haar cascades are used to detect faces in the image.
- Emotion recognition: Each detected face is preprocessed and passed to the trained model for emotion prediction.
- Displaying results: The predicted emotion is displayed on the webcam feed.
The model recognizes the following seven emotions:
- Angry
- Disgust
- Fear
- Happy
- Sad
- Surprise
- Neutral
The application displays the webcam image with a green rectangle around the detected face. The predicted emotion is shown above the face:
[Happy] [Sad] [Neutral]
██████ ██████ ██████
- The accuracy of emotion recognition may vary depending on facial expressions, lighting conditions, and camera quality.
- For better performance in face detection or emotion recognition, additional data preprocessing or a different face detection model may be useful.
This project is licensed under the MIT License.