Coders Packet

Mask Detection using Python (TensorFlow, OpenCV)

By Shivesh Chaturvedi

This is the project on deep learning, it uses TensorFlow, OpenCV, and some other important libraries. This model detects the mask on your face.

This contains 3 sections - 1) Data Preprocessing 2) Training of Model 3) Final Prediction
The dataset and the code of the model are with this article, so please first go through the code and download the dataset and other provided files to deploy this model.
The file which I attach with this article contains - dataset, data preprocessing code, training of data code, final prediction code, data.npy, target.npy, haarcascade_frontalface_default, model-017.model.
Please download this file, and then read the codes properly, try to understand them.
Then only you can able to learn this article.

I will tell you only tough parts where you may get confused.

 

1) Data Preprocessing Process

In this process, we are preprocessing our data like- converting images to gray, dimensionality reduction, normalization.

Before doing this we first load the path of our dataset, there are two folders in the dataset folder- with mask, without mask.

We label out with mask folder as 0 and without mask 1.

data_path=r'G:\face-mask-detection-keras-master\dataset' #dataset in the directory
categories=os.listdir(data_path) #stores the path of dataset

labels=[i for i in range(len(categories))]
label_dict=dict(zip(categories,labels))

 

Then we use preprocessing steps and store our processed images in the data list and target values in target list.

And at last, convert them into an array and save them.

 


2) Training the data

Using convolutional layers performing neural operations on the layers.

Applying Conv2D, activation function-relu, and maxpooling methods. Then applying flatten, and dense on the layers.

Divide the data into train, cross validation and test sets.

Finally applying ModelCheckpoint, this will save your summed weights and other operations at the given checkpoints.In this function "model-{epoch:03d}.model" this is the path where this trained model will get saved.

Then using model.fit() function training of our model gets started.

 


3) Prediction Step

In this, we will use our trained model, run the live camera and start the prediction. 

This code is simple, just you need to make rectangles on the face of the person and if he's wearing the mask then write MASK there above the rectangle or else write WEAR YOUR MASK above the rectangles with different suitable colors.

cv2.rectangle(img,(x,y),(x+w,y+h),color_dict[label],2)
cv2.rectangle(img,(x,y-40),(x+w,y),color_dict[label],-1)
cv2.putText(img, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)

 


I hope you are getting this code, I know it's big but try learning this code.

Thank you for reading this article.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Shivesh Chaturvedi (shiveshchaturvedi)

Download packets of source code on Coders Packet