Desperate times call for desperate measures. In the view of the ongoing pandemic, we should always be careful regarding our health and wearing a Face Mask is one of the most important precautions to take. Sadly, we come across people not wearing masks, and causing the deadly virus to spread🥺.In an attempt to control such activities, this Face Mask Detection Program can be used in public places, such as University Entrances, Parks and Canteens where we may find more people. The program is designed to detect whether a person is wearing his/her facemask properly or not.🤓👍
Objective - To detect whether a person is wearing his/her facemask properly with Live Web-Cam and OpenCV.😷
Prerequisites- Minimum knowledge in Python😄, and Download OpenCV
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.
Let's get Started!🤞🤞
If you're using Jupyter Notebook, Create a New Notebook and install OpenCV in your Anaconda Navigator. Use the command below:
!pip install opencv-python
Cv2 here, is the name of package:
import cv2
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's how:
- Open your browser and search from "Haar cascade Github"
- Open the Haar Cascade-Github link (The first one) and look for the file: haarcascade_frontalface_default.xml.
- Open haarcascade_frontalface_default.xml and download it.
Load the file📁:
haar_data = cv2.CascadeClassifier('Enter the path here')
To access Web-Cam and capture video🎥:
A Window capturing your video appears soon, and 200 images will be captured. Do not wear a Mask at this stage, we're giving the program input without Mask. Your Screen may look like😅🤦♀️:
Next:
import numpy as np
Save your inputs in without_mask.npy by: np.save('without_mask.npy',data)
Capture your video again, but this time wear a mask: Your Output should look like this:
Save these inputs in with_mask.npy by: np.save('with_mask.npy', data)
Further:
Combining the data:
X = np.r_[with_mask, without_mask
The following command stores total 400 inputs we've previously given:
labels = np.zeros(X.shape[0])
labels[200:] = 1.0
names = {0 : 'Mask', 1 : 'No Mask'}
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
x_train, x_test, y_train, y_test = train_test_split(X,labels, test_size=0.25)
from sklearn.decomposition import PCA
The following command gives us the accuracy of the model we created:
accuracy_score(y_test, y_pred)
Final Snippet✨😄:
Final Outputs✨✨(Without-Mask):
Output (With-Mask):
To exit, press "Esc".
Inspired by Live Implementation of Face Mask Detection using Python Webinar by BrainMentors.Pvt.Ltd
If you're facing any issues implementing this program, mention it in the comments below. Thanks for reading!