Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ˜ท FaceMask Detection AI

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.

Python YOLO OpenCV PyTorch ByteTrack

GitHub stars GitHub forks GitHub issues GitHub last commit


๐Ÿ“Œ Overview

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.


โœจ Key Features

๐ŸŽฏ YOLO-Based Detection

Uses a YOLO object detection model to identify face-mask-related classes in visual input.

๐Ÿ“ท Real-Time Webcam Detection

Run the detector directly against your webcam for real-time monitoring.

python main.py --webcam

๐ŸŽฅ Video Processing

Process existing videos and generate annotated detection output.

python main.py --source path/to/video.mp4

๐Ÿ›ฐ๏ธ ByteTrack Tracking

For 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.

๐Ÿ“Š Live Statistics

The application maintains live detection/tracking information instead of simply accumulating detections from previous frames.

โš™๏ธ Flexible Inference

Configure:

  • Detection confidence
  • Image size
  • CPU/GPU device
  • Model checkpoint
  • Input source
  • Display/output behavior

๐Ÿ“ˆ Evaluation

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.


๐Ÿง  Detection Pipeline

                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚       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     โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Tech Stack

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

๐Ÿ“‚ Project Structure

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.


๐Ÿš€ Installation

1. Clone the Repository

git clone https://github.com/HadeedJalani/FaceMask-Detection-AI.git
cd FaceMask-Detection-AI

2. Create a Virtual Environment

Windows PowerShell

py -3.11 -m venv .venv

Activate it:

.\.venv\Scripts\Activate.ps1

If PowerShell reports that script execution is disabled:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Then activate again:

.\.venv\Scripts\Activate.ps1

You should see:

(.venv) PS C:\...\FaceMask-Detection-AI>

macOS / Linux

python3.11 -m venv .venv
source .venv/bin/activate

๐Ÿ“ฆ Install Dependencies

Upgrade pip:

python -m pip install --upgrade pip

Install project dependencies:

pip install -r requirements.txt

๐Ÿค– Model Setup

Download the required model:

python download_model.py

The 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.pt

โ–ถ๏ธ Usage

๐Ÿ–ผ๏ธ Image Detection

python main.py --source path/to/image.jpg

Example:

python main.py --source test.jpg

๐ŸŽฅ Video Detection

python main.py --source path/to/video.mp4

Example:

python main.py --source test.mp4

๐Ÿ“ท Webcam Detection

Start real-time webcam detection:

python main.py --webcam

Save the webcam output:

python main.py --webcam --save

โš™๏ธ Configuration

Confidence Threshold

python main.py --conf 0.40

Higher values generally produce fewer but more confident detections.


Image Size

python main.py --imgsz 640

CPU Inference

python main.py --device cpu

GPU Inference

If a compatible CUDA environment is available:

python main.py --device 0

Display Results

python main.py --show

Combined Example

python main.py --source test.mp4 --conf 0.40 --imgsz 640 --device 0 --show

๐Ÿท๏ธ Class Mapping

Different 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_mask

This helps prevent a dangerous situation where the numerical class index is correct but the application displays the wrong semantic label.


๐Ÿ›ฐ๏ธ Tracking with ByteTrack

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.


๐Ÿ“Š Model Evaluation

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 val

The evaluation reports:

Precision
Recall
mAP@50
mAP@50-95

Results are saved to:

output/metrics.json

Why mAP Instead of Simple Accuracy?

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.


๐Ÿงช Testing

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.


๐Ÿ““ Model Training

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

๐Ÿ“ˆ Improving Detection Accuracy

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.


๐Ÿ” Security & Repository Hygiene

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.


๐Ÿ“œ Third-Party Software

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.


๐Ÿ”ฎ Future Improvements

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

๐Ÿค Contributing

Contributions, ideas, bug reports, and improvements are welcome.

Recommended workflow

git checkout -b feature/my-feature

Make 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-feature

Then open a Pull Request.


โญ Support the Project

If you find this project useful:

  • โญ Star the repository
  • ๐Ÿด Fork the project
  • ๐Ÿ› Report issues
  • ๐Ÿ’ก Suggest improvements
  • ๐Ÿค Contribute

Your support is appreciated!


๐Ÿ‘จโ€๐Ÿ’ป Author

Hadeed Jalani

Computer Vision & AI Project

GitHub: @HadeedJalani

Repository: FaceMask-Detection-AI


Built with Python โ€ข YOLO โ€ข PyTorch โ€ข OpenCV โ€ข ByteTrack

Made by Hadeed Jalani

About

Real-time face mask detection using YOLO, OpenCV, PyTorch and ByteTrack, with image/video/webcam inference, multi-object tracking, evaluation metrics, and automated testing.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages