Skip to main content
Tayyab Manan
Home
Projects
Blog
About
Contact
Resume
Tayyab Manan
HomeProjectsBlogAboutContactResume
  1. Home
  2. Projects
  3. Face Expression Detection

Computer Vision · December 2025

Face Expression Detection

Deep Learning Emotion Recognition in Group Photos

Built with Python · PyTorch · Flask · MTCNN · OpenCV · ResNet-18 · Docker · Hugging 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 write-up →

    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

    The model classifies 7 expressions: Surprise, Fear, Disgust, Happiness, Sadness, Anger, and Neutral. It handles multiple faces in a single image, using MTCNN as the primary detector with a Haar Cascade fallback for the hard cases. The app returns the annotated image with bounding boxes and emotion labels, plus the full probability distribution across all 7 classes for each face. There's a dark/light toggle, and the layout works on desktop and mobile.

    What I did

    Face detection pipeline

    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

    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 tried focal loss, class weights, and weighted sampling to improve performance on minority classes like Fear and Disgust. An ensemble of models trained with different loss functions ended up working best.

    Web application

    A Flask app with image upload, emotion detection, and annotated output showing bounding boxes and confidence scores. Deployed on Hugging Face Spaces using Docker.

    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