Face Mask Detection with Machine Learning

Machine Learning Project on Face Mask Detection with Python.

Face detection has become a very interesting problem in image processing and computer vision. In this article, I will introduce you to a computer vision project on Face Mask Detection with Machine Learning using Python.

Introduction to Face Mask Detection

Face mask detection has a range of applications from capturing the movement of the face to facial recognition which at first requires the face to be detected with very good precision. Face detection is more relevant today as it is not only used on images, but also in video applications like real-time surveillance and face detection in videos.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

High precision image classification is now possible with advances in convolutional networks. Pixel level information is often needed after face detection, which most face detection methods do not provide.

Obtaining pixel-level detail has been a difficult part of semantic segmentation. Semantic segmentation is the process of assigning a label to each pixel in the image.

Process of Face Mask Detection with Machine Learning

  • Step 1: Extract face data for training.
  • Step 2: Train the classifier to classify faces in mask or labels without a mask.
  • Step 3: Detect faces while testing data using SSD face detector.
  • Step 4: Using the trained classifier, classify the detected faces.

In the third step of the above process, you have to think about what is the SSD face detector? Well, the SSD is a Single Shot Multibox Detector. This is a technique used to detect objects in images using a single deep neural network.

It is used for the detection of objects in an image. Using a basic architecture of the VGG-16 architecture, the SSD can outperform other object detectors such as YOLO and Faster R-CNN in terms of speed and accuracy.

Face Mask Detection with Machine Learning

Now, let’s get started with the task of Face Mask Detection with Machine Learning by using the Python programming language. I will start this task by importing the necessary Python libraries that we need for this task:

Creating Helper Functions

I will start this task by creating two helper functions:

  1. The getJSON function retrieves the json file containing the bounding box data in the training dataset.
  2. The adjust_gamma function is a non-linear operation used to encode and decode luminance or tristimulus values in video or still image systems. Simply put, it is used to instil a little bit of light into the image. If gamma <1, the image will shift to the darker end of the spectrum and when gamma> 1, there will be more light in the image.

Data Processing

The next step is now to explore the JSON data provided for the training:

jsonfiles= []
for i in os.listdir(directory):
    jsonfiles.append(getJSON(os.path.join(directory,i)))
jsonfiles[0]
{'FileName': '2349.png',
 'NumOfAnno': 4,
 'Annotations': [{'isProtected': False,
   'ID': 193452793312540288,
   'BoundingBox': [29, 69, 285, 343],
   'classname': 'face_other_covering',
   'Confidence': 1,
   'Attributes': {}},
  {'isProtected': False,
   'ID': 545570408121800384,
   'BoundingBox': [303, 99, 497, 341],
   'classname': 'face_other_covering',
   'Confidence': 1,
   'Attributes': {}},
  {'isProtected': False,
   'ID': 339053397051370048,
   'BoundingBox': [8, 71, 287, 373],
   'classname': 'hijab_niqab',
   'Confidence': 1,
   'Attributes': {}},
  {'isProtected': False,
   'ID': 100482004994698944,
   'BoundingBox': [296, 99, 525, 371],
   'classname': 'hijab_niqab',
   'Confidence': 1,
   'Attributes': {}}]}
  • The Annotations field contains the data of all the faces present in a particular image.
  • There are different class names, but the real class names are face_with_mask and face_no_mask.
df = pd.read_csv("train.csv")
df.head()
namex1x2y1y2classname
02756.png69126294392face_with_mask
12756.png50510723283face_with_mask
22756.png75252264390mask_colorful
32756.png521136711277mask_colorful
46098.jpg36085728653face_no_mask

Using the mask and the non_mask labels, the bounding box data of the json files is extracted. The faces of a particular image are extracted and stored in the data list with its tag for the learning process.

mask and no mask visualization

The visualization above tells us that the number of mask images> Number of images without a mask, so this is an unbalanced dataset. But since we’re using a pre-trained SSD model, which is trained to detect unmasked faces, this imbalance wouldn’t matter much.

But let’s reshape the data before training a neural network:

Training Neural Network for Face Mask Detection

Now the next step is to train a Neural Network for the task of Face Mask Detection with Machine Learning:

Testing The Model

The test dataset contains 1698 images and to evaluate the model so I took a handful of images from this dataset as there are no face tags in the dataset:

face mask detection

By analyzing the output above, we can observe that the whole system works well for faces that have spatial dominance. But fails in the case of images where the faces are small and take up less space in the overall image.

For best results, different image preprocessing techniques can be used, or the confidence threshold can be kept lower, or one can try different blob sizes.

Hope you liked this article on face mask detection with machine learning using the Python programming language. Please feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply