Skip to content

rppradhan08/face-mask-detection

Repository files navigation

GitHub Repo stars GitHub forks contributors-shield LinkedIn


Face Mask Detector
Real-time Face Mask Detector

Table of Contents

About the Project

The World Health Organization (WHO) report suggests that respiratory droplets are one of the main routes of transmission of the COVID-19 virus. In a medical study, it was proven that surgical masks can prevent up to 95% of viral transmission caused due to respiratory droplets. This project uses Computer-Vision techniques like Face Detection and CNN classification to detect whether a person is wearing a mask in real-time.

Approach

To detect whether the person is wearing a face mask in real-time, the app needs to keep track of individual faces per frame. OpenCV comes with many pre-trained DNN models for facial detection. Once faces are detected with high confidence, they are sent to a CNN classifier that detects face masks.

Steps Involved

  1. Loading images and preparing data for the CNN classifier
  2. Building and training CNN classifier
  3. Evaluating the CNN Classifier
  4. Capturing video frames and detecting faces
  5. Performing predictions and displaying results

Getting Started

Installations

Firstly, execute the below commands in the terminal for setting up the virtual environment and installing packages:

  1. Create a virtual environment
python3 -m venv env
  1. Activate newly created virtual environment env
env/Scripts/activate.bat
  1. Execute the below command to install python packages used in this project
pip install requirement.txt
  1. After installing all dependencies execute below command to run main_app.py.
python main_app.py

Loading images and data preparation for the CNN classifier

To training our CNN classifier, images are present inside the data.zip file. Once unzipped, below will be the folder structure.

    data
     ├── with_mask  - this folder contains images of people with mask
     └── without_mask - this folder contains images of people without the mask

The below bar plot shows that labels are uniformly distributed. As there is a minimal class imbalance, 'accuracy' can be used as an evaluation metric for the classifier.


class_distribution

Once images are loaded, they are resized to a fixed size of 224x224 pixels.


image_grid

The resized images are then normalized and passed through a data augmentation phase so that the model can generalize. Finally, 20% of the data is spared for testing the final model.

Building and training CNN classifier

For building, the CNN classifier architecture transfer learning has been used to reduce the number of epochs to train the model. The MobileNet model was used as its base architecture, due to its high performance and smaller footprint. As the model will be capturing similar abstract features present in the Imagenet dataset, the trainable parameters of the Conv-Maxpool blocks are set to False. The top layers of MobileNet are chopped off and custom fully connected layers are added. On, training the model for 20 epochs below are the loss and Accuracy curves for our train and test data.

After model completion, it is trained for 20 epochs to achieve an accuracy of 97.5% on the train and test data. Below are the accuracy and loss of the trained model.


metric_curves

Evaluating the CNN Classifier

The trained model is evaluated on the test data. The below confusion matrix shows that the model fares pretty well on unseen data.


confusion_metrics

Capturing video frames and detecting faces

For capturing video frames, the VideoCapture module of OpenCV has been used. Then individual frames are passed through an OpenCV pre-trained DNN model for detecting faces present inside a frame. Faces that are above a certain threshold are saved and the rest are discarded. The model returns the pixel coordinates of detected faces. These coordinates are then used to extract faces and form the bounding box.

Performing perdictions and displaying results

The faces captured by the face detector are resized and preprocessed so that they can be fed to the CNN model to classify whether the person is wearing a mask or not. Finally, the bounding and the resulting predictions are displayed on the captured frame.


outcome

Contacts

Socials : Linkedin
E-mail : rppradhan310@gmail.com

About

This application uses OpenCV and CNN for detecting whether the person is face-mask or not.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published