How to Make Mask Detection AI Using Python in 6 Steps

Comprehensive guide to create mask detection AI using keras step by step

Philip Purwoko
Analytics Vidhya
4 min readDec 9, 2020

--

Photo by Volodymyr Hryshchenko on Unsplash
Photo by Volodymyr Hryshchenko on Unsplash

Abstraction

In this article I want to share my experience making mask detection app. I create Convolutional Neural Network using Keras, Python. The purpose of this project is to classify facial photos, where we will divide the data into two classes, so that we can answer the following question.

Is that person wearing a mask or not?

I don’t even think about this idea before, this idea is just pass through my head. I think it’s pretty cool and I quickly write it before it’s gone. Maybe this is the proof of Albert Einstein’s says.

“Imagination is more important than knowledge”

Photo by Taton Moïse on Unsplash
Photo by Taton Moïse on Unsplash

Project Workflow

Step 1: Data Collection

You can find the dataset easily on the Kaggle website. I’m looking for the ideal dataset (the download size is neither too big nor too small). I finally decided to use the following dataset.

The dataset is 160 MB in size, containing 7553 photos with 3-channel jpg extension (RGB). The dataset consists of 3725 photos of faces with masks and 3828 photos of faces without masks with various photo sizes. You can search for large datasets and use Transfer Learning to build more precise models.

Step 2: Get Environment Ready

First of all you need to download the dataset and extract the zip file. Refactor the directory so that it looks like this.

dataset
├── train
│ ├── with_mask
│ └── without_mask
└── valid
├── with_mask
└── without_mask

Next, we need to import all required library. Define loading_data() function to iterate trough directory to get all of files directory into python list.

Step 3: Data Preprocessing

Here I don’t use Keras flow_from_directory to load the dataset, but I use Tensorflow input output tf.io.read_file(path) because I want to create a batch of data before training the model, so that the training process can run faster.

During preprocessing you can use image augmentation techniques to vary the data, in order to improve model performance and avoid overfitting by flipping, rotating, cropping, zooming, and shifting the image position.

Step 4: Building Neural Network

I created a Convoutional Neural Network (CNN) with Keras. Here I use Dense layer 2 units with Softmax activation as the output node because I want 2 array outputs to make it easier to receive predicted information. Because, I have also tried with 1 unit of Dense layer using Sigmoid activation and the results are not that significant.

Get model summarization using model.summary() function.

Model: "Mask Detector"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_18 (Conv2D) (None, 222, 222, 32) 896
_________________________________________________________________
max_pooling2d_18 (MaxPooling (None, 111, 111, 32) 0
_________________________________________________________________
conv2d_19 (Conv2D) (None, 109, 109, 64) 18496
_________________________________________________________________
max_pooling2d_19 (MaxPooling (None, 54, 54, 64) 0
_________________________________________________________________
conv2d_20 (Conv2D) (None, 52, 52, 128) 73856
_________________________________________________________________
max_pooling2d_20 (MaxPooling (None, 26, 26, 128) 0
_________________________________________________________________
flatten_6 (Flatten) (None, 86528) 0
_________________________________________________________________
dense_12 (Dense) (None, 128) 11075712
_________________________________________________________________
activation_6 (Activation) (None, 128) 0
_________________________________________________________________
dense_13 (Dense) (None, 2) 258
_________________________________________________________________
activation_7 (Activation) (None, 2) 0
=================================================================
Total params: 11,169,218
Trainable params: 11,169,218
Non-trainable params: 0

Step 5: Training Model

The training process only run for 5 epochs. I also use early stopping so that the training process can be stopped automatically if the model doesn’t improve for 3 epcohs. This is also use to avoid overfitting.

Training process graph in 5 epochs

Step 6: Demonstration

I did a demonstration using 4 photos that the model had never seen before. And the results are quite surprising for me because the model can predict everything correctly with a large probability value.

Neural network predict people not wearing mask

I saved the model into an h5 file using model.save('output/face_mask_ai.h5') then made a python script using the opencv library. After that I tried to use this model to make predictions by video. The only reason why I use video is because my computer is too weak if it is done in real time using the camera.

Resolution

You can continue this project to the deployment process and create it as an API for use in other projects. For a satisfying portfolio, maybe you can create a Mask Camera application on a mobile device to detect mask wear in real time. The applications should run online using an API, so it don’t consume smartphone’s resources.

If you want full code of this project, you can checkout my Github repository https://github.com/PhilipPurwoko/Face-Mask-Detection

Enjoy Learning !

--

--

Philip Purwoko
Analytics Vidhya

Senior Software Developer & IT Consultant — Indonesia — purwoko.dev