Real Time Face Mask Detector with OpenCV, Keras/TensorFlow, and Deep Learning

Image for post
Image for post

In these tough COVID-19 times, everyone want to make something related COVID-19 and which is also useful for others. So why not to build a very simple and basic OpenCV project to detect if you are wearing a face mask to protect yourself, which also shows you accuracy of it.

For building this model, We will be using the face mask dataset provided by Prajna Bhandary. It consists of about 1,376 images with 690 images containing people with face masks and 686 images containing people without face masks.

Given the trained COVID-19 face mask detector, we’ll proceed to implement two more additional Python scripts used to:

To create this dataset, Prajna had the ingenious solution of:

This method is actually a lot easier than it sounds once you apply facial landmarks to the problem.

Facial landmarks allow us to automatically infer the location of facial structures, including:

  • Eyes
  • Eyebrows
  • Nose
  • Mouth
  • Jawline

Implementing our COVID-19 face mask detector in real-time video streams with OpenCV

Let see how to detect face with real time video stream. First install necessary packages

This function detects faces and then applies our face mask classifier to each face ROI. Such a function consolidates our code — it could even be moved to a separate Python file if you so choose.

Our detect_and_predict_mask function accepts three parameters:

  • frame : A frame from our stream
  • faceNet : The model used to detect where in the image faces are
  • maskNet : Our COVID-19 face mask classifier model

Inside, we construct a blob , detect faces, and initialize lists, two of which the function is set to return. These lists include our faces (i.e., ROIs), locs (the face locations), and preds (the list of mask/no mask predictions).

From here, we’ll loop over the face detections:

We’re now ready to run our faces through our mask predictor:

Next, we’ll define our command line arguments:

Our command line arguments include:

  • face: The path to the face detector directory
  • model: The path to our trained face mask classifier
  • confidence: The minimum probability threshold to filter weak face detections

With our imports, convenience function, and command line args ready to go, we just have a few initializations to handle before we loop over frames:

Here we have initialized our:

  • Face detector
  • COVID-19 face mask detector
  • Webcam video stream

Let’s proceed to loop over frames in the stream:

Great job implementing your real-time face mask detector with Python, OpenCV, and deep learning with TensorFlow/Keras!

Image for post
Image for post

From above video we can see it was correctly able to detect if I was wearing a mask or not and displays the mask accuracy.

Here is full code on my GitHub repository

Download the dataset and paste it in your project

In this article, we have successfully built a tensor flow project to detect if a person is wearing a face mask or not. This can be used in numerous applications. Sporting a mask may be necessary in the near future, considering the COVID-19 crisis and this method to detect if the person wears a face mask may come very useful.

Stay Safe !

Written by

Tech Lead and Founder at @XenonStudio. #Mobile #AI. Visit :-

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store