text_plain_output_1.png
from scipy.spatial import distance

import cv2

face_model = cv2.CascadeClassifier('../input/haarcascades/haarcascade_frontalface_default.xml')

img = cv2.imread('../input/face-mask-detection/images/maksssksksss244.png')
img = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
faces = face_model.detectMultiScale(img, scaleFactor=1.1, minNeighbors=4)
out_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
for x, y, w, h in faces:
    cv2.rectangle(out_img, (x, y), (x + w, y + h), (0, 0, 255), 1)

MIN_DISTANCE = 130

if 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])
            if dist < MIN_DISTANCE:
                label[i] = 1
                label[j] = 1
    new_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    for i in range(len(faces)):
        x, y, w, h = faces[i]
        if label[i] == 1:
            cv2.rectangle(new_img, (x, y), (x + w, y + h), (255, 0, 0), 1)
        else:
            cv2.rectangle(new_img, (x, y), (x + w, y + h), (0, 255, 0), 1)
    plt.figure(figsize=(10, 10))
    plt.imshow(new_img)
else:
    print('No. of faces detected is less than 2')