Deep Learning

Neural Network to identify face without mask

I am writing this article in the amid of Covid 19 outbreak. Lot many people are suffering of this diseases and lot many lost their life. Social distancing is one of the key that people are using to avoid this disease as much as possible. As well as mask and sanitisers becomes our daily need. Wearing masks helping people to avoid this virus to get a way through our breathing systems. Here a AI based image recognition process had been discussed to identify if one is wearing a mask or not. It can be deployed at places where public gathering used to happen like shopping complex, coaching classes or offices to let the people in only if he/she had wear mask.

So Here we start……

Let’s create a function that can create a directory to store training and testing data. Using it we will create directories to store two types of images. One with mask and next without mask.

def makedir(directory):
    if not os.path.exists(directory): 
        os.makedirs(directory) 
        return None
    else:
        pass

Capture the image using opencv as shown below. We can defined a region of interest where person can put there face to be captured and the captured face will be stored in the directory defined as faces. Two directories had been defined namely train and test containing two sub directories as 0 and 1. In 0 we are having faces without any mask and directory 1 is containing faces with mask.

import cv2
import os

cap = cv2.VideoCapture(0)

i = 0
image_count = 0

while i < 6:
    ret,frame = cap.read()
    frame = cv2.flip(frame,1)

  #ROI
    roi = frame[100:400,220:520]
    cv2.imshow('roi',roi)
    roi = cv2.cvtColor(roi , cv2.COLOR_BGR2GRAY)
    roi = cv2.resize(roi , (28,28) , interpolation = cv2.INTER_AREA)

    cv2.imshow('ROI_GRAY' , roi)
    copy = frame.copy()
    cv2.rectangle(copy , (220,100) , (520,400) , (255,0,0) , 5)

    if i == 0:
        image_count = 0
        cv2.putText(copy, 'Press Enter to record 1st object' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
    if i == 1:
        image_count += 1
        cv2.putText(copy, 'Recording 1st object - Train Dataset' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        cv2.putText(copy, str(image_count) , (300,400) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        faces = 'E:/ML_Codes/Mask/train/0/'
        makedir(faces)
        cv2.imwrite(faces + str(image_count) + ".jpg" , roi)
    if i == 2:
        image_count += 1
        cv2.putText(copy, 'Recording 1st object - Test Dataset' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        cv2.putText(copy, str(image_count) , (300,400) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        faces = 'E:/ML_Codes/Mask/test/0/'
        makedir(faces)
        cv2.imwrite(faces + str(image_count) + ".jpg" , roi)
  
    if i == 3:
        image_count = 0
        cv2.putText(copy, 'Press Enter to record 2nd object' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
    if i == 4:
        image_count += 1
        cv2.putText(copy, 'Recording 2nd object - Train Dataset' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        cv2.putText(copy, str(image_count) , (300,400) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        faces = 'E:/ML_Codes/Mask/train/1/'
        makedir(faces)
        cv2.imwrite(faces + str(image_count) + ".jpg" , roi)
    if i == 5:
        image_count += 1
        cv2.putText(copy, 'Recording 2nd object - Test Dataset' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        cv2.putText(copy, str(image_count) , (300,400) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
        faces = 'E:/ML_Codes/Mask/test/1/'
        makedir(faces)
        cv2.imwrite(faces + str(image_count) + ".jpg" , roi)
    if i == 6:
        cv2.putText(copy, 'Hit Enter to Exit' , (50,100) , cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,0),1)
    cv2.imshow('frame' , copy)

    if cv2.waitKey(1) == 13:
        image_count = 0
        i += 1
cap.release()
cv2.destroyAllWindows()

Import the tensor flow libraries to define a CNN based neural network.

import tensorflow
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import numpy as np
import os

Define your CNN based architecture for training your model.

model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation='relu' , input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.20))

model.add(Dense(1, activation = 'sigmoid'))

print(model.summary())

Use of Open CV to extract the features and labels of images within train folder to train a model.

import cv2
labels = []
features = []
import os
for i in os.listdir('E:/ML_Codes/Mask/train/0'):
    labels.append(0)
for i in os.listdir('E:/ML_Codes/Mask/train/1'):
    labels.append(1)
    
for i in os.listdir('E:/ML_Codes/Mask/train/0'):
    features.append(cv2.imread(os.path.join('E:/ML_Codes/Mask/train/0',i),0))
for i in os.listdir('E:/ML_Codes/Mask/train/1'):
    features.append(cv2.imread(os.path.join('E:/ML_Codes/Mask/train/1',i),0))

Extract features and labels of images within test directory to validate your model.

test_labels = []
test_features = []
import os
for i in os.listdir('E:/ML_Codes/Mask/test/0'):
    test_labels.append(0)
for i in os.listdir('E:/ML_Codes/Mask/test/1'):
    test_labels.append(1)
    
for i in os.listdir('E:/ML_Codes/Mask/test/0'):
    test_features.append(cv2.imread(os.path.join('E:/ML_Codes/Mask/test/0',i),0))
for i in os.listdir('E:/ML_Codes/Mask/test/1'):
    test_features.append(cv2.imread(os.path.join('E:/ML_Codes/Mask/test/1',i),0))

Resize and rescale your extracted features for smooth training through CNN based model.

import numpy as np
features = np.array(features).reshape(-1,28,28,1)
test_features = np.array(test_features).reshape(-1,28,28,1)

features = features/255
test_features = test_features/255

labels = np.array(labels)
test_labels = np.array(test_labels)

Compile your model and fit the training data into it which will let the machine learn. We will use binary cross entropy as loss function and rmsprop will be our optimiser.

model.compile(loss = 'binary_crossentropy',
              optimizer = 'rmsprop',
              metrics = ['accuracy'])

epochs = 20
batch_size = 32

model.fit(features, labels, batch_size=batch_size, 
          steps_per_epoch=features.shape[0] // batch_size,
          epochs=40,
          verbose=1,validation_data=(test_features,test_labels))

Once the training of model is finished you can save the model for any future deployments.

model.save('Mask.h5')

from tensorflow.keras.models import load_model
classifier = load_model('Mask.h5')

Define a function which can return label once the predicted value is passed through it. As prediction will be in terms of 0 and 1 which can be encoded into ‘No Mask’ or ‘Mask’ for notification to the user.

def getLabel(result):
    classLabels = {0: 'No Mask',
                   1: 'Mask'}
    try:
        res = int(result)
        return classLabels[res]
    except:
        return 'Error'

Test your model in real time by capturing the image using Open CV. It will generate the output upon predictions as given below.

import cv2
cap = cv2.VideoCapture(0)

while True:
    ret,frame = cap.read()

    frame = cv2.flip(frame,1)

    #region of interest
    roi = frame[100:400 , 220:520]
    cv2.imshow('roi',roi)
    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    roi = cv2.resize(roi, (28,28), interpolation = cv2.INTER_AREA)

    #cv2.imshow('roi scaled and gray' , roi)
    copy = frame.copy()
    cv2.rectangle(copy, (220,100) , (520,400) , (255,0,255) , 5)

    roi = roi.reshape(1,28,28,1)
    roi = roi/255
    result = (model.predict(roi) > 0.5).astype("int32")

    cv2.putText(copy,getLabel(result),(150,100),cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),2)
    cv2.imshow('frame',copy)
    print(result)

    if cv2.waitKey(1) == 13:
        break

cap.release()
cv2.destroyAllWindows()

Thanks for reading it and just give it a try. It pretty easy to create a deep neural network application. Suggestions are always welcome. Pen down your comments. Keep Learning

Leave a Reply

Your email address will not be published. Required fields are marked *