/Face-Mask-Detector-using-MobileNetV2

This is a simple image classification project trained on the top of Keras/Tensorflow API with MobileNetV2 deep neural network architecture having weights considered as pre-trained 'imagenet' weights. The trained model (mask-detector-model.h5) takes the real-time video from webcam as an input and predicts if the face landmarks in Region of Interest (ROI) is 'Mask' or 'No Mask' with real-time on screen accuracy.

Primary LanguageJupyter NotebookApache License 2.0Apache-2.0

Face Mask Detector using MobileNetV2 ๐Ÿ‡ฎ๐Ÿ‡ณ

Table of Content

Demo

Link: https://github.com/ikigai-aa/Face-Mask-Detector-using-MobileNetV2/blob/master/demo%20video.mp4

Overview

This is a simple image classification project trained on the top of Keras/Tensorflow API with MobileNetV2 deep neural network architecture having weights considered as pre-trained 'imagenet' weights. The trained model (mask-detector-model.h5) takes the real-time video from webcam as an input and predicts if the face landmarks in Region of Interest (ROI) is 'Mask' or 'No Mask' with real-time on screen accuracy.

Motivation

Globally, the coronavirus stats says it has more than 16.3M confirmed cases and claimed over 649K lives so far, according to the Johns Hopkins University when I am writing this project repo. As many as 9.41 M people have recovered.

Indiaโ€™s coronavirus cases are increasing in an unimaginable rate and is breaking the record in the highest single-day increase so far every new day.. The countryโ€™s tally rose to 14,35,453 and the toll stood at 32,771. India is now the third worst-affected country by the pandemic and has overtaken Italy, according to Johns Hopkins University.

The World Health Organization said that new information showed that protective masks could be a barrier for potentially infectious droplets. The coronavirus primarily spreads through the transmission of respiratory droplets from infected people.On 5th day of June changed its guidelines about the use of protective face masks in public, saying that they must be worn at all places where physical distancing is not possible. The global health body had said in April that there was not enough evidence to show that healthy people should wear masks to shield themselves from the coronavirus.

Link: https://twitter.com/i/status/1268986094042992640

WHO also said that high-risk groups should wear medical grade masks in cases where physical distancing is not possible.Several countries, including India, have made wearing masks in public compulsory. In many states, people have been fined for not wearing masks. Maintaining hygiene and using protective equipment has become even more important ahead of the reopening of religious places, malls and restaurants in India from next week.

This motivated me to create a the COVID-19 Mask Detector with some of my ML/DL skills and making it such accurate that it could potentially be used to help ensure your safety and the safety of others (Leaving it on to the medical professionals to decide on, implement in public places).

Technical Aspect

In order to train a Face Mask Detector, we need to break our project into two distinct phases, each with its own respective sub-steps:

Training: Here weโ€™ll focus on loading our face mask detection dataset from disk, training a model (using Keras/TensorFlow) on this dataset, and then serializing the face mask detector to disk

Deployment: Once the face mask detector is trained, we can then move on to loading the mask detector, performing face detection, and then classifying each face as with_mask or without_mask

\

Dataset Resource:

Link: https://drive.google.com/drive/folders/1FHPJRCab-cyLq8IVz83LkU71gOc7gTS8?usp=sharing

Project structure

โ”œโ”€โ”€ dataset
โ”‚   โ”œโ”€โ”€ with_mask [690 entries]
โ”‚   โ””โ”€โ”€ without_mask [686 entries]
โ”œโ”€โ”€ examples
โ”‚   โ”œโ”€โ”€ example_01.png
โ”‚   โ”œโ”€โ”€ example_02.png
โ”‚   โ””โ”€โ”€ example_03.png
โ”œโ”€โ”€ face_detector
โ”‚   โ”œโ”€โ”€ deploy.prototxt
โ”‚   โ””โ”€โ”€ res10_300x300_ssd_iter_140000.caffemodel
โ”œโ”€โ”€ detect_mask_image.py
โ”œโ”€โ”€ detect_mask_video.py
โ”œโ”€โ”€ mask_detector.model
โ”œโ”€โ”€ evaluation.png
โ””โ”€โ”€ Data Augmentation and Model Training.ipynb
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ mask-detector-model.model

Important Python Scripts:

  1. Data Augmentation and Preprocessing.ipynb: In this notebook Accepts our dataset is taken as input and fine-tuning is donw with MobileNetV2 DNN architecture upon it to create our mask-detector-model.model. A training history evaluation.png containing accuracy/loss curves is also produced for better visualization of Model Evaluation through a plot.Some important processes which we performed here:

a. Data augmentation b. Loading the MobilNetV2 classifier (we will fine-tune this model with pre-trained ImageNet weights) c. Building a new fully-connected (FC) head d. Pre-processing e. Loading image data

Libraries Significance:

scikit-learn: for binarizing class labels, segmenting our dataset, and printing a classification report. imutils: To find and list images in our dataset. matplotlib: To plot our training curves.

  1. detect_mask_from_webcam.py: Using your webcam, this script applies face mask detection to every frame in the stream using webcom to read the real-time video.

Some command line arguments in this script include:

--image: The path to the input image containing faces for inference
--face: The path to the face detector model directory (we need to localize faces prior to classifying them)
--model: The path to the face mask detector model that we trained earlier in this tutorial
--confidence: An optional probability threshold can be set to override 50% to filter weak face detections

Installation

The Code is written in Python 3.7. If you don't have Python installed you can find it here. If you are using a lower version of Python you can upgrade using the pip package, ensuring you have the latest version of pip. To install the required packages and libraries, run this command in the project directory after cloning the repository:

## Run
> STEP 1
After unzipping the forked zip file of this project into your local machine, type the follwing command from the directory where you saved the project files in the command prompt: 
pip install -r requirements.txt

This will install thw following libraries:

absl-py==0.9.0
astunparse==1.6.3
cachetools==4.1.1
certifi==2020.6.20
chardet==3.0.4
cycler==0.10.0
gast==0.3.3
google-auth==1.19.2
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.30.0
h5py==2.10.0
idna==2.10
importlib-metadata==1.7.0
imutils==0.5.3
joblib==0.16.0
Keras-Preprocessing==1.1.2
kiwisolver==1.2.0
Markdown==3.2.2
matplotlib==3.3.0
numpy==1.19.1
oauthlib==3.1.0
opencv-python==4.3.0.36
opt-einsum==3.3.0
Pillow==7.2.0
protobuf==3.12.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyparsing==2.4.7
python-dateutil==2.8.1
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scikit-learn==0.23.1
scipy==1.4.1
six==1.15.0
sklearn==0.0
tensorboard==2.2.2
tensorboard-plugin-wit==1.7.0
tensorflow==2.2.0
tensorflow-estimator==2.2.0
termcolor==1.1.0
threadpoolctl==2.1.0
urllib3==1.25.10


> STEP 2
Open Jupyter Notebook and run Data Augmentation and Preprocessing.ipynb in order to train your custom dataset within your loacl machine and preprocess the images meanwhile.

> STEP 3
Run detect_mask_from_webcam.py from the same directory of your project folder in the command prompt in order to test the detector in real- time using the webcam.

Results/Classification Report

              precision    recall  f1-score   support

   with_mask       0.97      1.00      0.99       138
without_mask       1.00      0.97      0.99       138

    accuracy                           0.99       276
   macro avg       0.99      0.99      0.99       276
weighted avg       0.99      0.99      0.99       276

Accuracy/Loss Plot

\

...

To Do

  1. This approach reduces our computer vision pipeline to a single step โ€” rather than applying face detection and then our face mask detector model, all we need to do is apply the object detector to give us bounding boxes for people both with_mask and without_mask in a single forward pass of the network.

  2. An integration of this project to a web app/android app.

Bug / Feature Request

If you find a bug (the website couldn't handle the query and / or gave undesired results), kindly open an issue here by including your search query and the expected result.

If you'd like to request a new function, feel free to do so by opening an issue here. Please include sample queries and their corresponding results.

Technologies Used

Team

Ashish Agarwal

LinkedIn Profile: https://www.linkedin.com/in/ashish-agarwal-502203113/

License

Apache license

Copyright 2020 Ashish Agarwal

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Resources

  1. https://www.who.int/publications/i/item/advice-on-the-use-of-masks-in-the-community-during-home-care-and-in-healthcare-settings-in-the-context-of-the-novel-coronavirus-(2019-ncov)-outbreak
  2. https://www.pyimagesearch.com/2018/09/10/keras-tutorial-how-to-get-started-with-keras-deep-learning-and-python/
  3. http://www.image-net.org/
  4. https://arxiv.org/abs/1801.04381