Introduction
In this tutorial, I have implemented a Face Mask Detection using the Convolution Neural Network. This project will detect your face with a mask or without mask. If the mask is on your face then it’ll give a result “Mask” and if the mask is not on your face then it’ll give “No Mask”.
I have split this project into two parts:
- You can check with a single image. If you give an image of faces to a model, then it’ll classify whether that person wears a mask or not.
- When you run the program then the camera will be open and model start classifying your face in real time.
So, I have trained my face mask detection model on a dataset and after training, I got above 96% validation accuracy.
About dataset:
In the recent trend in world wide Lockdowns due to COVID19 outbreak, as Face Mask has become mandatory for everyone while roaming outside, approach of Deep Learning for Detecting Faces With and Without mask was a good trendy practice. Here I have created a model that detects face masks trained on 4489 images with 3 color channels (RGB).
On Custom CNN architecture Model training accuracy reached 96% and Validation accuracy also reached 96%.
Let’s start :-
First of all, import required libraries like keras, matplotlib, etc. These libraries are used to train our face mask detection model.
Then create two variables: train path and valid path. Train path contains the training data’s path and the valid path contains the validation data’s path.
Now perform Data Augmentation on the training and validation data. Data Augmentation creates different angles of images. Like we have a normal image, then it’ll zoom the image, increase shear range, horizontal or vertical flip, etc. So, it will be helpful for our model to get a better accuracy.
Now create a function called PlotImage. PlotImage function helps us to visualize the images with the help of the matplotlib library.
Now it’s time to create our own CNN architecture using the keras library.
→ Conv2D layer with 32 filters, 3 kernel size and 200x200x3 input shape.
→ MaxPool2D layer with pool size is 2.
→ Conv2D layer with 64 filters and 3 kernel sizes.
→ MaxPool2D layer with pool size is 2.
→ Conv2D layer with 128 filters and 3 kernel sizes.
→ MaxPool2D layer with pool size is 2.
→ Conv2D layer with 256 filters and 3 kernel sizes.
→ MaxPool2D layer with pool size is 2.
→ Dropout layer or also called regularization.
→ Flatten layer to convert multi dimensional images to vector (1-d image or array).
→ Dense layer with 128 units and relu activation function.
→ Dropout layer.
→ Dense layer with 256 units and relu activation function.
→ Dropout layer.
→ Now output layer with 2 units (Mask or No Mask) and softmax activation function.
Now compiling the model with loss is sparse_categorical_crossentropy, optimizer is Adam and metrics is accuracy and display the summary of the model as you can see in the image below.
Now finally train the model with training and validation data for 50 epochs. Training will take some time (approx. 30 min) depending on your system configuration. You can prefer the Google Colab which provides you the GPU support. So, after training the model, I got 96% training and 96% validation accuracy.
Then save the model using the name face_mask_detection_model.h5.
Now I create a app.py file. Firstly import some important libraries like cv2, numpy, keras, etc. Then load the “face_mask_detection_model.h5” model. And keep a 200x200 frame size. Then load a haarcascade classifier “haarcascade_frontalface_default.xml”. This classifier is used to detect the faces. Then create an object of the VideoCapture class. Now write a code which will detect whether the Face wears a mask or not. And keep this code in a while loop which will continuously detect whether the Face wears a mask or not. You have to simply write a command in command prompt "python app.py". Your camera will start to open and detect your face whether the Face wears a mask or not and at the same time program will click your images with mask or without mask and save into a separate folder of with_mask and without_mask. These two folders are in the input_faces folder. You will get the the output of the project in the youtube video. you'll get the link in the end.
Now I create a single_face_classifier.ipnyb file. Firstly, import all required libraries like cv2, numpy, keras, etc. Then the same step loads the “face_mask_detection_model.h5” model. And keep a 200x200 image size. Then load a haarcascade classifier “haarcascade_frontalface_default.xml”. This classifier is used to detect the faces. Then create an object of the VideoCapture class. Now write a code which will classify whether the Face wears a mask or not in an image. your have to simply open this file in jupyter notebook and give the path of the image which you want to detect with mask or without mask and simple run. You will get the the output of the project in the youtube video. you'll get the link in the end.
Before run the project, you have to make a structure of the folder like the below image.
- input_faces folder: It'll store the output of your faces in the form of images.
- mask detection dataset folder: It contains the dataset of mask or without mask on which your model trained.
- app.py file: This file contains the code for continuously detect the your face with mask or without mask.
- face_mask_detection_model.h5: This is a trained model. With the help of it, your face will be detect whether face wears a mask or not.
- Face-mask-detection training.ipynb: This file contains the code for train the face mask detection model.
- haarcascade_frontalface_default.xml: This is a haarcascade classifier. This will help to detect the face.
- Single_mask_detection.ipynb: This file contains the code for detect a single image. If your give a image to model then model detect faces in the image and give a output whether face wears a mask or not.
Source Code:-
- Go to GitHub and fork or download the repo: Face Mask Detection
- Download the dataset: Dataset
- Download the trained model: Download model
- Then run app.py file in command prompt using command "python app.py". Make sure all the requirements should be satisfied in your system.