Face Mask Detector 🔎

Face Mask Detector 🔎

Objective - To detect whether a person is wearing his/her facemask properly with Live Web-Cam and OpenCV.

Prerequisites - Minimum knowledge in Python, in Machine Learning and Deep Learning, OpenCV

What is OpenCV? Open Source Computer Vision Library, or OpenCV, is an Open-Source computer vision and Machine Learning Software Library. It has over 2500 optimized algorithms. It’s features include: Face Detection, Geometric Transformations, Image Thresholding, Smoothing Images, Canny Edge Detection, Background Removals and Image Segmentation.

You can find the source code on my GitHub.

Get started! 💪

If you're using Jupyter Notebook, create a New Notebook and install OpenCV in your Anaconda Navigator using the conmmand below, otherwhise open your terminal and install it.

!pip install opencv-python

Import the package:

import cv2

Now talk about Haar Cascade. It is a machine learning based approach in which a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images. Download your haarcascade file from Github, here.

Load the file📁:

classifier = cv2.CascadeClassifier('Enter the path here')

Access to the web-cam to capture frames and draw a rectangle around the faces📹:

webcam = cv2.VideoCapture(0)

while True:
    (rval, im) = webcam.read()
    im = cv2.flip(im, 1, 1) # Flip to act as a mirror

    # Resize the image to speed up detection
    mini = cv2.resize(im, (im.shape[1] // size, im.shape[0] // size))

    # detect MultiScale / faces 
    faces = classifier.detectMultiScale(mini)

    # Draw rectangles around each face
    for f in faces:
        (x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
        #Save just the rectangle faces in SubRecFaces
        face_img = im[y : y + h, x : x + w]
        resized = cv2.resize(face_img, (150, 150))
        normalized = resized / 255.0
        reshaped = np.reshape(normalized, (1, 150, 150, 3))
        reshaped = np.vstack([reshaped])
        result = model.predict(reshaped)
        print(model.predict)

        label = np.argmax(result, axis = 1)[0]

        cv2.rectangle(im, (x, y), (x + w, y + h), color_dict[label], 2)
        cv2.rectangle(im, (x , y - 40), (x + w, y), color_dict[label], -1)
        cv2.putText(im, labels_dict[label], (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)

    # Show the image
    cv2.imshow('LIVE', im)
    key = cv2.waitKey(10)
    # if Esc key is press then break out of the loop 
    if key == 27: # The Esc key
        break

Next import NumPy:

import numpy as np

Scikit-learn is a free software machine learning library for the Python programming language👍. It features various classification, regression and clustering algorithms including support vector machines💻.

from sklearn.model_selection import train_test_split #pip install -U scikit-learn
from sklearn.metrics import f1_score
from sklearn.utils import shuffle

Final Output ✨

With mask: mask.png

Without mask: without_mask.png

To exit press "Esc".

Would you mind giving me a follow on Instagram and Twitter? You can check my LinkedIn and Github profiles too 😁.