You are on page 1of 17

Face Mask Detection

A report submitted in partial fulfilment of the requirements for the award of the degree of

BACHELOR OF TECHNOLOGY
in
Electronics and Communication Engineering

By
GAURAV KUMAR (19220)

SCHOOL OF ELECTRONICS/COMPUTING

INDIAN INSTITUTE OF INFORMATION TECHNOLOGY UNA


HIMACHAL PRADESH

MAY 2022
BONAFIDE CERTIFICATE

This is to certify that the project titled Face Mask Detection is a bonafide record of the
work done by

GAURAV KUMAR (19220)

in partial fulfilment of the requirements for the award of the degree of Bachelor of
Technology in ECE of the INDIAN INSTITUTE OF INFORMATION TECHNOLOGY
UNA, HIMACHAL PRADESH, during the year 2019 - 2023.

under the guidance of


Dr. Vanita Arora

Examiner (Guide Name)

i
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
ORIGNALITY / NO PLAGARISM DECLARATION

I certify that this internship report is my original report and no part of it is copied from any
published reports, papers, books, articles, etc. I certify that all the contents in this report are
based on my personal findings and research and I have cited all the relevant sources which
have been required in the preparation of this project report, whether they be books, articles,
reports, lecture notes, and any other kind of document. I also certify that this report has not
previously been submitted partially or as whole for the award of degree in any other
university in India and/or abroad.

I hereby declare that, I am fully aware of what constitutes plagiarism and understand that if
it is found at a later stage to contain any instance of plagiarism, my degree may be
cancelled.

GAURAV KUMAR (19220)

ii
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
ABSTRACT

The COVID-19 pandemic has caused a worldwide emergency in


healthcare. This virus mainly spreads through droplets which
emerge from a person infected with coronavirus and poses a risk to
others. The risk of transmission is highest in public places. One of
the best ways to stay safe from getting infected is wearing a face
mask as indicated by the WHO.
In this project, I propose a method which uses TensorFlow and
OpenCV to detect face masks on people. A bounding box drawn
over the face of the person describes weather the person is wearing a
mask or not.

Keywords - COVID-19, TensorFlow, OpenCV, Face


Mask, Image Processing, Computer Vision

iii
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
ACKNOWLEDGEMENT

I would like to thank the following people for their support and guidance without whom the
completion of this internship in fruition would not be possible.

I would like to express my sincere gratitude and heartfelt thanks to Dr. Vanita Arora for the
unflinching support and guidance, valuable suggestions and expert advice. her words of
wisdom and expertise in subject matter were of immense help throughout the duration of
this project.

I also take the opportunity to thank our Director and all the faculty of School of
Computing/Electronics, IIIT Una for helping me by providing necessary knowledge base
and resources.

I would also like to thank my parents, brother and friends for their constant support.

GAURAV KUMAR (19220)

iv
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
TABLE OF CONTENTS

Title Page No.

ABSTRACT iii
ACKNOWLEGEMENT iv
TABLE OF CONTENTS v

1 Introduction 1-2

2 Working 3-4

3 Result 5-6

4 Conclusion 7

Appendices 8-12

v
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
Chapter 1
Introduction

COVID-19 has had a big impact on human lives. The pandemic led to the loss
of millions and affected the lives of billions of people. Its negative impact was
felt by almost all commercial establishments.
After the person gets infected, it takes almost fourteen days for the virus to
grow in the body of its host and affect them and in the meantime, it spreads to
almost everyone who is in contact with that person. So, it is extremely hard to
keep the track of the spread of COVID-19. COVID-19 mainly spreads through
droplets produced as a result of coughing or sneezing by an infected person.
This transfers the virus to any person who is in direct close contact (Within
one-meter distance) with the person suffering from coronavirus. Because of
this, the virus spreads rapidly among the masses. With the nationwide
lockdowns being lifted, it has become even harder to track and control the
virus. Face masks are an effective method to control the spread of virus. The
governments, all over the world, have imposed strict rules the everyone should
wear masks while they go out. But still, some people may not wear masks and
it is hard to check weather everyone is wearing mask or not.
In such cases, computer vision will be of great help. There are no efficient
face mask detection applications to detect weather the person is wearing face
mask or not. This increases the demand for an efficient system for detecting
face masks on people for transportation means, densely populated areas,
residential districts, large-scale manufacturers and other enterprises to ensure
safety. This project uses machine learning classification using OpenCV and
TensorFlow to detect facemasks on people.

MACHINE LEARNING CLASSIFIERS:

These are used to predict the class/target/labels/categories of a given data


points. Classification belongs to the category of supervised learning in which
the targets are provided with input data. They are used in many applications
like medical
diagnosis, spam detection, target marketing etc. They use a mapping function
(f) from input variables (X) to discrete output variables(Y).

1
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
OPENCV:

OpenCV is an open-source library which is primarily used for Computer


Vision Applications. This contains many functions and algorithms for Motion
tracking, Facial recognition, Object Detection, Segmentation and recognition
and many
other applications. Images and real time video streams can be manipulated to
suit different needs using this library.

TENSORFLOW:

It is an open-source machine learning framework to build and train neural


networks. It has a collection of tools, libraries and community resources which
helps in easy building of deployment of ML powered applications. This is
developed and maintained by Google and was released in 2015.

2
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
Chapter 2
Working

This project makes the use of OpenCV, Caffe-based face detector, Keras,
TensorFlow and MobileNetV2 for the detection of face mask on humans. The
dataset which is being used contains 3833 images out of which 1915 images
have people with masks in them and 1918 people without masks in them.

Dataset Collection: The dataset was collected from Kaggle Repository and
was split into training and testing data after its analysis.
Training a model to detect face masks: A default OpenCV module was used
to obtain faces followed by training a Keras model to identify face mask.
Detecting the person not wearing a mask: A OpenCV model was trained to
detect the names of the people who are not wearing masks by referring the
database.
A base model is generated by using Keras and MobileNetV2. The generated
model is then trained with the labeled dataset by splitting it into two portions.
One portion contains 80 percent images and it is used for training. The
remaining portion contains the remaining 20 percent of images and is used for
testing the model accuracy. After the model is trained, it can be used for
detection of facemask on human faces. The trained model is loaded and image
which contains human faces with or without masks or a continuous video
stream with humans is given as input.
The image or a frame of the video, in case the input is a video stream, is first
sent to the default face detector module for the detection of human faces. This
is done by resizing the image or the video frame first, followed by detecting
the blob in it. This detected blob is sent to the face detector model which
outputs only the cropped face of a person without the background. This face is
given as the input to the model which we trained earlier. This outputs weather
there is a mask or not.

3
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
4
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
RESULT

5
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
CONCLUSION
With the increasing number of COVID cases all over the world, a system to
replace humans to check masks on the faces of people is greatly needed. This
system satisfies that need. This system can be employed in public places like
railway stations and malls. It will be of a great help in companies and huge
establishments where there will be a lot of workers. This system will be of a
great help there because it is easy to obtain and store the data of the employees
working in that Company and will very easily find the people who are not
wearing the mask and a mail will sent to that respective person to take
Precautions not wearing mask.

6
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
Appendices

7
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
Appendix A
Code Attachments
The following is the partial / subset of the code. Code of some module(s) have been
wilfully supressed.

A.1 Sample Code

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input


from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from imutils.video import VideoStream
import numpy as np
import imutils
import time
import cv2
import os

def detect_and_predict_mask(frame, faceNet, maskNet):


    # grab the dimensions of the frame and then construct a blob
    # from it
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224),
        (104.0, 177.0, 123.0))

    # pass the blob through the network and obtain the face detections
    faceNet.setInput(blob)
    detections = faceNet.forward()
    print(detections.shape)

    # Initialize our list of faces, their corresponding locations,


    # and the list of predictions from our face mask network
    faces = []
    locs = []
    preds = []

    # loop over the detections


    for i in range(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with

8
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
        # the detection
        confidence = detections[0, 0, i, 2]

        # filter out weak detections by ensuring the confidence is


        # greater than the minimum confidence
        if confidence > 0.5:
            # compute the (x, y)-coordinates of the bounding box for
            # the object
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            # ensure the bounding boxes fall within the dimensions of


            # the frame
            (startX, startY) = (max(0, startX), max(0, startY))
            (endX, endY) = (min(w - 1, endX), min(h - 1, endY))

            # extract the face ROI, convert it from BGR to RGB channel


            # ordering, resize it to 224x224, and preprocess it
            face = frame[startY:endY, startX:endX]
            face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            face = cv2.resize(face, (224, 224))
            face = img_to_array(face)
            face = preprocess_input(face)

            # add the face and bounding boxes to their respective


            # lists
            faces.append(face)
            locs.append((startX, startY, endX, endY))

    # only make a predictions if at least one face was detected


    if len(faces) > 0:
        # for faster inference we'll make batch predictions on *all*
        # faces at the same time rather than one-by-one predictions
        # in the above `for` loop
        faces = np.array(faces, dtype="float32")
        preds = maskNet.predict(faces, batch_size=32)

    # return a 2-tuple of the face locations and their corresponding


    # locations
    return (locs, preds)

# load our serialized face detector model from disk


prototxtPath = r"face_detector\deploy.prototxt"
weightsPath = r"face_detector\res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)

9
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
# load the face mask detector model from disk
maskNet = load_model("mask_detector.model")

# initialize the video stream


print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()

# loop over the frames from the video stream


while True:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=400)

    # detect faces in the frame and determine if they are wearing a


    # face mask or not
    (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)

    # loop over the detected face locations and their corresponding


    # locations
    for (box, pred) in zip(locs, preds):
        # unpack the bounding box and predictions
        (startX, startY, endX, endY) = box
        (mask, withoutMask) = pred

        # determine the class label and color we'll use to draw


        # the bounding box and text
        label = "Mask" if mask > withoutMask else "No Mask"
        color = (0, 255, 0) if label == "Mask" else (0, 0, 255)

        # include the probability in the label


        label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)

        # display the label and bounding box rectangle on the output


        # frame
        cv2.putText(frame, label, (startX, startY - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
        cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)

    # show the output frame


    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop

10
School of Electronics/Computing, IIITU [Internship code] : IIITU19220
    if key == ord("q"):
        break

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

11
School of Electronics/Computing, IIITU [Internship code] : IIITU19220

You might also like