All notes

AI/ML & Data Science / FaceMaskDet

Face Mask Detection System

A comprehensive face mask detection system that combines computer vision, deep learning, and real-time web interfaces to ensure public safety and health compliance.

October 22, 20256 min readPython1 stars
Open the full technical write-up

AI-Powered Real-Time Mask Compliance Monitoring

** Protect your community with current AI technology**
A comprehensive face mask detection system that combines computer vision, deep learning, and real-time web interfaces to ensure public safety and health compliance.


What Makes This Special?

Lightning Fast & Accurate

  • Ultra-lightweight models: Only 2.4M parameters for blazing-fast inference
  • Real-time detection: Process webcam streams at 30+ FPS
  • Multi-face support: Detect and classify multiple faces simultaneously
  • High accuracy: 99%+ accuracy on mask detection tasks

Advanced Detection Capabilities

  • Correct mask wearing - Green bounding box
  • Incorrect mask wearing - Yellow bounding box (nose/mouth exposed)
  • No mask detected - Red bounding box with audio alerts

Beautiful Web Interface

  • Interactive Dash-based dashboard
  • Real-time video streaming
  • Image upload and processing
  • Configurable confidence thresholds
  • Audio alarm system for violations

Architecture Overview

graph TB
    A[πŸ“· Input Source] --> B[πŸ” Face Detection]
    B --> C[🎭 Mask Classification]
    C --> D[πŸ“Š Results Processing]
    D --> E[πŸ–₯️ Web Interface]
    D --> F[πŸ”Š Audio Alerts]
    
    B --> G[OpenCV DNN
SSD Face Detector]
    C --> H[MobileNetV2
Mask Classifier]
    E --> I[Dash Web App]
    F --> J[MP3 Audio Files]

Quick Start Guide

Option 1: One-Click Setup

# Clone the repository
git clone https://github.com/sidx255/FaceMaskDetection
cd FaceMaskDetection

# Create virtual environment
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Run the application
python main.py

Option 2: Docker Setup

# Build and run with Docker
docker build -t face-mask-detection .
docker run -p 8050:8050 face-mask-detection

How to Use

Web Application

  1. Start the server:

    python main.py
    
  2. Open your browser: Navigate to http://localhost:8050

  3. Choose your mode:

    • Image Mode: Upload and analyze static images
    • Video Mode: Real-time webcam detection
  4. Configure settings:

    • Select model (MFN/RMFD)
    • Adjust confidence threshold
    • Enable/disable audio alerts

Command Line Interface

Image Detection

python src/detect_mask_image.py -i example_images/pic1.jpg -m MFN -c 0.5

Video Detection

python src/detect_mask_video.py -m MFN -c 0.5

Model Training

python src/train.py -d MFN

AI Models & Performance

Model Comparison

Model Classes Parameters Accuracy Use Case
MFN 3 2.4M 99.1% Comprehensive detection
RMFD 2 2.4M 99.3% Binary classification

Detection Classes

MFN Model (3-Class)

  • Correct Mask: Properly worn mask
  • Incorrect Mask: Improperly worn (nose/mouth exposed)
  • No Mask: No face covering detected

RMFD Model (2-Class)

  • With Mask: Face covering detected
  • Without Mask: No face covering

Technical Stack

Core Technologies

  • ** Python 3.7+** - Primary programming language
  • ** TensorFlow 2.x** - Deep learning framework
  • ** OpenCV 4.x** - Computer vision library
  • ** Dash** - Interactive web applications
  • ** NumPy** - Numerical computing
  • ** Matplotlib** - Data visualization

AI/ML Libraries

  • ** MobileNetV2** - Lightweight CNN architecture
  • ** OpenCV DNN** - Face detection pipeline
  • ** Keras** - High-level neural network API
  • ** Scikit-learn** - Machine learning utilities

How files are organized

FaceMaskDetection/
β”œβ”€β”€ 🎯 main.py                 # Web application entry point
β”œβ”€β”€ βš™οΈ config.py              # Configuration management
β”œβ”€β”€ πŸ“‹ requirements.txt        # Python dependencies
β”œβ”€β”€ πŸš€ setup.py               # Installation script
β”œβ”€β”€ πŸ“– README.md              # This file
β”‚
β”œβ”€β”€ πŸ“ src/                    # Source code
β”‚   β”œβ”€β”€ πŸ–ΌοΈ detect_mask_image.py    # Image detection
β”‚   β”œβ”€β”€ πŸ“Ή detect_mask_video.py    # Video detection
β”‚   └── πŸŽ“ train.py               # Model training
β”‚
β”œβ”€β”€ 🧠 models/                 # AI models
β”‚   β”œβ”€β”€ πŸ‘€ face_detector/      # Face detection models
β”‚   └── 🎭 mask_detector/      # Mask classification models
β”‚
β”œβ”€β”€ 🎨 assets/                 # Web assets
β”‚   β”œβ”€β”€ 🎨 base.css            # Styling
β”‚   β”œβ”€β”€ πŸ”Š *.mp3               # Audio alerts
β”‚   └── πŸ–ΌοΈ *.jpg               # Example images
β”‚
β”œβ”€β”€ πŸ“Š checkpoints/            # Training checkpoints
β”œβ”€β”€ πŸ“ˆ figures/                # Training plots
└── πŸ“ output/                 # Generated outputs

Core workflows

Performance Optimized

  • Lightweight models for edge deployment
  • GPU acceleration support
  • Batch processing for multiple faces
  • Memory efficient inference pipeline

User Experience

  • Intuitive web interface with real-time feedback
  • Drag-and-drop image upload
  • Responsive design for all devices
  • Audio-visual alerts for violations

Developer Friendly

  • Modular architecture for easy customization
  • Comprehensive documentation and examples
  • CLI tools for batch processing
  • Training scripts for model fine-tuning

Performance Metrics

Model Accuracy

MFN Model Performance:
β”œβ”€β”€ Correct Mask:     99.2% accuracy
β”œβ”€β”€ Incorrect Mask:   98.8% accuracy
└── No Mask:         99.5% accuracy

RMFD Model Performance:
β”œβ”€β”€ With Mask:       99.3% accuracy
└── Without Mask:    99.1% accuracy

Inference Speed

  • CPU: ~50ms per frame (Intel i7)
  • GPU: ~15ms per frame (NVIDIA GTX 1060)
  • Mobile: ~100ms per frame (ARM Cortex-A78)

Training Your Own Models

Dataset Preparation

  1. Organize your data:

    data/
    β”œβ”€β”€ MFN/
    β”‚   β”œβ”€β”€ face_with_mask_correct/
    β”‚   β”œβ”€β”€ face_with_mask_incorrect/
    β”‚   └── face_no_mask/
    └── RMFD/
        β”œβ”€β”€ face_with_mask/
        └── face_no_mask/
    
  2. Start training:

    python src/train.py -d MFN
    

Training Features

  • Data augmentation for reliable models
  • Early stopping to prevent overfitting
  • Model checkpoints for recovery
  • Training visualization with plots

Configuration Options

Detection Parameters

# Confidence thresholds
FACE_DETECTION_CONFIDENCE = 0.5    # Face detection threshold
MASK_CLASSIFICATION_CONFIDENCE = 0.5  # Mask classification threshold

# Model selection
MODEL_TYPE = "MFN"  # or "RMFD"

# Audio settings
AUDIO_ALERTS_ENABLED = True
ALERT_VOLUME = 0.8

Web Interface Settings

# Server configuration
HOST = "0.0.0.0"
PORT = 8050
DEBUG = False

# UI customization
THEME = "light"  # or "dark"
LANGUAGE = "en"  # or "es", "fr", etc.

Deployment Options

Local Development

python main.py

Production Server

gunicorn main:server -b 0.0.0.0:8050

Docker Deployment

docker run -p 8050:8050 face-mask-detection

Cloud Deployment

  • AWS: EC2 with GPU instances
  • Google Cloud: AI Platform
  • Azure: Machine Learning Service

Contributing

We welcome contributions! Here's how you can help:

Bug Reports

  • Use GitHub Issues
  • Include system information
  • Provide error logs

Feature Requests

  • Describe the use case
  • Explain the expected behavior
  • Consider implementation complexity

Code Contributions

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.


Acknowledgments

  • OpenCV team for excellent computer vision tools
  • TensorFlow team for the powerful ML framework
  • Dash team for the amazing web framework
  • Community contributors for feedback and improvements

Support & Contact


Related notes

AI/ML & Data Science

AI Lab

A hands-on AI, machine learning, deep learning, computer vision, and data analytics lab built for learners who want practical projects, readable code, and a clear roadmap.

AI/ML & Data Science

CNN

Pure NumPy implementation of a tiny convolutional neural network (CNN) designed for education and experimentation.

AI/ML & Data Science

Emotion Detection Chatbot

An intelligent chatbot system that detects emotions from user text input using advanced Machine Learning, Deep Learning, and Natural Language Processing techniques.