Inspiration
The new Coronavirus disease (COVID-19) has seriously affected the world. By the end of November 2020, the global number of new coronavirus cases had already exceeded 60 million and the number of deaths 1,410,378 according to information from the World Health Organization (WHO). To limit the spread of the disease, mandatory face-mask rules are now becoming common in public settings around the world. Additionally, many public service providers require customers to wear face masks in accordance with predefined rules (e.g., covering both mouth and nose) when using public services. These developments inspired me to build a simple face-mask detector that may help monitor public behavior and contribute towards constraining the COVID-19 pandemic. Although existing research in this area resulted in efficient techniques for face-mask detection, I tried to implement my knowledge to build a beginner-level mask detector...

Fig: Example images from the MAFA dataset. Similar to other publicly available datasets of masked faces, the MAFA dataset contains images where facial masks are present but are not necessarily placed/worn in accordance with recommendations from medical experts—which is critical for containing pandemics, such as COVID-19. All the presented examples are labeled as masked faces, while only the ones marked green have correctly placed masks.
Pre-processing
We used ImageDataGenerator to:
- Read the picture files
- Decode the .png content to RGB pixels
- Convert these to floating point tensors
- Rescale the pixel values (between 0 and 255) to the [0,1] interval
train_datagen = ImageDataGenerator(rescale=1.0/255, rotation_range=40, width_shift_range=0.2,
height_shift_range=0.2, shear_range=0.2, zoom_range=0.2,
horizontal_flip=True)
# Performing Data Augmentation to avoid overfitting
test_datagen = ImageDataGenerator(rescale=1.0/255)
validation_datagen = ImageDataGenerator(rescale=1.0/255)
Mask-Detection Methods
Multiple solutions for detecting masked-face images have been presented in the literature or made available online over the last year. In this project, I have used VGG19 model to predict face masks.
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(128,128,3)))
model.add(keras.layers.MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(64, (3,3), activation='relu'))
model.add(keras.layers.MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(128, (3,3), activation='relu'))
model.add(keras.layers.MaxPooling2D((2,2)))
model.add(keras.layers.Conv2D(128, (3,3), activation='relu'))
model.add(keras.layers.MaxPooling2D((2,2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dropout(0.3))
model.add(keras.layers.Dense(512, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
I first used the above model architecture, which gave me an accuracy of 98.33 %.
Then, I used the VGG-19 model for the experimental evaluation. The VGG network is a 19-layer CNN model that uses a series of convolutional layers with small filters sizes 3 × 3, to facilitate training. As with AlexNet, the last two layers of VGG-19 are again fully connected and used to define the learning objective for the model. More details about the VGG-19 model can be found here
Model: sequential
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
vgg19 (Functional) (None, 4, 4, 512) 20024384
_________________________________________________________________
flatten (Flatten) (None, 8192) 0
_________________________________________________________________
dropout (Dropout) (None, 8192) 0
_________________________________________________________________
dense (Dense) (None, 512) 4194816
_________________________________________________________________
dense_1 (Dense) (None, 1) 513
=================================================================
Total params: 24,219,713
Trainable params: 4,195,329
Non-trainable params: 20,024,384
_________________________________________________________________
This model gave me an accuracy of 99.4 %.
Model Predictions
I fed two images (one with mask and other without mask) to the model, and the predictions were correct...

The model predicted this picture as wearing a mask...
While, this picture was predicted as not wearing a mask.
Log in or sign up for Devpost to join the conversation.