Analytics Vidhya
Published in

Analytics Vidhya

Mask and Social Distancing Detector: Part 1

Discipline is an immense issue with the majority of the Indian population. People are not willing to maintain social distancing or wear a mask while they are out. Most of us are careless about our mask hygiene leading to a deadly outbreak of the coronavirus in India killing lakhs.

Another issue is the police brutality in India and insensitivity among powerful people. Every day we read about a poor man beaten or even killed by police for not wearing a mask. In India, there is hardly a middle ground. So I was wondering if we can leave it to the machines to identify and punish the people for breaking the corona rules.

We will design an algorithm to identify

  1. If the group is maintaining a safe distance.
  2. if the people in a photo are wearing a mask or not.

Face Detection

We can start with designing a face detector. let’s import all the required libraries. Some of these libraries will be used later on, so don’t be bothered.

import seaborn as sns
import os
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import cv2
from scipy.spatial import distance

We don’t have to write and train a Haar classifier since it is already trained. Download the Haar cascade XML files from here. We will use an already trained classifier frontal face classifier. We have pre-trained classifiers for all features but we only need a face detector for now.

face_model = cv2.CascadeClassifier('mask/haarcascade_frontalface_default.xml')
img = cv2.imread('images/GOPR1592.JPG')
img = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
faces = face_model.detectMultiScale(img,scaleFactor=1.2, minNeighbors=2, minSize=(100, 50))

img_2 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
for (x,y,w,h) in faces:


We can see rectangles marked around the faces for face detection. Let’s read through the code and understand the terms mentioned

1. Haar Classifier

It is an object detection algorithm that can be used to detect faces by locating the edges and pixel color differences between features. For example, your eyes will be darker than their adjacent features and so will be your nose, lips, and eyebrows.

For a comprehensive understanding, you can read from here.

2. Detect Multiscale

This function is used to detect the faces. This function will return a rectangle with coordinates(x,y,w,h) around the detected face. The faces variable has the rectangle coordinates which will be drawn around the detected faces. Different parameters this function takes is

It takes 3 common arguments

  1. Image
  2. Scale factor: specifies how much the image size is reduced with each scale
  3. min neighbors: specifies how many neighbors each candidate rectangle should have to retain it

Social DIstancing Detector

Now that our faces are detected, We will now detect if the social distancing is maintained or not.

MIN_DISTANCE = 1000if len(faces)>=2:
label = [0 for i in range (len(faces))]
for i in range(len(faces)-1):
for j in range(i+1, len(faces)):
dist = distance.euclidean(faces[i][:2], faces[j][:2])
label[i] = 1
label[j] = 1

new_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
for i in range(len(faces)):
if label[i]==1:
cv2.rectangle(new_img,(x,y), (x+w, y+h),(255,0,0),10)
#if distance<MIN_DISTANCE then red box showing social distancing not maintained
cv2.rectangle(new_img,(x,y),(x+w,y+h),(0,255,0),15)#else green box showing Social distancing maintained
print("No. of faces detected is less than 2")

The classifier has rightly detected that I'm socially distant while the other two persons are not. My face has a green

rectangle while other two have red rectangles around there face.

Let’s read through the code and understand it.

1. Scipy

we are using the distance function from Scipy library to calculate Euclidean distance between two 1-D arrays

2. Loops

Since we have to calculate the distance between all the faces(3) present in the photo, all the faces need to be compared and the distance calculated. “i” will be having a range of (0,2) while”j” will be having a range of (1,3). Hence while iterating distance for every face will be compared one by one.

For the faces having distance less than accepted social distance, labels will be updated to 1 from zero.

For all the labels will value 1 will be shown having a red rectangle around their faces.


So far we have completed a face and distance detector. We can continue from here in part-2 and design a mask detector.



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
Anmol Hans

Anmol Hans

Geopolitics and Data Science enthusiast.