mark@arch:~/projects/facial_sentiment_analisys
← cd ~/projects facial_sentiment_analisys cover

markart25/facial_sentiment_analisys

project in 2nd year. mostly vibe coding

Python 4 commits 0 stars updated 2 May 2026
view on github ↗

README.md — rendered from the repo

Facial Sentiment Analysis

A simple PyTorch project that classifies faces as happy or sad in real time from a webcam feed. A custom CNN is trained on labelled face images, and OpenCV's Haar cascade is used to detect faces in each frame before passing them through the model.

Features

Project structure

.
├── sentiment_analysis_model.py   # Training + real-time webcam detection
├── test_model.py                 # Evaluate the trained model on test_data/
├── emotion_model.pth             # Saved model weights (created after training)
├── data/                         # Training images
│   ├── happy/                    # *.jpg images of happy faces
│   └── sad/                      # *.jpg images of sad faces
└── test_data/                    # Test images (same structure as data/)
    ├── happy/
    └── sad/

Filenames may differ in your repo — adjust if needed.

Requirements

Install with:

pip install torch torchvision opencv-python pillow numpy

A webcam is required for the real-time detection script.

Dataset setup

Organise your training images into folders named after each emotion:

data/
├── happy/
│   ├── img1.jpg
│   ├── img2.jpg
│   └── ...
└── sad/
    ├── img1.jpg
    ├── img2.jpg
    └── ...

Only .jpg files are picked up by the loader. Mirror the same structure under test_data/ for evaluation.

Usage

1. Train the model

In sentiment_analysis_model.py, uncomment the training lines in the __main__ block:

print("Training model...")
train_model('data', epochs=15, batch_size=32)

Then run:

python sentiment_analysis_model.py

This will train the CNN for 15 epochs and save the weights to emotion_model.pth.

2. Run real-time detection

With emotion_model.pth present, run:

python sentiment_analysis_model.py

A window will open showing the webcam feed with green boxes around detected faces and the predicted emotion + confidence shown next to each one. Press q to quit.

3. Evaluate on a test set

python test_model.py

This loads emotion_model.pth, runs it over everything in test_data/, and prints overall accuracy.

Model architecture

A small convolutional network designed for 128×128 RGB inputs:

Trained with:

Notes & limitations

License

MIT