You are on page 1of 19

Real-Time Face Mask

Detection
Submitted in partial fulfillment of the requirements of the
degree
BACHELOR OF ENGINEERING IN COMPUTER
ENGINEERING

By

Lalit Vinde Roll No - 72


Nikita Dubey Roll No - 66
Neha Sargar Roll No - 75

Supervisor
Prof. Ranjita Asati

Department of Computer Engineering

Atharva College of Engineering

Malad -( W ) , Mumbai - 400 095 University of


Mumbai
(AY 2020-21)

1
CERTIFICATE

This is to certify that the Mini Project entitled “ Real-Time Face Mask

Detection ” is a bonafide work of Nikita Dubey (Roll no. 66), Lalit Vinde (Roll

no. 72), and Neha Sargar (Roll no. 75) submitted to the University of Mumbai

in partial fulfillment of the requirement for the award of the degree of “Bachelor

of Engineering” in “Computer Engineering”.

Prof. Ranjita Asati


Supervisor

Prof. Suvarna Pansambal Prof. S. Kallurkar


Head of Department Principal

2
Mini Project Approval

This Mini Project entitled “Real-Time Face Mask Detection” by


Lalit Vinde (Roll no. 72), Nikita Dubey (Roll no. 66) and
Neha Sargar (Roll no. 75) is approved for the degree of BE
in Computer Engineering.

Examiners

1………………………………………
(Internal Examiner Name & Sign)

2…………………………………………
(External Examiner name & Sign)

Date :

Location: Malad -(West), Mumbai


Contents

1. Acknowledgments 5

2. Abstract 6

3. Introduction 6

4. List of figures 7

5. Problem Statement 8

6. Literature survey 8

7. Flowchart 11

8. Architecture and Process Design 14

9. Details of Hardware & Software 16

10. Results and analysis 17

11. Conclusion 19

12. References 19

4
Acknowledgments

We would like to express a deep sense of thanks and gratitude to our project
guide prof. Ranjita Asati for constantly guiding us throughout this project. Her
constructive advice and constant motivation have been responsible for the successful
completion of this project.
We have been greatly benefitted from your valuable guidance, encouragement,
and kind of guidance provided to us throughout this project which often helped us to
develop the project successfully.

Submitted By,
Nikita Dubey, Roll No.: 66
Lalit Vinde, Roll No.: 72
Neha Sargar, Roll No.: 75
Under the guidance of
Ranjita Asati

5
2. Abstract

Changes in the lifestyle of everyone around the world. In those changes wearing
a mask has been very vital to every individual. Detection of people who are not
wearing masks is a challenge due to the Outbreak of the Coronavirus pandemic
which has created a large number of populations. This particular project has a
technique of detecting people’s faces and segregating them into two classes: the
individuals with masks in addition to people without masks is done together with the
help of associated image processing and deep learning. With the help of this project,
a person who is intended to monitor the people can be seated in a remote area and
still can monitor efficiently and give instructions accordingly. Various libraries of
python such as OpenCV, Tensor flow, and Keras, Gtts, Os, flask are being used. In
Deep Learning Convolution Neural Networks is a class of Deep Neural Networks
that is used to train the models used for this project.

Keywords:- Mask Detection, Deep Neural Network, Flask, OpenCV, Pyautogui,


keyboard, HTML, CSS, Bootstrap4, Gtts

3. Introduction

The ongoing crisis arises a severe need for protection mechanisms, face masks
being the primary one. Studies have proved that wearing a face mask significantly
reduces the risk of viral transmission as well as provides a sense of protection. In
such a condition, computers come to our rescue. In this project, with the help of a
graphic user interface using a flask, an application is created. Which will scan each
face using a webcam and voice assistant to give the output. A pre-trained model is
proposed to automate the process of identifying people who are not wearing masks. 
The images are trained on a Teachable machine https://teachablemachinewith
google.com/ whose code is getting exported and the result is usually returned in a
dialog of "Plz wear a mask"

To perform this project you will find a basic need of :


1. Web camera
2. Speaker

6
4. List of Figures

List of Figures Title Page Number

1 Overview of the Workflow of ML 9

2 Phases of face detection 10

3 Flow chart of creating a neural model 11


and dataset normalization

4 Flow chart of train neural model 12

5 Flow chart to do prediction from the 13


trained model

6 1st HTML page 17

6.1 1st HTML page 17

7 2nd HTML page with mask 18

7.1 2nd HTML page without a mask 18

7
5. Problem Statement :

● Our goal is to train a custom deep learning model to detect whether a person is
or is not wearing a mask using OpenCV, TensorFlow, Keras, voice assistant,
and Gui.

● The proposed model is trained and tested on known and then on unknown data
for maximum accuracy. The project will scan each face and give us the output
by using a voice assistant.

6. Literature Survey :

● ‘Workflow Of Machine Learning’ by Ayush Pant, Published in Towards Data


Science, Jan 11, 2019

It provides a machine learning project workflow, which involves all of the


steps necessary to create a proper machine learning project from the ground
up.
Even a beginner can go through the fundamentals of data pre-processing, data
cleaning, feature discovery, and feature engineering and demonstrate how they
affect Machine Learning Model Efficiency. It also goes through a few of the
pre-modeling steps that can make the modeling process go more smoothly.
Python Libraries that would be needed to achieve the task are:
1 . Numpy
2. Pandas
3. Sci-kit Learn
4. Matplotlib

Fig. 1. Overview of the Workflow of ML

8
Understanding the machine learning workflow
We all can define the machine learning workflow in 3 stages.
1. Gathering data
2. Data pre-processing
3. Researching the model that will be best for the type of data
4. Training and testing the model
5. Evaluation

● Real-time Face Recognition by Marcelo Rovai, Published in Towards Data


Science, Mar 12, 2018
Learned how the face is recognized from real-time video streams and images
and got the idea of how to build a real-time face mask detector.
We learned that there are 3 Phases to create a complete project on Face
Recognition, we must work on 3 very distinct phases:
1. Face Detection and Data Gathering
2. Train the Recognizer
3. Face Recognition

Fig 2. Phases of face detection

9
7. Flowchart :

Fig 3. Flow chart of creating a neural model and dataset normalization

10
Fig 4. Flow chart of train neural model

11
Fig 5. Flow chart to do prediction from the trained model

12
8. Algorithm and Process Design:

Algorithm to do some manipulation on the dataset before serving to the


model:

1. Collect the images of faces with mask and without a mask in RGB format

2. Label these images as with mask or without mask whichever class they belong
to.

3. Store the image in a form of a 3-dimensional array/tensor,


where,
● 1st dimension represents the number of channels we have 3 channels
as we have 3 color Red(1st channel), Green(2nd channel), and Blue(3rd
channel)
● 2nd dimension represents the number of row in a channel
● 3rd dimension represents the number of column in a channel

and combined these three dimensions identify the color intensity value at a
particular pixel,

For example, we have an image 3D array/tensor named as imgArr,


if we want to access the color blue intensity value of the pixel,
row number 55 and column number 34 then the query will be:
imgArr [2] [55] [34]
where,
2 is 2nd channel i.e blue
55 is row
34 is column

4. resize all of those images into 244*244* 3-pixel image


where,
3 represent the number of channels Red, Green, and Blue,
244*244 represent the number of pixel in each channel
13
so there are a total of 244*244=59536 pixels in each channel and each pixel
have its corresponding R, G, B color intensity value ranging from 0 to 255

5. After resizing we need to do some standardization of data, that process is


known as normalization so in normalization we shrink the array/tensor color
intensity values to a range between 0 to 1 without affecting its actual
representation, so to do so we use simple mathematic as we know that all
color intensity values of Red, Green, and Blue are in the range of 0 to 255 so,
we simply divide each value of tensor/array by 255 so it gets ended up with
range within 0 to 1,

Example:
one pixel having R, G, B value as 255, 127, 0 respectively
if we divide those intensity value by 255 we get
R = 255/255 = 1
G = 127/255 = 0.498 = 0.5
B = 0/255 = 0

6. And that’s it now we can feed it to the neural network

Algorithm to create a neural network model:

1. As we resize our image into 244*244*3 we must have 244*244*3 neuron at


the input layer i.e total of 178608 neuron

2. We have to classify the image whether it is with a mask or without a mask


so it is and classification problem and we have only two class one for with
mask and another for without mask, so we have 2 neurons at the output layer

3. and have some additional hidden layers in between to recognize the pattern
from the image.

4. Now setup the following hyperparameter


learning ratio = 0.001
batch size = 16
number of epoch = 50

5. now our model is ready to train

Algorithm to train a neural network model:

1. As we successfully created a model and all our images are well labeled, we
are ready to train our model

2. Start a new epoch

14
3. as our batch size is 6 images for we start feeding the data to the neural
network in a batch of 16.

4. After feeding all 16 images of one batch the model will yield a prediction, At
the time of training, the model doesn’t know the pattern in our images so it
will predict so badly.

5. So if the model prediction doest match with our label


(with mask/without mask) then we find the loss of the prediction and do
backpropagation with the Gradient descent optimization algorithm. the main
goal of the gradient descent algorithm is to find the local minima in the loss
function,
which is indirectly proportional to the prediction.

Gradient descent algorithm:

● We find the partial derivation of all weights and biases which get
encountered while backtracking the path and make use of the chain rule
● Then negate the sign of the slope(partial derivation) and multiply it
with the learning ratio in our case it is 0.001
● and then it gets added to the weight

6. We repeat steps 3 and 4 till the whole data set gets fed to the model.

7. We set the number of epochs as 50, so go to step 2 till 50 echo gets completed.

8. Now our model is ready to do prediction.

Algorithm to do prediction:

1. Load our trained model


2. Turn on the webcam
3. Take an image in a 3 dimension format
4. Resize it to the 244*244*3
5. Give it to the trained model to give a prediction value
6. The model with giving the production value for both with masks class and
without mask class
7. If the prediction value of without mask class is greater than with mask class
then give the warning by saying “Please wear the mask”.
8. Repeat the step from 2 to 7 till the user presses the “Exit” button

15
9. Details of Hardware & Software:

● Hardware:
This project uses the internal web camera to capture the input from the user,
therefore, there are specific hardware requirements which are as follows
○ A computer with Webcam access and Speaker: The application requires
access to the webcam to monitor the user and a speaker to give the warning
to the users.
○ 8GB or more RAM: Since this application uses webcam and processes
images continuously it is preferable that enough RAM is available for
smooth and fast processing and to avoid lagging.

● Software:
○ Numpy - 1.19.2, opencv-python == 4.0.1 and Pillow == 8.0.1:
For making image processing efficient and less complex.
○ Flask-1.1.2: Main back-end framework.
○ Threading: Inbuilt Python module for enabling multithreading
○ Tensorflow: we will implement the Python script to train a face mask
detector on our dataset using TensorFlow.
○ OpenCV: OpenCV is an open-source library for computer vision and image
processing. It representing the image in the form of an array where each cell
represents the color intensity value.
○ Numpy - Python library used for working with arrays. It also has functions
for working in the domain of linear algebra, Fourier transform, and matrices.
○ OS: The OS module in Python provides functions for interacting with the
operating system. 
○ Gtts: a Python library and CLI tool to interface with Google Translate’s
text-to-speech API.

16
10. Results and analysis

Fig 6. 1st HTML page

Fig 6.1. 1st HTML page

17
Fig 7. 2nd HTML page with mask

Fig 7.1. 2nd HTML page without a mask

18
11. Conclusion

This project has implemented real-time detection of masks through image training
with a deep neural network model and OpenCV, TensorFlow, Keras. We have
developed a web
application using Flask as a back-end server and HTML, CSS, and Bootstrap4 as
front-end
technologies. We have used OpenCV, TensorFlow, and Keras to train the model in
Python. The application enables the user to detect their face and predict if they are
wearing the mask or not.
This model has been trained various times to get an accurate prediction. Through
this project, we had come across new techniques like the Training model using
Tensorflow, Keras, Teachable machine and learned web development technologies
like Flask and implementation of Python as the main programming language.
This specific project was created on the basics of our basic learning of deep
learning. We want to extend this project by training this project with different models
present in deep learning which will improve the flexibility and accuracy of these
models. After which it can be deployed on the larger platform.

12. References

[1] https://www.ijert.org/covid-19-facemask-detection-with-deep-learning-and-
computer-vision
[2] Raza Ali, Saniya Adeel, Akhyar Ahmed Face Mask Detector July 2020.
[3] Amit Chavda, Jason D'souza, Sumit Badgujar Multi-Stage CNN Architecture for
Face Mask Detection September 2020.
[4] A FACEMASK DETECTOR USING MACHINE LEARNING AND
IMAGE PROCESSING TECHNIQUES. Amrit Kumar Bhadani, Anurag
Sinha, Department of computer science and IT, Student, Amity University
Jharkhand. Engineering Science and Technology an International Journal,
November
2020.
[5]  https://youtube.com/playlist?list=PLhhyoLH6IjfxVOdVC1P1L5z5azs0XjMsb
[6]  https://youtu.be/T9KfYaS9hwQ
[7] https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_
tutorials.html

19

You might also like