FACE MASK DETECTOR
Real-time Face mask detection using OpenCv , keras CNN classifier i.e Vgg16
During pandemic COVID-19, WHO has made wearing masks compulsory to protect against this deadly virus.
So, We will build a real-time system to detect whether the person on the webcam is wearing a mask or not. We will train the face mask detector model using Keras and OpenCV.
The idea is to build a system that will help the government or traffic police to put an eye on the people and ensure that the social distancing and face masking is being followed strictly or not.
In order to train a custom face mask detector, we need to break our project into two distinct phases.
Two-phase COVID-19 face mask detector
- Training: Here we’ll focus on loading our face mask detection dataset from disk, training a model (using Keras/TensorFlow) on this dataset, and then serializing the face mask detector to disk
- Deployment: Once the face mask detector is trained, we can then move on to loading the mask detector which we have trained and placed in the disk, performing face detection from our image or video steam, and then classifying each face as with_mask or without_mask.
FACE MASK DETECTION DATASET
The dataset we are working on consists of 1376 images with 690 images containing images of people wearing masks and 686 images with people without masks.
I have Downloaded the dataset frem pyimagesearh: Face Mask Dataset
Platform to Start this project
In this project we will use Jupyter Notebook for the development. so, first of all download anconda and install jupyter notebook.
It will start the notebook server at http://localhost:8888
To create a project click on the “new” tab on the right panel, it will generate a new .ipynb file.Create a new file and write the code.
#importing os module and cv2 module
import os
import cv2
os module provides functions for interacting with the operating system. and by using cv2 we can develop real-time computer vision application mainly do inage processing.
#img_to_array i.e converts a PIL image instance to a numpy array
from keras.preprocessing import image
we have two types of dataset so we classifies it into two categories
categories = [‘with_mask’,’without_mask’]
now, join dataset path and read image and put it into variable img
data = [] #Initializing data lists
for category in categories: # loop over the image paths
path = os.path.join(‘dataset’,category) #join path of dataset and
category and put in path label = categories.index(category) #extract the class label from
file name
for file in os.listdir(path): #get the list of all files in
specified directory
img_path = os.path.join(path,file) #join path & file & put in var img = cv2.imread(img_path) #read the img_path and put in img
img = cv2.resize(img,(224,224)) # load the input image (224x224)
and process it.
(224,224) meands vgg take this
size of image.
data.append([img,label]) #Appending the pre-processed image
and associated label to the data and
labels lists.
after this we have to check our data is load or not
len(data) # total number of data with and without mask
now we suffle the data because with and without mask data sequentially then it may develop bias
import randomrandom.shuffle(data)
now we disstribute x and y and then it convert into numpy array
x = []
y = []for features, label in data:
x.append(features) # we have to convert in 2D array
y.append(label) # and we have to convert in 1D array
lenth of x and y will always same
len(x) # check the length of xlen(y) # chech length of y
Import numpy array
import numpy as npx = np.array(x)
y = np.array(y)
now, we can see that shape of X is 2D array and shape of y is 1D array, By
x.shape
y.shape
So, we can see that in x we have 1376 images of 224*224 size and in y we have labels.
Now, we do X train and Y train
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)
now we can see that data in x_train and Y_test
x_train.shapex_test.shape
Finally our data is to be prepared now, we are going tocreate the CNN artitecture, and we do the transfer learning.
from keras.applications.vgg16 import VGG16vgg = VGG16()
our VGG model is imported so now we can see that vgg model by vgg summary
vgg.summary()
In this vgg model we have to remove this last layer and then we add our layer by the use of sequential model, because we know that vgg is the functional model but we required sequential model. so, first of all we have to import sequential.
from keras import Sequentialmodel = Sequential() # create a model sequentialfor layer in vgg.layers[:-1]: # remove last layer of vgg and add
current layer
model.add(layer)
By this method we remove the last layer of vgg model.
model.summary()
now, we freeze all the layers so, that if we train the model then this layer will not do update automatically
for layer in model.layers:
layer.trainable=False
By this all trainable data convert into non-trainable. hence for check we can see the model summary.
model.summary()
So, we can match the previous model summary to new model summary. In previous tainable parameter is non zero but here new model trainable parameter is zero. so our all layer is freeze now. hence,we can’t update it during the trainable process.
Now we have to add the last layer in this model
from keras.layers import Densemodel.add(Dense(1,activation='sigmoid'))
By this process we add the new dense layer with one output node and activation se sigmoid. Hence, we can see that our dense layer is added by model.summary.
model.summary()
Here, we can see that Trainable layer is added from the last layer of our model. so we have both tainable parameter and non-trainable parameter. so we have train this during the training process. this is happen in transfer learning
model.compile(optimizer='Adam',loss='binary_crossentropy',metrics=['accuracy'])
By this method we compiled the model. Then we fit the model and train this and see the accuracy to epoch-5.
model.fit(x_train,y_train,epochs=5,validation_data=(x_test,y_test))
So, our accuracy at Training is 1 i.e 100% and 98% at validation. Now we use this model in video for face detection.
Implementing our model for face mask detector for images with OpenCV
First of all we capture our video i.e open Webcam “0” for local web cam and 1 for external webCam.
cap = cv2.VideoCapture(0)
Now, on this capture video i.e we get continous frame or image so, take this image and put in the model after that model will give the output or say what is detect by webcam ie mask or no mask and which display on screen. So, we make the detect function and do binary classification.
def detect_face_mask(img):
y_pred = model.predict_classes(img.reshape(1,224,224,3))
return y_pred
After that we want to write or show on screen i.e on the capture vedio display that the person is wearing mask or not. so we have to make the function also for this. So, we make the draw label function in which we required (image, text, position where we have to write, and in which color)
def draw_label(img,text,pos,bg_color):
text_size = cv2.getTextSize(text,cv2.FONT_HERSHEY_SIMPLEX,1,cv2.FILLED)
end_x = pos[0] + text_size[0][0] + 2
end_y = pos[1] + text_size[0][1] - 2
cv2.rectangle(img,pos,(end_x,end_y),bg_color,cv2.FILLED)
cv2.putText(img,text,pos,cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),3,cv2.LINE_AA)
Now we use pre trained model i.e Haar Cascade. it is an Object Detection Algorithm used to identify faces in an image or a real time video. The algorithm uses edge or line detection features. so we download the xml file haarcascade_frontalface_default.xml.
haar = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Then, detect the co-ordinates of face i.e left most corner and right most corner of detected rectangle.
def detect_face(img):
coods = haar.detectMultiScale(img)
return coods
Now, we run the while loop for continous capture the face of human for check face mask detection i.e call the detected coordinates and call the draw label for text which we want to write on corner of image .Then our code will run unless our while loop is true by then webcam detect the face, if loop is false then it break the loop and stop detecting face and destroy all windows .
while True:
ret,frame = cap.read()
#call the detection method
img = cv2.resize(frame,(224,224))
y_pred = detect_face_mask(img)
coods = detect_face(cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY))
for x,y,w,h in coods:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),3)
if y_pred ==0:
draw_label(frame,"Mask",(30,30),(0,255,0))
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)
else:
draw_label(frame,"No Mask",(30,30),(0,0,255))
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
cv2.imshow("window",frame)
if cv2.waitKey(10)==27:
break
cv2.destroyAllWindows()
Finally, we can check our face mask detector working properly or not
Our face mask detector has correctly predicted No Mask
As you can see, our face mask detector correctly labeled this image as Mask.