Real time Face Mask Detection System
By: Saumy Srivastava
Tags: TensorFlow, Face Mask Detection, Covid 19, Opencv, Keras

Real Time Face Mask Detector

“Productivity is being able to do things that you were never able to do before.” — Franz Kafka

The recent coronavirus pandemic has pushed humans all around the world to the new challenges. In this context of uncertainty, we can all play our role by contributing to the fight against this disease. This is an excellent opportunity to put technology at the service of humanity. So, we can contribute towards this epidemic by implementing the productive things in its favour.

What if we had a system that could monitor whether people around us are complying with all the safety measures? So why not to build a very simple and basic Convolutional Neural Network (CNN) model using TensorFlow with Keras library and OpenCV project to detect if you are wearing a face mask to protect yourself, which also shows you accuracy of it.

Convolutional Neural Network (CNN) — Deep Learning

Neural network with many convolutional layers

CNN’s are inspired by the structure of the brain but our focus will not be on neural science here as we do not have any expertise or academic knowledge in any of the biological aspects. Also to note they are nowhere close to the actual human brain. CNN’s are a class of Neural Networks that have proven very effective in areas of image recognition, processing, and classification.

As per Wiki — In machine learning, a convolutional neural network (CNN, or ConvNet) is a class of deep, feed-forward artificial neural networks, most commonly applied to analysing visual imagery.

They exist already for several decades but were shown to be very powerful when large labeled datasets are used. This requires fast computers (e.g. GPUs)!

This network is a great example of variation for multilayer perceptron for processing and classification. It’s a deep learning algorithm in which it takes input as an image and put weights and biases effectively to its objects and finally able to differentiate images from each other.

TensorFlow with Keras Library

TensorFlow with Keras

In this , we’ll be talking about two of the many libraries and frameworks — TensorFlow and Keras.

What is TensorFlow?

TensorFlow is an open-sourced library. It is one of the most famous libraries when it comes to dealing with Deep Neural Networks. The primary reason behind the popularity of TensorFlow is the sheer ease of building and deploying applications using TensorFlow.

TensorFlow excels at numerical computing, which is critical for deep learning. It provides APIs in most major languages and environments needed for deep learning

What is Keras?

Keras is a completely Python-based framework, which makes it easy to debug and explore. It is a high-level library that’s built on top of TensorFlow. It provides a scikit-learn type API for building Neural Networks. Developers can use Keras to quickly build neural networks without worrying about the mathematical aspects of tensor algebra, numerical techniques, and optimization methods.

The key idea behind the development of Keras is to facilitate experiments by fast prototyping. The ability to go from an idea to result with the least possible delay is key to good research.

This offers a huge advantage for scientists and beginner developers alike because they can dive right into Deep Learning without getting their hands dirty with low-level computations.

OpenCV

OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.

OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.

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.

Importing Necessary Packages

Function which detects the faces and then applies our face mask classifier to each face.

Function to detect faces and appling mask classifier

The above mentioned detect_and_predict_mask function accepts three parameters:

frame: A frame from our stream

faceNet : This is the model used to detect the faces inside the image

maskNet : The 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 detections of faces:

Loop over face detection

Now, we can implement the mask predictor created for predicting the faces.

Next, defining some of the command line arguments:

The command line arguments mentioned above includes:

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 :

Face detector

face mask detector

Webcam video stream

Now, we’ll proceed to loop over frames from the live webcam video stream:

Loop over frames from the stream

All the above mentioned code snippets are for detecting the face mask in live webcam video stream. It is just for understanding the Detection through live stream part. Thus, implementation of real-time face mask detector with Python, OpenCV, and deep learning with TensorFlow/Keras can be done.