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:
Identify human Face and Mouth in each frame of input video
Identify Person is using Mask or not
Step 1: Identify Face and Mouth Steps for Face Prediction using Python and OpenCV are:
Create HAAR Cascade object using ‘CascadeClassifier’ function and ‘haarcascade_frontalface_default.xml’
Read image using function ‘imread’ (or ‘read’ for video/ camera input) function
Convert in gray scale using ‘cvtColor’ function
Detect face using ‘detectMultiScale’ function
To get details how Open CV detect face refer link Face Recognition with OpenCV — OpenCV 126.96.36.199 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:
Step 2: Identify Person is using Mask or not
As shown in above code, there are three rectangle objects:
- ‘Gray’ image face rectangle.
- ‘Black & White’ image face rectangle.
- ‘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 :-
=> When Person is not Wearing Mask it will show as Mask not Detected :-
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.
That is all, I hope you liked the post. Feel Free to follow me.