A real-time face mask detection system using a convolutional neural network (CNN). This project detects whether a person is wearing a mask or not from live video feed or images.
- Real-Time Detection: Detects faces and classifies them as "With Mask" or "Without Mask".
- Data Augmentation: Enhances training dataset with transformations like rotation, zoom, and flipping.
- User-Friendly Interface: Visualizes detection results with bounding boxes and color-coded labels.
- Customizable: Easy to modify for different datasets or detection tasks.
Face-Mask-Detector/
├── train.py # Script to train the mask detection model
├── test.py # Script to test the model with live video feed
├── model-010.h5 # Pre-trained model file
├── train/ # Training dataset directory
├── test/ # Testing dataset directory
├── README.md # Project documentation
Ensure you have the following installed:
- Python 3.6+
- Keras
- TensorFlow
- OpenCV
- Numpy
Install dependencies with:
pip install -r requirements.txt
-
Clone the Repository:
git clone https://github.com/yourusername/Face-Mask-Detector.git cd Face-Mask-Detector
-
Prepare Dataset:
- Place your training dataset in the
train/
folder. - Place your testing dataset in the
test/
folder. - The dataset should have two subdirectories:
mask/
andno_mask/
.
- Place your training dataset in the
-
Train the Model: Run the
train.py
script to train the model:python train.py
-
Test the Model: Use the
test.py
script to run real-time detection:python test.py
The CNN architecture includes:
- Two Convolutional Layers with 100 filters each.
- MaxPooling layers to reduce spatial dimensions.
- Dropout for regularization.
- Fully connected Dense layers for classification.
Loss function: binary_crossentropy
Optimizer: adam
Metrics: accuracy
The training data is augmented with:
- Rotation: up to ±40 degrees.
- Zoom: range of 20%.
- Shifts: Horizontal and vertical by 20%.
- Flipping: Horizontal flips.
This improves model robustness and prevents overfitting.
The model trains for 10 epochs with ImageDataGenerator
for augmentation.
Example command:
python train.py
- Run the detection system:
python test.py
- Press
Esc
to exit the live detection window.
Your dataset structure should look like:
train/
├── mask/
│ ├── image1.jpg
│ ├── image2.jpg
│ ...
├── no_mask/
│ ├── image1.jpg
│ ├── image2.jpg
│ ...
test/
├── mask/
├── no_mask/
Contributions are welcome! Feel free to submit a PR or open an issue.
This project is licensed under the MIT License.
For any queries or issues, reach out to:
- Email: vikas.sharma005@protonmail.com
- GitHub: vikasharma005