Real-Time Face Mask Detection using YOLO, OpenCV & ByteTrack
An end-to-end computer vision application for detecting face-mask usage from images, videos, and live webcam streams.
FaceMask Detection AI is a computer vision project designed to detect whether people are wearing face masks using a YOLO-based object detection model.
The application supports:
- ๐ผ๏ธ Image detection
- ๐ฅ Video detection
- ๐ท Real-time webcam detection
- ๐ฐ๏ธ Multi-object tracking with ByteTrack
- ๐ Live detection statistics
- ๐ฏ Configurable confidence thresholds
- โ๏ธ CPU and GPU inference
- ๐ Model performance evaluation
- ๐งช Automated testing
- ๐ GitHub Actions continuous integration
The project is designed to be easy to run locally while maintaining a clean structure suitable for further development, experimentation, and deployment.
Uses a YOLO object detection model to identify face-mask-related classes in visual input.
Run the detector directly against your webcam for real-time monitoring.
python main.py --webcamProcess existing videos and generate annotated detection output.
python main.py --source path/to/video.mp4For video and webcam streams, ByteTrack can maintain persistent IDs across frames.
Example:
Frame 1 โ Person โ Track ID 4
Frame 2 โ Person โ Track ID 4
Frame 3 โ Person โ Track ID 4
This makes it possible to track the same detected person across consecutive frames.
The application maintains live detection/tracking information instead of simply accumulating detections from previous frames.
Configure:
- Detection confidence
- Image size
- CPU/GPU device
- Model checkpoint
- Input source
- Display/output behavior
The project includes an evaluation script capable of reporting:
- Precision
- Recall
- mAP@50
- mAP@50-95
This provides a more meaningful measurement of model performance than an unsupported single "accuracy %" number.
โโโโโโโโโโโโโโโโโโโโโ
โ INPUT โ
โ โ
โ Image / Video / โ
โ Webcam โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ YOLO DETECTOR โ
โ โ
โ Object Detection โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ CLASS MAPPING โ
โ โ
โ Mask / No Mask / โ
โ Other Model Class โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ BYTE TRACK โ
โ โ
โ Persistent IDs โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโ
โ ANNOTATED OUTPUT โ
โ โ
โ Labels + Boxes + โ
โ Tracking Info โ
โโโโโโโโโโโโโโโโโโโโโ
| Technology | Purpose |
|---|---|
| ๐ Python | Application development |
| ๐ฏ Ultralytics YOLO | Object detection |
| ๐ฅ PyTorch | Deep learning framework |
| ๐๏ธ OpenCV | Image/video processing |
| ๐ฐ๏ธ ByteTrack | Multi-object tracking |
| ๐ข NumPy | Numerical operations |
| ๐งช unittest | Automated testing |
| ๐ค GitHub Actions | Continuous integration |
FaceMask-Detection-AI/
โ
โโโ ๐ models/
โ โโโ mask_detector.pt
โ
โโโ ๐ dataset/
โ โโโ data.yaml
โ
โโโ ๐ tests/
โ โโโ test_pipeline.py
โ
โโโ ๐ output/
โ โโโ generated results
โ
โโโ ๐ .github/
โ โโโ ๐ workflows/
โ โโโ tests.yml
โ
โโโ ๐ main.py
โโโ ๐ evaluate.py
โโโ โฌ๏ธ download_model.py
โโโ ๐ colab_train_yolov11_mask_detector.ipynb
โโโ ๐ requirements.txt
โโโ ๐ NOTICE.md
โโโ ๐ README.md
โโโ ๐ซ .gitignore
Note: Model weights and generated output files should generally not be committed to Git unless their redistribution and licensing terms have been verified.
git clone https://github.com/HadeedJalani/FaceMask-Detection-AI.git
cd FaceMask-Detection-AIpy -3.11 -m venv .venvActivate it:
.\.venv\Scripts\Activate.ps1If PowerShell reports that script execution is disabled:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserThen activate again:
.\.venv\Scripts\Activate.ps1You should see:
(.venv) PS C:\...\FaceMask-Detection-AI>
python3.11 -m venv .venv
source .venv/bin/activateUpgrade pip:
python -m pip install --upgrade pipInstall project dependencies:
pip install -r requirements.txtDownload the required model:
python download_model.pyThe application expects the model at:
models/mask_detector.pt
You can also provide your own compatible YOLO model:
python main.py --model path/to/your/best.ptpython main.py --source path/to/image.jpgExample:
python main.py --source test.jpgpython main.py --source path/to/video.mp4Example:
python main.py --source test.mp4Start real-time webcam detection:
python main.py --webcamSave the webcam output:
python main.py --webcam --savepython main.py --conf 0.40Higher values generally produce fewer but more confident detections.
python main.py --imgsz 640python main.py --device cpuIf a compatible CUDA environment is available:
python main.py --device 0python main.py --showpython main.py --source test.mp4 --conf 0.40 --imgsz 640 --device 0 --showDifferent trained models can use different class names and class ordering.
For this reason, the application supports explicit class mapping.
Example:
python main.py \
--model models/best.pt \
--mask-class with_mask \
--no-mask-class without_maskThis helps prevent a dangerous situation where the numerical class index is correct but the application displays the wrong semantic label.
For video and webcam inputs, the application can use ByteTrack to associate detections between frames.
Conceptually:
โโโโโโโโโโโโโโโโ
โ Frame 1 โ
โ โ
โ Person #3 โ
โโโโโโโโฌโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ Frame 2 โ
โ โ
โ Person #3 โ
โโโโโโโโฌโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโ
โ Frame 3 โ
โ โ
โ Person #3 โ
โโโโโโโโโโโโโโโโ
Tracking state is reset when a new input source begins.
The project includes an evaluation script for measuring model performance on a labeled dataset.
Run:
python evaluate.py --model models/best.pt --data dataset/data.yaml --split valThe evaluation reports:
Precision
Recall
mAP@50
mAP@50-95
Results are saved to:
output/metrics.json
Object detection is not adequately represented by ordinary classification accuracy.
A proper detection benchmark should consider:
- Whether the object was detected
- Whether the predicted class was correct
- Whether the bounding box overlaps the ground truth
- Precision
- Recall
- Intersection over Union (IoU)
- mAP at different IoU thresholds
Therefore, this project avoids claiming a performance percentage without an actual benchmark dataset.
Run the automated test suite:
python -m unittest discover -s tests -p "test_*.py"A successful run should report all tests passing.
The project also includes GitHub Actions so tests can automatically run when changes are pushed to the repository.
A training notebook is included:
colab_train_yolov11_mask_detector.ipynb
The notebook can be used as a starting point for training or experimenting with your own face-mask detection dataset.
For reproducible research, record:
- Dataset version
- Training/validation split
- Model checkpoint
- Image size
- Number of epochs
- Confidence threshold
- Evaluation metrics
Detection performance depends heavily on the training dataset.
For better real-world performance, the dataset should contain variation in:
- Lighting
- Camera quality
- Face orientation
- Occlusion
- Mask styles
- Indoor/outdoor environments
- Single and multiple people
- Different distances from the camera
A model should be evaluated on data that was not used for training or tuning.
Do not commit:
.env
API keys
passwords
private datasets
personal data
large generated outputs
unverified model weights
.venv/
Python caches
The repository includes a .gitignore to help keep local/environment-specific files out of version control.
This project uses third-party libraries and model assets.
Important dependencies include:
- Ultralytics
- PyTorch
- OpenCV
- ByteTrack
Please review the applicable licenses before redistributing or using the project commercially.
See:
NOTICE.md
for additional third-party attribution information.
Potential future development includes:
- ๐ Streamlit web interface
- โก FastAPI inference API
- ๐ Real-time analytics dashboard
- ๐ CSV/JSON detection export
- ๐ Configurable alerts
- ๐ง Improved custom-trained models
- ๐ฑ Mobile/web deployment
- ๐ณ Docker support
- โ๏ธ Cloud deployment
- ๐ Advanced performance benchmarking
- ๐ฅ FPS and latency monitoring
Contributions, ideas, bug reports, and improvements are welcome.
git checkout -b feature/my-featureMake your changes, test them:
python -m unittest discover -s tests -p "test_*.py"Commit:
git add .
git commit -m "Add my feature"Push:
git push origin feature/my-featureThen open a Pull Request.
If you find this project useful:
- โญ Star the repository
- ๐ด Fork the project
- ๐ Report issues
- ๐ก Suggest improvements
- ๐ค Contribute
Your support is appreciated!
Computer Vision & AI Project
GitHub: @HadeedJalani
Repository: FaceMask-Detection-AI
Built with Python โข YOLO โข PyTorch โข OpenCV โข ByteTrack
Made by Hadeed Jalani