Karan Shah
Karan Shah's Blog

Karan Shah's Blog

Face Mask Detection using Open CV

Our motto is to detect facial mask monitoring using Open CV and Python to stop the flow of corona virus.

Face Mask Detection using Open CV
Karan Shah's photo
Karan Shah

Published on May 25, 2021

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Corona Virus disease has spread to more than 213 countries infecting more than 7 million people and killing over 403,202 globally, according to data compiled by worldometer (as on June 5, 2020)

To limit Corona Virus spread, social distancing and observing hygiene standards like compulsory wearing of mask, use of hand gloves, face shield, and use of sanitizer is very important.

Many Organizations are making it compulsory to follow social distancing and wearing of face mask. This article, explains how to detect facial mask monitoring using OpenCV and Python.

I am assumig that you have basic knowledge of OpenCV and Python.

There are two main steps:

  1. Identify human Face and Mouth in each frame of input video

  2. Identify Person is using Mask or not

Step 1: Identify Face and Mouth Steps for Face Prediction using Python and OpenCV are:

  1. Create HAAR Cascade object using ‘CascadeClassifier’ function and ‘haarcascade_frontalface_default.xml’

  2. Read image using function ‘imread’ (or ‘read’ for video/ camera input) function

  3. Convert in gray scale using ‘cvtColor’ function

  4. Detect face using ‘detectMultiScale’ function

To get details how Open CV detect face refer link Face Recognition with OpenCV — OpenCV 2.4.13.7 documentation

It has been observed that, person with white mask, most of time OpenCV cannot identify face correctly. To over come this difficulty, convert image in Black and White using ‘threshold’ function and then send this image to ‘detectMultiScale’ function.

Note: It is important to adjust threshold (bw_threshold) value in range 80 to 105 ‘threshold’ based on camera and surrounding light.

Following code detects face and mouth of a person in an image:

1.png

Step 2: Identify Person is using Mask or not

As shown in above code, there are three rectangle objects:

  1. ‘Gray’ image face rectangle.
  2. ‘Black & White’ image face rectangle.
  3. ‘Gray’ image Mouth rectangle.

Add following code to validate mask/ no-mask after ‘Detect Mouth’ code.

=> When Person is wearing the Mask it will show as Mask Detected :-

2.png

=> When Person is not Wearing Mask it will show as Mask not Detected :-

3.png

Mass Detection Criteria

Based on number of rectangles and rectangle position of mouth and face, we can create rule to detect mask. Following truth table will provide correct condition of with mask/ without mask.

4.png

You can download complete code and HAAR Cascade XML Files from GitHub.

That is all, I hope you liked the post. Feel Free to follow me.

 
Share this

Impressum

Do share your feedback and suggestions ❤️

you can connect with me at Website | GitHub | LinkedIn | Twitter

Proudly part of