Skip to content

Detects face masks in real-time from webcam or video using deep learning with TensorFlow/Keras and Computer Vision (OpenCV). Includes model training, image resizing, and live video inference.

Notifications You must be signed in to change notification settings

MukulGupta121190/FaceMaskDetection

Repository files navigation

Face Mask Detection System

A real-time face mask detection system that can detect whether a person is wearing a face mask or not. This project uses deep learning and computer vision techniques to analyze video streams.

Features

  • Real-time face mask detection in video streams
  • High accuracy MobileNetV2-based model
  • Customizable confidence threshold
  • Support for both webcam and video file inputs
  • Visual bounding boxes and labels with confidence scores

How It Works

The project utilizes the following:

  1. Face Detection: The face_detector folder contains the deploy.prototxt and res10_300x300_ssd_iter_140000.caffemodel files. These are used for detecting faces in the input video stream. It's based on the Single Shot Detector (SSD) framework with a ResNet base architecture.

  2. Mask Classification: The train_mask_detector.py script trains a custom Convolutional Neural Network (CNN) model to classify detected faces as either "with_mask" or "without_mask". This trained model is saved as either.model/.h5.

  3. Detection Pipeline: The detect_mask_video.py script combines the face detection and mask classification steps. It processes each frame of the input video, detects faces, classifies them, and overlays the result on the video frame.

Code Files

  • train_mask_detector.py: Script for training the face mask detection model.
  • detect_mask_video.py: Script for detecting face masks in video streams.
  • face_detector/:
    • res10_300x300_ssd_iter_140000.caffemodel: Pre-trained model for face detection.
    • deploy.prototxt: Configuration file for the face detection model.

Technology Stack

  • Python 3.7+
  • OpenCV (for computer vision tasks)
  • TensorFlow/Keras (for deep learning model)
  • MobileNetV2 (as base model)
  • Caffe-based face detector

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Python 3.7 or higher installed
  • pip package manager
  • Git (for cloning the repository)

Installation

  1. Clone this repository:
git clone https://github.com/MukulGupta121190/FaceMaskDetection.git
cd FaceMaskDetection 
  1. Install the required packages:
 # Install dependencies 
  pip install -r requirements.txt 
# Train the model 
  python train_mask_detector.py --dataset dataset/ 
  1. You can also download the Face Detector Model (if not planning to use the one included in repo):
wget https://github.com/chandrikadeb7/Face-Mask-Detection/raw/master/face_detector/deploy.prototxt
wget https://github.com/chandrikadeb7/Face-Mask-Detection/raw/master/face_detector/res10_300x300_ssd_iter_140000.caffemodel
mkdir face_detector
mv deploy.prototxt face_detector/ 
mv res10_300x300_ssd_iter_140000.caffemodel face_detector/ 

Usage

  1. Training the Model (Optional):

If you want to train your own model, ensure you have a dataset of images with and without masks. To train the face mask detector model on your own dataset, run the following script.

python train_mask_detector.py --dataset dataset --model mask_detector.h5

This will train the model and save it as mask_detector.h5

Note: Your dataset should have the following structure

dataset/
    with_mask/
        image1.jpg
        image2.jpg
        ...
    without_mask/
        image1.jpg
        image2.jpg
        ...
  1. Running the Face Mask Detection Model: To detect face masks in a video file, run the below script.
python detect_mask_video.py 

Press 'q' to quit the detection window!

Note: You will need to modify the input path and name according to your video file in the below script

vs = cv2.VideoCapture('/Users/mukulgupta/Downloads/mask_video.mp4') 

Project Structure

FaceMaskDetection/
├── detect_mask_video.py        # Main detection script
├── train_mask_detector.py      # Training script
├── mask_detector.h5            # Pre-trained model
├── face_detector/              # Face detection models
│   ├── deploy.prototxt
│   └── res10_300x300_ssd_iter_140000.caffemodel
├── dataset/                    # Sample training data
│   ├── with_mask/
│   └── without_mask/
├── images                      # Model Prediction results
│   ├── mask_detection/
│   └── no_mask_detection/
├── images_resized              # Resized Prediction results (just for better aesthetic in this .Readme file)
│   ├── mask_detection/
│   └── no_mask_detection/
├── requirements.txt            # Python dependencies
└── README.md                   # This file

Results

Here are some sample results of the mask detection in action:

Detection With Mask

Result 1 Result 2 Result 3 Result 4

Detection Without Mask

Result 1 Result 2 Result 3
Result 4 Result 5 Result 6

Contributions

Contributions to this project are welcome! If you have any ideas for improvements, bug fixes, or new features, feel free to submit a pull request.

Contact

Connect with me:

mukulgupta0991 mukulgupta121190 @mukulgupta121190 mukulgupta121190

About

Detects face masks in real-time from webcam or video using deep learning with TensorFlow/Keras and Computer Vision (OpenCV). Includes model training, image resizing, and live video inference.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages