Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎭 Face Mask Detection System

A high-performance, real-time face mask detector built with OpenCV YuNet, MobileNetV2, and a threaded, batch-inference architecture.

Python OpenCV TensorFlow License Platform


✨ Overview

This system detects in real time whether people in a live webcam feed are wearing a face mask. It processes every frame through a two-stage AI pipeline β€” fast face localisation followed by mask classification β€” and overlays results directly on the camera feed with a clean, minimalist UI.

Key highlights:

  • πŸš€ Threaded capture β€” camera I/O runs in a dedicated daemon thread, eliminating blocking waits
  • ⚑ Batch inference β€” all faces in a frame are classified in a single GPU/CPU call
  • 🎯 Dual-resolution pipeline β€” full-res for display, downscaled for fast AI inference
  • πŸ–ΌοΈ Resolution-independent UI β€” scales cleanly from 720p to 4K

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      main.py (Orchestrator)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                 β”‚                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  camera.py    β”‚  β”‚ detector.py  β”‚  β”‚     ui.py         β”‚
β”‚               β”‚  β”‚              β”‚  β”‚                   β”‚
β”‚ ThreadedCameraβ”‚  β”‚ YuNetFace    β”‚  β”‚ UIManager         β”‚
β”‚ (daemon I/O)  β”‚  β”‚ Detector     β”‚  β”‚ - Pill labels     β”‚
β”‚               β”‚  β”‚              β”‚  β”‚ - Rounded boxes   β”‚
β”‚ β†’ Frame Queue β”‚  β”‚ MaskDetector β”‚  β”‚ - HUD dashboard   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚ (batch CNN)  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                   β”‚  utils.py    β”‚
                   β”‚ (Compat.     β”‚
                   β”‚  Model Load) β”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                   β”‚  config.py   β”‚
                   β”‚ (All tunable β”‚
                   β”‚  constants)  β”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data flow per frame:

Camera β†’ Flip (mirror) β†’ Downscale β†’ YuNet detect β†’ Crop ROIs β†’
  β†’ MobileNetV2 batch predict β†’ Map coords back β†’ Draw UI β†’ Display

πŸ“‚ Project Structure

Face-Mask-Detection-Python/
β”œβ”€β”€ main.py              # Application entry point & main loop
β”œβ”€β”€ setup_env.py         # One-click model download script
β”œβ”€β”€ requirements.txt     # Python dependencies
β”œβ”€β”€ models/              # AI model weights (downloaded by setup_env.py)
β”‚   β”œβ”€β”€ face_detection_yunet_2023mar.onnx
β”‚   └── mask_detector.h5
└── src/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ config.py        # All tunable constants (single source of truth)
    β”œβ”€β”€ camera.py        # ThreadedCamera β€” non-blocking frame capture
    β”œβ”€β”€ detector.py      # YuNetFaceDetector + MaskDetector (batch CNN)
    β”œβ”€β”€ ui.py            # UIManager β€” rendering engine
    └── utils.py         # Robust Keras model loader (compat. patches)

πŸ› οΈ Installation

Prerequisites

  • Python 3.8 – 3.11 (TensorFlow 2.x does not yet support Python 3.12+)
  • A working webcam

Steps

# 1. Clone the repository
git clone https://github.com/rebeeh/Face-Mask-Detection-Python.git
cd Face-Mask-Detection-Python

# 2. (Recommended) Create a virtual environment
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Download AI models (one-time, ~11 MB total)
python setup_env.py

▢️ Usage

python main.py
Key Action
Q or ESC Quit the application
Close window Quit the application

The HUD (top-centre) shows live FPS, Mask count, and Alert count (faces without masks).


βš™οΈ Configuration

All tunable parameters live in src/config.py β€” no edits needed in main.py.

Constant Default Description
CAMERA_INDEX 0 Webcam device index
CAMERA_WIDTH 1280 Capture width (pixels)
CAMERA_HEIGHT 720 Capture height (pixels)
INFERENCE_WIDTH 640 AI inference frame width (lower = faster)
YUNET_SCORE_THRESHOLD 0.6 YuNet face detection confidence cutoff
CONFIDENCE_THRESHOLD 0.5 Mask classification decision boundary
MIN_FACE_SIZE_PX 10 Minimum face box size to process (filters noise)
BATCH_SIZE 32 Mask classifier batch size

πŸ”§ Troubleshooting

Symptom Fix
"Failed to open camera source" Another application is using the webcam, or CAMERA_INDEX is wrong. Try CAMERA_INDEX = 1.
"TensorFlow is not installed" Run pip install tensorflow (Windows/Linux) or pip install tensorflow-macos (Apple Silicon).
"Mask model not found" Run python setup_env.py to download model files.
Low FPS Lower INFERENCE_WIDTH in config.py (e.g., 320), or reduce CAMERA_WIDTH/CAMERA_HEIGHT.
"YuNet model not found" Run python setup_env.py. If the download fails, check internet access.
Mask model loads with warnings Normal β€” utils.py applies backward-compatibility patches transparently.

🀝 Contributing

See CONTRIBUTING.md for guidelines on submitting pull requests, reporting bugs, and code style.


πŸ“„ License

This project is licensed under the MIT License β€” see LICENSE for details.


Face detection powered by OpenCV YuNet. Mask classifier architecture from chandrikadeb7/Face-Mask-Detection.

About

A robust, real-time Face Mask Detection system built with Python, OpenCV, and TensorFlow. Features automated environment setup, FPS monitoring, and fail-safe execution.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages