Skip to content

zyna-b/Real-Time-Emotion-Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎭 Real-Time Emotion Detector (FER2013, OpenCV + TensorFlow)

Python Version TensorFlow OpenCV Keras License: MIT Platform

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.


🌟 Features

  • 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

🚀 Quick Start

Prerequisites

  • Python 3.11 or newer
  • A webcam (integrated or USB)
  • Windows/Linux/macOS

Install

  1. Create and activate a virtual environment

Windows PowerShell:

python -m venv .venv
.venv\Scripts\Activate.ps1

Linux/macOS:

python3 -m venv .venv
source .venv/bin/activate
  1. Install dependencies
pip install -r requirements.txt
  1. Ensure the model file is present

Place fer2013_emotion_cnn.h5 in the project root (same folder as emotion_detector.py).

  1. Run
python emotion_detector.py

📖 Usage

Basic run:

python emotion_detector.py

Advanced 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 1

Keyboard shortcuts:

  • q Quit the application
  • s Save a snapshot of the current frame

🧠 Model Details

  • 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

🔎 How It Works

  1. Capture and mirror the webcam frame for a natural view.
  2. Detect faces with OpenCV’s Haar cascade.
  3. Resize face crops to 48×48 RGB and normalize.
  4. Run the CNN to get emotion probabilities.
  5. Draw bounding boxes, labels, and confidence bars.

🐛 Known Issues

  • 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.

🧩 Troubleshooting

  • 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.

📦 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.

🤝 Contributing

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)

📝 License

This project is licensed under the MIT License. See LICENSE.

🔗 Keywords (SEO)

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

About

“Real-time facial emotion recognition (OpenCV + TensorFlow, FER2013) with webcam visualization”

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages