Skip to content

Rahul-codehub/Emotion-Detection-using-Facial-Expression

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emotion Detection Using Facial Expression

A Python project that detects human faces from a webcam and predicts facial emotions in real time using OpenCV and a CNN trained with TensorFlow/Keras.

Features

  • Detects faces from live webcam video using Haar Cascade.
  • Preprocesses face ROI with grayscale conversion, resizing, and normalization.
  • Trains a CNN on FER2013 or a similar folder-based emotion dataset.
  • Classifies seven emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, Neutral.
  • Draws a bounding box and emotion label on the webcam feed.
  • Saves the trained model, class labels, and accuracy/loss plots.
  • Optional CSV emotion logging during real-time detection.

Project Structure

.
├── dataset/
│   └── README.md
├── logs/
│   └── .gitkeep
├── model/
│   └── .gitkeep
├── plots/
│   └── .gitkeep
├── main.py
├── train.py
├── utils.py
├── requirements.txt
└── README.md

Setup

Create and activate a virtual environment:

py -3.11 -m venv .venv
.\.venv\Scripts\activate

Install dependencies:

pip install -r requirements.txt

Dataset

FER2013 is commonly distributed as fer2013.csv with these columns:

  • emotion: numeric class id
  • pixels: 48x48 grayscale image pixels as a space-separated string
  • Usage: Training, PublicTest, or PrivateTest

Place it here:

dataset/fer2013.csv

The FER2013 label mapping used by this project is:

0 Angry
1 Disgust
2 Fear
3 Happy
4 Sad
5 Surprise
6 Neutral

You can also use a folder dataset:

dataset/
├── train/
│   ├── Angry/
│   ├── Happy/
│   └── ...
├── validation/
│   ├── Angry/
│   ├── Happy/
│   └── ...
└── test/
    ├── Angry/
    ├── Happy/
    └── ...

Train the Model

Train with FER2013 CSV:

python train.py --dataset dataset/fer2013.csv --epochs 35 --batch-size 64

Train with a folder dataset:

python train.py --dataset dataset --epochs 35 --batch-size 64

Training creates:

  • model/emotion_cnn.h5
  • model/labels.json
  • plots/training_curves.png

The terminal prints test accuracy and loss when a test split is available.

Run Real-Time Emotion Detection

After training:

python main.py --model model/emotion_cnn.h5

Press q or Esc to close the webcam window.

Optional logging:

python main.py --save-logs --log-file logs/emotion_log.csv

Useful Options

python train.py --help
python main.py --help

Examples:

python main.py --camera 1
python main.py --min-confidence 0.60
python train.py --epochs 50 --learning-rate 0.0005

Notes

  • The model file is not included because it must be trained from the dataset.
  • Accuracy depends on the dataset quality, lighting, camera angle, and class balance.
  • For better results, train for more epochs, use balanced data, and keep the face well lit and centered.
  • TensorFlow wheels are tied to Python versions. If pip install tensorflow fails on a very new Python release, install a TensorFlow-supported Python version and recreate the virtual environment with that interpreter.

Releases

No releases published

Packages

 
 
 

Contributors

Languages