A real-time face mask detection system that can identify whether a person is wearing a mask, not wearing a mask, or wearing a mask incorrectly using computer vision and deep learning.
- ๐ Real-time Detection: Works with webcam for live mask detection
- ๐ฅ Multiple Face Support: Detects and classifies multiple faces simultaneously
- ๐ข๐ก๐ด Three-Class Classification:
- ๐ข
with_mask: Person is wearing a mask correctly - ๐ด
without_mask: Person is not wearing a mask - ๐ก
mask_weared_incorrect: Person is wearing a mask incorrectly
- ๐ข
- ๐ High Accuracy: CNN-based model with data augmentation
- ๐ฅ๏ธ User-Friendly Interface: Real-time statistics and visual feedback
- ๐ธ Screenshot Capability: Save detection results as images
- ๐ Python 3.7 or higher
- ๐ฅ Webcam (for real-time detection)
- ๐พ Sufficient RAM (4GB+ recommended for training)
- โก GPU (optional, for faster training)
-
Clone or download this repository
git clone https://github.com/HexCrystal69/AirAware.git
-
Install dependencies:
pip install -r requirements.txt
-
Verify installation:
python -c "import tensorflow, cv2, numpy; print('All dependencies installed successfully!')"
The system expects the following dataset structure:
data/
โโโ images/
โ โโโ maksssksksss0.png
โ โโโ maksssksksss1.png
โ โโโ ... (all PNG images)
โโโ annotations/
โโโ maksssksksss0.xml
โโโ maksssksksss1.xml
โโโ ... (corresponding XML annotations)
To train the mask detection model:
python train.pyThis will:
- ๐ฅ Load and preprocess the dataset from
data/folder - ๐๏ธ Create a CNN model with data augmentation
- ๐ Train the model with early stopping and learning rate scheduling
- ๐พ Save the best model as
mask_detector_model.h5 - ๐ผ๏ธ Generate training plots (
training_history.png) - ๐ Create a confusion matrix (
confusion_matrix.png) - ๐ Print evaluation metrics
Training Parameters:
- โณ Epochs: 50 (with early stopping)
- ๐ฆ Batch Size: 32
- ๐ฆ Learning Rate: 0.001 (with reduction on plateau)
- ๐ Data Augmentation: Rotation, shifts, flips, zoom, brightness
To run real-time mask detection with your webcam:
python run_detection.pyAdvanced Options:
# Use a different model file
python run_detection.py --model my_custom_model.h5
# Use a different camera (if you have multiple cameras)
python run_detection.py --camera 1
# Adjust confidence threshold
python run_detection.py --confidence 0.8
# Hide FPS and statistics display
python run_detection.py --no-fpsReal-time Controls:
- ๐ q: Quit detection
- ๐พ s: Save screenshot
- โน๏ธ h: Show help
To test the system on a single image:
python src/mask_classifier.pyThen enter the path to your image when prompted.
The CNN model consists of:
- ๐งฑ 4 Convolutional Blocks with increasing filters (32โ64โ128โ256)
- ๐งช Batch Normalization for stable training
- ๐ MaxPooling for dimension reduction
- ๐ก๏ธ Dropout for regularization
- ๐ข Dense Layers (512โ256โ3) for classification
- ๐งฎ Softmax activation for multi-class output
The system typically achieves:
- ๐ Training Accuracy: 95%+
- ๐ฅ Validation Accuracy: 90%+
- โก Real-time FPS: 15-30 FPS (depending on hardware)
- ๐จโ๐ฉโ๐งโ๐ฆ Face Detection: Works with multiple faces simultaneously
Face Mask Detection System/
โโโ data/ # Dataset folder
โ โโโ images/ # PNG images
โ โโโ annotations/ # XML annotations
โโโ src/ # Source code
โ โโโ realtime_detection.py # Real-time detection
โ โโโ mask_classifier.py # Image-based detection
โ โโโ face_detector.py # Face detection utilities
โ โโโ utils.py # Data loading and utilities
โโโ train.py # Complete training script
โโโ run_detection.py # Simple detection launcher
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
-
โ "Model file not found"
- Make sure you've trained the model first using
python train.py
- Make sure you've trained the model first using
-
๐ท "Could not open camera"
- Check if your webcam is connected and not being used by another application
- Try different camera indices:
--camera 1,--camera 2, etc.
-
๐ผ๏ธ "No images found in dataset"
- Ensure your dataset is in the correct structure under
data/folder - Check that images are in PNG format
- Ensure your dataset is in the correct structure under
-
๐ข Low FPS
- Close other applications using the camera
- Reduce image resolution in the code
- Use a GPU for faster inference
-
๐ฏ Poor detection accuracy
- Retrain the model with more data
- Adjust confidence threshold
- Ensure good lighting conditions
- ๐ For better accuracy: Use good lighting and face the camera directly
- ๐ For faster processing: Close unnecessary applications
- ๐ For multiple faces: Ensure all faces are clearly visible
- ๐ฅ๏ธ For training: Use GPU acceleration if available
To add new mask types, modify src/utils.py:
CLASSES = ['with_mask', 'without_mask', 'mask_weared_incorrect', 'new_mask_type']Modify the create_model() function in src/train_model.py to change the CNN architecture.
Adjust face detection parameters in src/face_detector.py:
faces = detect_faces(frame, scaleFactor=1.1, minNeighbors=5)Feel free to contribute to this project by:
- ๐ Reporting bugs
- ๐ก Suggesting new features
- ๐ Improving documentation
- โก Optimizing performance
This project is open source and available under the MIT License.
- ๐ Dataset: Face Mask Detection Dataset
- ๐๏ธ Face Detection: OpenCV Haar Cascades
- ๐ง Deep Learning Framework: TensorFlow/Keras
- ๐ผ๏ธ Computer Vision: OpenCV