Coders Packet

MASK DETECTOR IN PYTHON USING OPENCV, KERAS AND TENSORFLOW.

By KRITIKA SHELLY

To build a model that can help to detect whether a person is wearing a mask or not with the help of a web camera using OpenCV, Keras and TensorFlow.

In this pandemic time, face mask becomes a vital element in our daily lives.
so, wearing a mask will protect you and others from the spread of the novel
coronavirus.
The primary goal of this project is to detect whether a person is wearing a
mask or not.
For this project we will require some libraries which are described below:
a) TensorFlow 1.15.2
b) Keras 2.3.1
c) Imutils 0.5.3
d) Numpy 1.18.2
e) Opencv-python 4.2.0
f) Matplotlib 3.2.1
g) Scipy 1.4.1

The link for the dataset is given below.

https://github.com/prajnasb/observations/tree/master/experiements/data

STEP 1: INSTALL THE DEPENDENCIES

Create a text file named requirements containing all libraries and put in
into an empty folder called Project, now run a command on the command
prompt in order to install all libraries.
Command:
pip install -r requirements.txt

STEP 2: LET'S TAKE A LOOK TO DATASET

Create a folder called dataset and copy all the contents of the dataset. Inside this dataset folder, there are 2 folders i.e
with_mask and without_mask.
This dataset consists of a total of 1376 images belonging to two classes:
a) with_mask: 690 images
This folder contains people wearing masks
b) without_mask: 686 images
This folder contains people not wearing masks.

STEP 3: DATA PREPROCESSING

1. In this part we will convert all the folders i.e with mask and without
mask into arrays.
Create a python file named train_mask_detector. There are many inputs
present in that file.
Some of the inputs and functions are listed below:
a) Directory is the input used to specify the path of the folder dataset.
b) Category is the input which contains 2 folders with_mask and
without_mask.
c) Two empty lists are also taken as inputs namely data and labels.
In this data list, we append all the image arrays and in the label list, we will
append all the images correspond to with mask and without a mask,
basically, it contains images indicating whether they are with a mask or
without a mask.
d) os.path.join - is used to join directory and category.
e) Listdir - is used to list all the images which are present.
f) load_img - is used to load the image
g) label Binarizer - is used to convert all the images to categorical form i.e 0
& 1 and then it is converted to Numpy arrays.
h) train_test_split - is used to test and split the data.

STEP 4:TRAINING

We will train our model using the below-mentioned method:

In this there are 5 stages:
a) Input- In this stage, the input is taken.
b) Convolution neural networks(CNN)- A convolutional neural network is a
type of artificial neural network that uses perceptrons, to analyze the data.
This also helps in image processing and other kinds of tasks.
c) Max pooling -Max Pooling takes the maximum value within the
convolution filter and reduces the dimensionality of the input image.
d) Fully connected -The image is then flattened.
e) Output- Now the output image is ready.

In this model, we will use mobile net instead of CNN because it is faster and
has lesser parameters.
Mobile net has 2 models -a) head model b) base model.
A plot will be generated to check accuracy using Matplotlib.

STEP 5: RUN AND VIEW ACCURACY

Now open the command prompt and enter the path in which your python
file is present.
Command - python train_mask_detector.py
After running the command, it will generate the model and plot.

STEP 6:APPLY MODEL TO CAMERA

We will use OpenCV for this.
2 models will be generated which are mentioned below:
a) Face detector model- this will detect the face of the user.
b) mask detector model- this will detect whether the user is wearing masks
or not.

STEP 7:FINAL RESULT

Let's try whether it is detecting the user's mask or not.
Yes, it will detect if you are wearing a mask or not

Download Complete Code

Comments

No comments yet