GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials || AI || ML || NLP ||  OpenCV || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
0 like 0 dislike
732 views
in AI-ML-Data Science Projects by (135 points)
edited by

COVID-19: Face Mask Detection using TensorFlow and OpenCV

Abridgement-The most recent recommendations related to prevantion against the spread of COVID19 include wearing  face mask as an added measure of protection. The Face Mask Detection System can be used at airports,hospitals,offices etc to detect people without masks. Face data of people can be captured in the system at the entrance so that authorities could take quick action and also can monitor if their staff is wearing masks during their shift or not. If quarantine people who are required to wear a mask, the system can keep an eye and detect if the mask is present or not and send notification automatically or report to the authorities.

3 Answers

0 like 0 dislike
by (135 points)
edited by
 
Best answer

FACE-MASK DETECTION

[Part1]

Purpose of the model- I decided to build a Convolutional Neural Network (CNN) model using TensorFlow with Keras library and OpenCV to detect if you are wearing a face mask or not.Our main focus is to detect whether a person is wearing a mask or not, without getting close to them. Wearing a face-mask is one of the prevention measures to limit spread of certain respiratory diseases,like Covid19. The Face Mask Detection System can be used at offices premises,schools,hospitals,bank etc to detect if employees are maintaining safety standards at work. The system can keep an eye and detect if the mask is present or not and send notification automatically or report to the authorities.

                                                                                     

Dataset-The dataset of  images we’ll be using here today was created by  Prajna Bhandary 

dataset without mask                                                             

 

dataset with mask

 Download source code and dataset

This dataset consists of 1,376 images in which the number of images with facemask : 690 and without facemask is 686.

Step1:Data Pre Processing

import cv2,os
import numpy as np
from keras.utils import np_utils
 
data_path='C:\\Users\\HP\\Pictures\\face-mask-detector\\dataset'
categories=os.listdir(data_path)
labels=[i for i in range(len(categories))]
label_dict=dict(zip(categories,labels))
print(label_dict)
print(categories)
print(labels)
data=[]
target=[]
{'without_mask': 0, 'with_mask': 1}
['without_mask', 'with_mask']
[0, 1]

Step 2: Converting images into grayscale images

The dataset we are using consists of images with different colors, different sizes, and different orientations. Therefore, we need to convert all the images into grayscale because we need to be sure that color should not be a critical point for detecting mask. After that, we need to have all the images in the same size (100x100) before applying it to the neural network. 

for category in categories:
folder_path=os.path.join(data_path,category)
 img_names=os.listdir(folder_path)
for img_name in img_names:
mg_path=os.path.join(folder_path,img_name)
 img=cv2.imread(img_path)
try:
 gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
#Coverting the image into gray scale
resized=cv2.resize(gray,(100,100))
#resizing the gray scale into 100x100, since we need a fixed common size for all the images in the dataset
data.append(resized)
target.append(label_dict[category])
#appending the image and the label(categorized) into the list (dataset)
except Exception as e:
 print('Exception:',e)
 #if any exception rasied, the exception will be printed here. And pass to the next image

Step3:Loading the saved numpy arrays 

import numpy as np
data=np.array(data)/255.0

data=np.reshape(data,(data.shape[0],60,60,1))
target=np.array(target)
new_target=np_utils.to_categorical(target)
np.save('data',data)   
np.save('target',new_target)
data=np.load('data.npy')

target=np.load('target.npy')

Step4:Building Sequential Model

Now,we build our Sequential CNN model with various layers such as Conv2D, MaxPooling2D, Flatten, Dropout and Dense. In the last Dense layer, we use the ‘softmax’ function to output a vector that gives the probability of each of the two classes.Since we have two categories(with mask and without mask) we can use binary_crossentropy.

from keras.models import Sequential
from keras.layers import Dense,Activation,Flatten,Dropout
from keras.layers import Conv2D,MaxPooling2D
from keras.callbacks import ModelCheckpoint
model=Sequential()
model.add(Conv2D(100,(3,3),input_shape=data.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#The first CNN layer followed by Relu and MaxPooling layers
model.add(Conv2D(100,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#The second convolution layer followed by Relu and MaxPooling layers
model.add(Dropout(0.5))
#Flatten layer to stack the output convolutions from second convolution layer
model.add(Dense(50,activation='relu'))
#Dense layer of 64 neurons
model.add(Dense(2,activation='softmax'))
#The Final layer with two outputs for two categories
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
0 like 0 dislike
by (135 points)
edited by

FACE-MASK DATASET [Part2]

Step5:Training the CNN Model

 In this step we fit our images in the training set and the test set to our Sequential model we built using keras library. I have trained the model for 20 epochs (iterations). However, we can train for more number of epochs to attain higher accuracy lest there occurs over-fitting.

from sklearn.model_selection import train_test_split
train_data,test_data,train_target,test_target=train_test_split(data,target,test_size=0.1)
checkpoint=ModelCheckpoint('model{epoch:03d}.model', monitor='val_loss',verbose=0,save_best_only=True,mode='auto')
history=model.fit(train_data,train_target,epochs=20, callbacks=[checkpoint], validation_split=0.2)

Train on 990 samples, validate on 248 samples

Epoch 1/20
990/990 [==============================] - 24s 24ms/step - loss: 0.7455 
- accuracy: 0.5636 - val_loss: 0.6569 - val_accuracy: 0.5161
Epoch 2/20
990/990 [==============================] - 26s 26ms/step - loss: 0.5443
 - accuracy: 0.7323 - val_loss: 0.4363 - val_accuracy: 0.8145
Epoch 3/20
990/990 [==============================] - 23s 23ms/step - loss: 0.3874
 -accuracy: 0.8313 - val_loss: 0.3551 - val_accuracy: 0.8548
Epoch 4/20
990/990 [==============================] - 22s 22ms/step - loss: 0.3384 
- accuracy: 0.8404 - val_loss: 0.3016 - val_accuracy: 0.8669
Epoch 5/20
990/990 [==============================] - 22s 22ms/step - loss: 0.2287 
- accuracy: 0.9192 - val_loss: 0.2209 - val_accuracy: 0.9113
Epoch 6/20
990/990 [==============================] - 21s 21ms/step - loss: 0.1569 
- accuracy: 0.9414 - val_loss: 0.1613 - val_accuracy: 0.9435
Epoch 7/20
990/990 [==============================] - 21s 22ms/step - loss: 0.1392
 - accuracy: 0.9414 - val_loss: 0.2222 -val_accuracy: 0.9234
Epoch 8/20
990/990 [==============================] - 21s 21ms/step - loss: 0.1310 
- accuracy: 0.9485 - val_loss: 0.1371 - val_accuracy: 0.9476
Epoch 9/20
990/990 [==============================] - 21s 21ms/step - loss: 0.0595 
- accuracy: 0.9848 - val_loss: 0.1476 - val_accuracy: 0.9395
Epoch 10/20
990/990 [==============================] - 21s 21ms/step - loss: 0.0687 
- accuracy: 0.9778 - val_loss: 0.1597 - val_accuracy: 0.9435
Epoch 11/20

                              
990/990 [==============================] - 21s 21ms/step - loss: 0.0657 
- accuracy: 0.9798 - val_loss: 0.1721 - val_accuracy: 0.9435
Epoch 12/20
990/990 [==============================] - 21s 21ms/step - loss: 0.0397 
- accuracy: 0.9869 - val_loss: 0.1883 - val_accuracy: 0.9395
Epoch 13/20
990/990 [==============================] - 21s 22ms/step - loss: 0.0415 
- accuracy: 0.9899 - val_loss: 0.2109 - val_accuracy: 0.9435
Epoch 14/20
990/990 [==============================] - 21s 21ms/step - loss: 0.0415
 - accuracy: 0.9838 - val_loss: 0.2083 - val_accuracy: 0.9395
Epoch 15/20
990/990 [==============================] - 23s 23ms/step - loss: 0.0314 
- accuracy: 0.9879 - val_loss: 0.1692 - val_accuracy: 0.9516
Epoch 16/20
990/990 [==============================] - 24s 25ms/step - loss: 0.0296 
- accuracy: 0.9899 - val_loss: 0.2530 - val_accuracy: 0.9435
Epoch 17/20
990/990 [==============================] - 24s 25ms/step - loss: 0.0320 
- accuracy: 0.9909 - val_loss: 0.1938 - val_accuracy: 0.9476
Epoch 18/20
990/990 [==============================] - 22s 23ms/step - loss: 0.0227 
- accuracy: 0.9939 - val_loss: 0.2387 - val_accuracy: 0.9395
Epoch 19/20
990/990 [==============================] - 21s 21ms/step - loss: 0.0194 
- accuracy: 0.9939 - val_loss: 0.2049 - val_accuracy: 0.9476
Epoch 20/20
990/990 [==============================] - 21s 22ms/step - loss: 0.0214 
- accuracy: 0.9919 - val_loss: 0.2226 - val_accuracy: 0.9476

 Step6:In this step we plot our accuracy and loss curves

from matplotlib import pyplot as plt
plt.plot(history.history['loss'],'r',label='training loss')
plt.plot(history.history['val_loss'],label='validation loss')
plt.xlabel('# epochs')
plt.ylabel('loss')
plt.legend()
plt.show()

plt.plot(history.history['accuracy'],'r',label='training accuracy')
plt.plot(history.history['val_accuracy'],label='validation accuracy')
plt.xlabel('# epochs')
plt.ylabel('loss'
plt.legend()
plt.show()
print(model.evaluate(test_data,test_target))

Step7Loading the model

The Keras deep learning framework supports saving trained model and loading them for later use.It will predict the possibility of  the two classes ([without_mask, with_mask]). Based on whose probability is higher, the label will be chosen and displayed around  faces.

from keras.models import load_model

import cv2

import numpy as np

model = load_model('C:\\Users\\HP\\downloads\\model-017.model')

Step8:Importing the Face detection Program

To detect if we are wearing a face mask using our PC’s webcam for this, we need to implement face detection. In this, we use the Haar Feature-based Cascade Classifiers for detecting the features of the face.

face_clsfr=cv2.CascadeClassifier('C:\\Users\\HP\\Pictures\\Face-Mask-Detector-master\\haarcascade_frontalface_default.xml')

source=cv2.VideoCapture(0)

0 like 0 dislike
by (135 points)
edited by

FACE-MASK DATASET[Part3]

Step9:Detecting Faces with and without Masks

We need to label the two probabilities (0 for with_mask and 1 for without_mask).After that, we need to set the bounding rectangle color using RGB values. I’ve given RED and GREEN as two colors.In the last step, we use the OpenCV library to run an infinite loop to use our web camera in which we detect the face using the Cascade Classifier. The code webcam = cv2.VideoCapture(0)denotes the usage of webcam.The model will predict the possibility of each of the two classes ([without_mask, with_mask]). Based on which probability is higher, the label will be chosen and displayed around our faces.

labels_dict={0:'with_mask',1:'without_mask'}

color_dict={0:(0,255,0),1:(0,0,255)}

while(True):

 ret,img=source.read()

gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_clsfr.detectMultiScale(gray,1.3,5)

 for x,y,w,h in faces:

face_img=gray[y:y+w,x:x+w]

 resized=cv2.resize(face_img,(100,100))

normalized=resized/255.0

reshaped=np.reshape(normalized,(1,100,100,1))

 result=model.predict(reshaped)

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

cv2.rectangle(img,(x,y),(x+w,y+h),color_dict[label],2)

cv2.rectangle(img,(x,y-40),(x+w,y),color_dict[label],-1)

cv2.putText(img, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)

cv2.imshow('LIVE',img)

key=cv2.waitKey(1)

if(key==27):

break

cv2.destroyAllWindows()

source.release()

NOTE: you can download the DroidCam application for both Mobile and PC to use your mobile’s camera and change the value from 0 to 1 in webcam= cv2.VideoCapture(1).

you can view face mask detection in real time video stream:

WITH_MASK                                                                                                                            

 WITH MASK   

WITHOUT_MASK

      

This project is done by DIVYA TANWAR of Government Women Engineering College, Ajmer.

I have done this project during online training in Artificial Intelligence and Deep Learning in the guidence of ma'am Sharda Godara at Goeduhub Technologies ,Jaipur.

Related questions

 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISRO List of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...