Open-source facial emotion recognition using Deep Learning, PyTorch, OpenCV, and FER-2013.
OpenEmotion is a deep-learning project that detects faces from images or a live webcam and predicts one of seven facial-expression classes.
Status: 🚧 Active development
OpenEmotion can process a live camera feed using OpenCV:
Webcam
↓
OpenCV
↓
Face Detection
↓
Face Preprocessing
↓
EfficientNet-B0
↓
Emotion + Confidence
| Emotion | Class |
|---|---|
| 😠 | Angry |
| 🤢 | Disgust |
| 😨 | Fear |
| 😀 | Happy |
| 😐 | Neutral |
| 😢 | Sad |
| 😲 | Surprise |
- 🧠 PyTorch deep-learning model
- 👤 Face detection using OpenCV
- 🎭 7-class facial-expression classification
- 📷 Real-time webcam detection
- 🖼️ Image-based inference
- ⚡ GPU support with CUDA
- 💻 CPU support
- 📊 Classification report
- 📈 Confusion matrix
- 📓 Kaggle/Jupyter training notebook
- 🔧 Modular Python source code
- 🧪 Basic automated tests
- Python
- PyTorch
- TorchVision
- OpenCV
- EfficientNet-B0
- FER-2013
- NumPy
- Pandas
- Scikit-learn
- Matplotlib
- Seaborn
OpenEmotion/
│
├── README.md
├── LICENSE
├── requirements.txt
├── pyproject.toml
├── .gitignore
│
├── notebooks/
│ └── emotion_detection.ipynb
│
├── src/
│ └── openemotion/
│ ├── __init__.py
│ ├── config.py
│ ├── model.py
│ ├── preprocessing.py
│ ├── inference.py
│ └── face_detector.py
│
├── scripts/
│ ├── train.py
│ ├── evaluate.py
│ └── webcam.py
│
├── tests/
│ └── test_inference.py
│
├── models/
│ └── .gitkeep
│
└── docs/
└── architecture.md
Recommended:
- Python 3.10+
- Git
- 8 GB+ RAM
- NVIDIA GPU + CUDA for faster training — optional
- Webcam for real-time detection — optional
OpenEmotion can run on CPU, but training will be considerably slower.
git clone https://github.com/sharmaB01/OpenEmotion.gitEnter the project directory:
cd OpenEmotionpython3 -m venv .venvActivate it:
source .venv/bin/activatepython -m venv .venvActivate:
.venv\Scripts\activateUpgrade pip:
python -m pip install --upgrade pipInstall dependencies:
pip install -r requirements.txtOpenEmotion uses the FER-2013 dataset.
The dataset is not included in this GitHub repository.
https://www.kaggle.com/datasets/msambare/fer2013 Download the FER-2013 dataset from a legitimate source and arrange it in the following structure:
fer2013/
│
├── train/
│ ├── angry/
│ ├── disgust/
│ ├── fear/
│ ├── happy/
│ ├── neutral/
│ ├── sad/
│ └── surprise/
│
└── test/
├── angry/
├── disgust/
├── fear/
├── happy/
├── neutral/
├── sad/
└── surprise/
Each directory should contain the corresponding facial-expression images.
For example:
fer2013/train/happy/
├── image1.jpg
├── image2.jpg
├── image3.jpg
└── ...
OpenEmotion currently uses EfficientNet-B0 as its baseline model.
To train:
python scripts/train.py \
--data /path/to/fer2013 \
--epochs 10Example:
python scripts/train.py \
--data ~/datasets/fer2013 \
--epochs 10You can change the batch size:
python scripts/train.py \
--data ~/datasets/fer2013 \
--epochs 10 \
--batch-size 64After training, the model is saved to:
models/openemotion_efficientnet_b0.pth
If you don't have a local GPU, you can train OpenEmotion using Kaggle.
The repository contains the training notebook:
notebooks/emotion_detection.ipynb
Upload the notebook to Kaggle and attach a FER-2013 dataset.
Kaggle GPU can then be used for training.
After training, download the generated model:
openemotion_efficientnet_b0.pth
Place it in:
models/
Resulting structure:
OpenEmotion/
└── models/
└── openemotion_efficientnet_b0.pth
You can then use the trained model with the local webcam application.
After training:
python scripts/evaluate.py \
--data /path/to/fer2013 \
--model models/openemotion_efficientnet_b0.pthExample:
python scripts/evaluate.py \
--data ~/datasets/fer2013 \
--model models/openemotion_efficientnet_b0.pthThe evaluation provides:
- Precision
- Recall
- F1-score
- Classification report
- Confusion matrix
Once you have a trained model, start the webcam application:
python scripts/webcam.py \
--model models/openemotion_efficientnet_b0.pthOpenEmotion will:
Webcam
↓
OpenCV Frame
↓
Face Detection
↓
Face Crop
↓
Image Preprocessing
↓
EfficientNet-B0
↓
Emotion Prediction
↓
Confidence Score
The webcam window will display something similar to:
┌─────────────────────────────┐
│ │
│ ┌───────────────┐ │
│ │ │ │
│ │ FACE │ │
│ │ │ │
│ └───────────────┘ │
│ │
│ Happy 87.4% │
│ │
└─────────────────────────────┘
Press Q to close the application.
The default camera is:
0
To use another camera:
python scripts/webcam.py \
--model models/openemotion_efficientnet_b0.pth \
--camera 1OpenEmotion automatically checks whether CUDA is available.
You can check your PyTorch installation:
python -c "import torch; print(torch.cuda.is_available())"If you get:
True
PyTorch can use your NVIDIA GPU.
If you get:
False
the project will run on CPU.
The complete experimentation notebook is available at:
notebooks/emotion_detection.ipynb
Start Jupyter:
jupyter notebookor:
jupyter labThe notebook can be used for:
- Dataset exploration
- Data visualization
- Model training
- Model evaluation
- Classification reports
- Confusion matrices
- Experimentation
For normal usage, the Python scripts are recommended.
OpenEmotion can also be used directly from Python.
Example:
import torch
from openemotion.model import load_model
from openemotion.inference import predict_emotion
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
model = load_model(
"models/openemotion_efficientnet_b0.pth",
device,
7
)
emotion, confidence = predict_emotion(
model,
face_image,
device
)
print("Emotion:", emotion)
print("Confidence:", confidence)Install pytest if required:
pip install pytestRun:
pytest| Component | Value |
|---|---|
| Model | EfficientNet-B0 |
| Framework | PyTorch |
| Input | 224 × 224 RGB |
| Output | 7 classes |
| Dataset | FER-2013 |
| Face Detection | OpenCV Haar Cascade |
| GPU | CUDA supported |
| CPU | Supported |
Facial-expression recognition is an estimation problem and is not perfectly reliable.
Performance can be affected by:
- Lighting
- Camera quality
- Face angle
- Occlusion
- Multiple faces
- Image quality
- Facial appearance
- Dataset bias
- Differences between training and real-world images
The predicted class should be considered a model prediction, not a definitive measurement of a person's actual emotional state.
- FER-2013 dataset support
- PyTorch training
- EfficientNet-B0 baseline
- Model evaluation
- Classification report
- Confusion matrix
- OpenCV face detection
- Real-time webcam prototype
- Improve model accuracy
- Better face detection
- Multi-face tracking
- FPS optimization
- Image inference CLI
- Video inference
- ONNX export
- FastAPI inference API
- React web application
- Docker support
- CI/CD
- Model benchmarking
- Improved automated tests
Contributions are welcome!
git clone https://github.com/sharmaB01/OpenEmotion.git
cd OpenEmotionCreate a feature branch:
git checkout -b feature/my-featureMake your changes and run the tests:
pytestCommit your changes:
git add .
git commit -m "Add my feature"Push the branch:
git push origin feature/my-featureThen open a Pull Request.
OpenEmotion is released under the MIT License.
See LICENSE for details.
OpenEmotion is built using:
- PyTorch
- TorchVision
- OpenCV
- Scikit-learn
- FER-2013
OpenEmotion is an open-source research and educational project.
Facial-expression classification is an estimation task and should not be interpreted as a reliable measurement of a person's internal emotional state.
Do not use the model as the sole basis for decisions involving employment, education, healthcare, law enforcement, financial services, or other high-impact decisions.
For experienced users:
git clone https://github.com/sharmaB01/OpenEmotion.git
cd OpenEmotion
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtPrepare FER-2013:
fer2013/
├── train/
└── test/
Train:
python scripts/train.py \
--data /path/to/fer2013 \
--epochs 10Evaluate:
python scripts/evaluate.py \
--data /path/to/fer2013 \
--model models/openemotion_efficientnet_b0.pthRun webcam:
python scripts/webcam.py \
--model models/openemotion_efficientnet_b0.pthPress Q to exit.
PyTorch + OpenCV + EfficientNet-B0 + FER-2013
⭐ If you find this project useful, consider giving the repository a star.