Skip to main content
Tayyab Manan
Home
Alt+H
Projects
Alt+P
Blog
Alt+B
About
Alt+A
Contact
Alt+C
Resume
Tayyab Manan
HomeProjectsBlogAboutContactResume
  1. Home
  2. Projects
  3. Computer Vision

Face Expression Detection

Deep Learning Emotion Recognition in Group Photos

A deep learning web application that detects and classifies facial expressions in images. Built with PyTorch and Flask, featuring a ResNet-18 model trained on the RAF-DB dataset achieving 80% accuracy.

December 2025
PythonPyTorchFlaskMTCNNOpenCVResNet-18DockerHugging Face
Live DemoSource Code
Previous projectWaterTrace PakistanNext project US Visa Approval Prediction
Back to all projects
HomeProjectsBlogAboutContactResume
 

Islamabad, Pakistan · UTC+5

Tayyab Manan

©

    Overview

    A deep learning web app that detects and classifies facial expressions in images. Built with PyTorch and Flask, using a ResNet-18 model trained on the RAF-DB dataset at 80% accuracy.

    Read the full technical deep-dive →

    the model reading a group · 8 faces, all 7 classes · 85–92% confidence
    the model reading a group · 8 faces, all 7 classes · 85–92% confidence

    Features

    • 7 Emotion Classes: Surprise, Fear, Disgust, Happiness, Sadness, Anger, Neutral
    • Multi-face Detection: Detects and analyzes multiple faces in a single image
    • MTCNN Face Detection: Primary detector with Haar Cascade fallback for tough cases
    • Real-time Visualization: Annotated images with bounding boxes and emotion labels
    • Confidence Scores: Probability distribution across all emotion classes
    • Dark/Light Mode: Toggle between themes
    • Responsive Design: Works on desktop and mobile

    What I Did

    Face Detection Pipeline

    I used a two-stage approach with MTCNN as the primary face detector and Haar Cascade as fallback. MTCNN handles multiple faces in group photos well, and Haar Cascade picks up faces that MTCNN misses (unusual angles, extreme lighting).

    Model Training

    I used transfer learning with ResNet-18 pretrained on ImageNet and fine-tuned on RAF-DB (Real-world Affective Faces Database), which has 15,339 images across 7 emotion classes. The custom classification head uses batch normalization and dropout for regularization.

    Handling Class Imbalance

    RAF-DB has serious class imbalance (Happiness: 39%, Fear: 2.3%). I addressed this with focal loss, class weights, and weighted sampling to improve performance on minority classes like Fear and Disgust. The ensemble of models trained with different loss functions ended up being the most effective approach.

    Web Application

    I built a Flask web app with image upload, real-time emotion detection, and annotated output showing bounding boxes and confidence scores. Deployed on Hugging Face Spaces using Docker.

    Tech Stack

    ComponentTechnology
    ModelResNet-18 (transfer learning from ImageNet)
    Face DetectionMTCNN + Haar Cascade fallback
    BackendFlask + Gunicorn
    FrontendVanilla JS with CSS animations
    DatasetRAF-DB (Real-world Affective Faces Database)
    DeploymentDocker on Hugging Face Spaces

    Model Performance

    MetricValue
    Accuracy80%
    DatasetRAF-DB
    ArchitectureResNet-18
    Input Size100x100

    Per-Class Performance

    EmotionPerformanceNotes
    HappinessHighestLargest class in dataset
    NeutralHighWell-represented class
    SurpriseGoodDistinctive facial features
    SadnessModerateSubtle expressions
    AngerModerateOften confused with intense expressions
    FearLowerOnly 2.3% of training data
    DisgustLowerOnly 5% of training data

    Authors

    • Muhammad Tayyab
    • Syed Measum
    • Mustafa Rahim

    Acknowledgments

    • RAF-DB Dataset for training data
    • facenet-pytorch for MTCNN implementation
    • PyTorch for the deep learning framework