Real-time facial emotion recognition from your webcam using a CNN trained on the FER2013 dataset. The app detects faces, draws bounding boxes, and overlays emotion labels with confidence bars — all live.
SEO: real-time emotion detection, facial expression recognition, FER2013, OpenCV, TensorFlow, Keras, CNN, webcam, computer vision, Python.
- Real-time detection at 20–30 FPS (hardware dependent)
- 7 emotions: Angry, Disgust, Fear, Happy, Neutral, Sad, Surprise
- Haar Cascade face detection (fast and lightweight)
- Confidence percentages with animated bars
- Snapshot capture with keyboard
- Simple CLI: choose camera, threshold, and model path
- Python 3.11 or newer
- A webcam (integrated or USB)
- Windows/Linux/macOS
- Create and activate a virtual environment
Windows PowerShell:
python -m venv .venv
.venv\Scripts\Activate.ps1Linux/macOS:
python3 -m venv .venv
source .venv/bin/activate- Install dependencies
pip install -r requirements.txt- Ensure the model file is present
Place fer2013_emotion_cnn.h5 in the project root (same folder as emotion_detector.py).
- Run
python emotion_detector.pyBasic run:
python emotion_detector.pyAdvanced options:
# Use a custom model
python emotion_detector.py --model path/to/your_model.h5
# Adjust confidence threshold (display behavior only)
python emotion_detector.py --threshold 0.5
# Select a different camera (e.g., external webcam)
python emotion_detector.py --camera 1Keyboard shortcuts:
qQuit the applicationsSave a snapshot of the current frame
- Dataset: FER2013 (Facial Expression Recognition 2013)
- Architecture: Convolutional Neural Network (CNN)
- Input: 48×48 RGB images, normalized to [0, 1]
- Output: 7-class probability distribution
- Model file:
fer2013_emotion_cnn.h5 - Model training: Created and trained by @zyna-b. Training repository and details: https://github.com/zyna-b/Face-Emotion-Detector-using-CNN
- Capture and mirror the webcam frame for a natural view.
- Detect faces with OpenCV’s Haar cascade.
- Resize face crops to 48×48 RGB and normalize.
- Run the CNN to get emotion probabilities.
- Draw bounding boxes, labels, and confidence bars.
- The provided model is often biased toward “Happy.” This stems from dataset/class imbalance during training rather than preprocessing. Retraining with class weighting/augmentation can help.
- Works best with frontal faces and decent lighting.
- Webcam won’t open: close other apps using the camera; try
--camera 1. - Low FPS: reduce camera resolution, ensure drivers are up to date, prefer CPU-only first.
- Import errors on Windows: activate the virtual environment and reinstall dependencies.
opencv-python>=4.8.0
numpy>=1.24.0
tensorflow>=2.12.0
keras>=2.12.0
See requirements.txt for the authoritative list.
PRs are welcome. Ideas to explore:
- Retrain with class balancing and augmentation
- Add ONNX/MediaPipe backends as optional engines
- Support video files and RTSP streams
- Provide a simple web UI (Flask/FastAPI)
This project is licensed under the MIT License. See LICENSE.
emotion detection, facial emotion recognition, facial expression recognition, FER2013, OpenCV, TensorFlow, Keras, CNN, deep learning, computer vision, real-time, webcam, Python, face detection, machine learning, AI