This project is an updated version of a team project I completed in 2021 (published in IEEE conference proceedings of ICACRS 2022), which previously only classified two categories: "with mask" and "without mask". This enhanced implementation now distinguishes a third category - "mask worn incorrectly", making it more comprehensive for real-world applications.
- Real-time face mask detection through webcam
- Image upload for mask detection
- Responsive web interface
- High accuracy classification (97.83% on test data)
The model achieves excellent performance across all three classes:
Classification Report:
precision recall f1-score support
mask_weared_incorrect 0.99 0.99 0.99 598
with_mask 0.96 0.98 0.97 598
without_mask 0.99 0.96 0.98 598
accuracy 0.98 1794
macro avg 0.98 0.98 0.98 1794
weighted avg 0.98 0.98 0.98 1794
Final validation accuracy: 97.83% Final validation loss: 0.0613
- Python 3.9
- TensorFlow 2.12 with Metal support for Mac
- MobileNetV2 for the base convolutional neural network
- OpenCV for computer vision and face detection
- Flask for web application framework
- Bootstrap 5 for frontend styling
- JavaScript for interactive frontend functionality
- Recommended: macOS with Apple Silicon (M1/M2/M3)
- Python: 3.9.x
- At least 4GB RAM
- Webcam (for live detection)
git clone https://github.com/421pvv/face-mask-detection-using-deep-learning.git
cd face-mask-detection-using-deep-learning# Create a new environment with Python 3.9
conda create -n mask-detection python=3.9
conda activate mask-detectionpip install -r requirements.txtNote for non-Mac users: If you're not using a Mac with Apple Silicon, you should replace
tensorflow-macosandtensorflow-metalin the requirements.txt with standardtensorflow:pip uninstall tensorflow-macos tensorflow-metal pip install tensorflow==2.12.0
face-mask-detection/
├── data/
│ └── processed/ # Place your dataset here
│ └── mask_dataset/ # Dataset directory
│ ├── with_mask/
│ ├── without_mask/
│ └── mask_weared_incorrect/
├── models/
│ ├── face_detector/ # Face detection model files
│ └── face_mask_detection_model/ # Trained model for inference
├── notebooks/
│ └── model_training.ipynb # Model training notebook
├── static/
│ ├── css/
│ ├── js/
│ └── images/ # Store visualization images here
│ ├── training_history.png
│ └── confusion_matrix.png
├── templates/ # HTML templates
├── app.py # Flask application
├── detection_utils.py # Utility functions for detection
└── requirements.txt
The dataset used for training is from Kaggle: Face Mask Detection Dataset
- Download the dataset from the Kaggle link
- Extract the dataset and organize it into three folders:
data/processed/mask_dataset/with_mask/data/processed/mask_dataset/without_mask/data/processed/mask_dataset/mask_weared_incorrect/
If you want to train the model yourself:
- Ensure you have the dataset organized as described above
- Open the notebook
notebooks/model_training.ipynb - Run the cells in sequence
- The trained model will be saved to the
models/directory
The model uses MobileNetV2 architecture with transfer learning, which provides an excellent balance between accuracy and performance for deployment.
To start the web application:
python app.pyThis will start the Flask development server, and you can access the application at http://localhost:3000.
- Home Page: Provides options for live detection or image upload
- Live Detection: Accesses your webcam for real-time mask detection
- Image Upload: Allows you to upload an image for mask detection
- If you encounter webcam access issues, make sure to grant camera permissions to your terminal/IDE
- For M1/M2/M3 Macs, the application uses a Haar Cascade face detector as a fallback if the DNN detector fails
- If you encounter CUDA/GPU errors on Windows/Linux, try disabling GPU acceleration by setting this environment variable:
export CUDA_VISIBLE_DEVICES=-1 # Linux/Mac set CUDA_VISIBLE_DEVICES=-1 # Windows
- MobileNetV2 Paper
- Face Mask Detection Dataset
- Our Previous Work (2022) - Conference paper from ICACRS 2022

