A high-performance, real-time face mask detector built with OpenCV YuNet, MobileNetV2, and a threaded, batch-inference architecture.
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
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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
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)
- Python 3.8 β 3.11 (TensorFlow 2.x does not yet support Python 3.12+)
- A working webcam
# 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.pypython 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).
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 |
| 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. |
See CONTRIBUTING.md for guidelines on submitting pull requests, reporting bugs, and code style.
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.