A simple Convolutional Neural Network (CNN) project to detect whether a person is wearing a mask or not wearing a mask using images. The project is based on a publicly available Kaggle dataset.
code.ipynb→ This is the main notebook.
It contains all steps:- Loading and preprocessing the dataset (
Train,Validation,Test) - Training the CNN model
- Evaluating the model
- Testing single images
- (Optional) Real-time webcam detection
- Loading and preprocessing the dataset (
Users should open this notebook first and run all cells sequentially.
The project uses the Face Mask Detection dataset from Kaggle.
The dataset is structured into three folders:
Train/
Test/
Validation/
Each folder contains two classes:
with_mask– Images of people wearing maskswithout_mask– Images of people not wearing masks
(Download and unzip the dataset into your project directory.)
Face-Mask-Detection/ │ ├─ dataset/ │ ├─ Train/ │ │ ├─ with_mask/ │ │ └─ without_mask/ │ ├─ Validation/ │ │ ├─ with_mask/ │ │ └─ without_mask/ │ └─ Test/ │ ├─ with_mask/ │ └─ without_mask/ │ ├─ model/ │ └─ face_mask_model.h5 │ ├─ train.py ├─ evaluate.py ├─ detect_mask_webcam.py # optional real-time detection └─ README.md
- Python 3.x
- TensorFlow / Keras
- OpenCV
- NumPy
- Matplotlib (optional, for visualizations)
Clone the repo:
git clone https://github.com/yourusername/Face-Mask-Detection.git
cd Face-Mask-DetectionTo run the model:
- Open
code.ipynbin Jupyter/VSCode - Execute all cells in order
- Model trains, evaluates, and saves as
maskDetector.h5 - Test images directly in notebook
For webcam detection (optional):
- Run the webcam section in notebook
- Face detection with OpenCV
- Real-time mask classification
- Green/Red indicators for mask status
- Press 'q' to quit
Input: (256, 256, 3) ├── Conv2D (32 filters, 3×3) → ReLU ├── MaxPooling2D (2×2) ├── Conv2D (64 filters, 3×3) → ReLU ├── MaxPooling2D (2×2) ├── Conv2D (128 filters, 3×3) → ReLU ├── MaxPooling2D (2×2) ├── Flatten ├── Dense (128 units) → ReLU ├── Dense (64 units) → ReLU └── Dense (1 unit) → Sigmoid
- Optimizer: Adam
- Loss Function: Binary Crossentropy
- Metrics: Accuracy
- Epochs: 20
- Batch Size: 32
- Validation Split: 20%
This project is licensed under the MIT License - see the LICENSE file for details.
Dataset: Kaggle - Face Mask Detection
Code: code.ipynb • Model: maskDetector.h5