This project provides a real-time facial expression recognition system using a webcam and the FER2013 CNN model trained in the accompanying Jupyter notebook.
- Real-time emotion detection from webcam feed
- 7 emotion classes: Angry 😠, Disgust 🤢, Fear 😨, Happy 😊, Sad 😢, Surprise 😲, Neutral 😐
- Visual feedback with bounding boxes, emotion labels, and confidence scores
- Performance monitoring with FPS display
- Interactive controls for saving frames and toggling fullscreen
- GPU acceleration support (CUDA/CPU automatic detection)
- Webcam or built-in camera
- Optional: CUDA-capable GPU for faster inference
- Python 3.7+
- Required packages (install via requirements file)
-
Clone or download the repository
cd fer_and_ter_model -
Install required packages
pip install -r requirements_camera_inference.txt
-
Ensure model file is present
- The trained model file
fer2013_final_model.pthshould be in the same directory - This file is generated by running the training notebook
fer2013_model_training.ipynb
- The trained model file
python camera_fer_inference.py# Use specific model file
python camera_fer_inference.py --model_path path/to/your/model.pth
# Use specific camera (if multiple cameras available)
python camera_fer_inference.py --camera_id 1
# Force CPU usage (disable GPU)
python camera_fer_inference.py --device cpu
# Force CUDA usage
python camera_fer_inference.py --device cudaWhile the application is running:
- Q: Quit the application
- S: Save the current frame with predictions
- F: Toggle fullscreen mode
Before running the camera inference, you can test the model loading and basic functionality:
python test_fer_model.pyThis will verify:
- Model loading from the .pth file
- Basic inference functionality
- Image preprocessing pipeline
The emotion recognition model is a Convolutional Neural Network (CNN) with the following architecture:
- Input: 48x48 grayscale face images
- Convolutional layers: 4 conv layers with batch normalization
- Pooling: Max pooling and adaptive average pooling
- Fully connected layers: 3 FC layers with dropout
- Output: 7 emotion classes
- Parameters: ~1.2M trainable parameters
- Input preprocessing: Resize to 48x48, normalize to [-1, 1]
- Output: Softmax probabilities for 7 emotion classes
- FPS: Typically 15-30 FPS depending on hardware
- Latency: ~30-50ms per frame on modern hardware
- Accuracy: Model accuracy depends on lighting conditions and face visibility
- Good lighting: Ensure faces are well-lit
- Face positioning: Keep faces relatively centered and upright
- Distance: Maintain reasonable distance from camera (1-3 feet)
- GPU usage: Use CUDA-capable GPU for better performance
-
"Model file not found"
- Ensure
fer2013_final_model.pthis in the current directory - Run the training notebook to generate the model file
- Ensure
-
"Failed to open camera"
- Check if camera is being used by another application
- Try different camera IDs (0, 1, 2, etc.)
- Check camera permissions
-
Low FPS or high latency
- Close other applications using the camera
- Use GPU acceleration with
--device cuda - Reduce camera resolution in the script if needed
-
Poor emotion recognition
- Ensure good lighting conditions
- Keep face clearly visible and unobstructed
- Model works best with frontal face views
If you encounter package conflicts:
# Create a virtual environment
python -m venv fer_env
source fer_env/bin/activate # On Windows: fer_env\Scripts\activate
# Install requirements
pip install -r requirements_camera_inference.txtTo retrain or fine-tune the model:
- Open
fer2013_model_training.ipynbin Jupyter - Follow the training process in the notebook
- The trained model will be saved as
fer2013_final_model.pth
- Uses OpenCV's Haar Cascade classifier for face detection
- Processes detected faces individually for emotion recognition
emotion_labels = {
0: 'Angry', # 😠
1: 'Disgust', # 🤢
2: 'Fear', # 😨
3: 'Happy', # 😊
4: 'Sad', # 😢
5: 'Surprise', # 😲
6: 'Neutral' # 😐
}Each emotion has a unique color for the bounding box:
- Angry: Red
- Disgust: Green
- Fear: Purple
- Happy: Yellow
- Sad: Blue
- Surprise: Orange
- Neutral: Gray
Feel free to contribute improvements:
- Better face detection algorithms
- Model architecture improvements
- Additional emotion classes
- Performance optimizations
- UI/UX enhancements
This project is for educational and research purposes. Please ensure proper attribution when using the code or model.