This project can detect whether a person is wearing a face mask or not using a webcam or image.
It uses Deep Learning (MobileNetV2) and OpenCV to work in real time.
The program:
- Looks at a person’s face through a webcam or an image.
- Detects if the person’s face has a mask or no mask.
- Shows the result on the screen with a label and box around the face.
-
Dataset — Around 4000 images of people with and without masks were collected.
-
Model Training —
- The images are split into 80% training and 20% testing.
- A MobileNetV2 deep learning model (pre-trained on ImageNet) is used.
- Only the last few layers are trained to detect masks.
-
Saved Model — After training, a file called
mask_detector_model.h5is created. -
Detection —
- The webcam or an image is given to the model.
- It checks the face area and predicts “Mask” or “No Mask”.
Make sure you have Python installed, then open Command Prompt in your project folder and type:
pip install -r requirements.txtIf you want to train it yourself:
python train_model.pyThis will create a new model file — mask_detector_model.h5.
After the model is ready:
python mask_detection_main.pyIt will open your webcam and start detecting faces with or without masks.
- Python 3
- TensorFlow / Keras
- OpenCV
- MobileNetV2 (Pretrained CNN)
- NumPy & Matplotlib
✅ Works in real time using a webcam ✅ Detects faces accurately ✅ Trained on a custom dataset ✅ Can easily be improved with more mask types and colors
- Add third category → “Improperly worn mask”
- Improve accuracy with more training images
- Deploy the model as a web or mobile app
Sameet Patro Undergraduate Student — IIIT Sonepat