live face mask detection

LIVE Face Mask Detection AI Project from Video & Image

live face mask detection

Project Name – LIVE Face Mask Detection App Project

In this video we know how we can find live face mask with the help of DP Learning, that too with a good accuracy 99%

Training Model using Own CNN Architecture

'''Live Face Mask Detection AI App Project Playlist Video Tutorial: https://www.youtube.com/playlist?list=PLfP3JxW-T70E23QE38M_XM-bFjRiS8z1H '''

# Training CNN model to predict Mask and No Mask  (Images)

# Install Packages
"""

pip install tensorflow==2.1.0

pip install keras==2.3.1

"""# Training model"""

#import libraries
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint

# for accuracy and loss graph
import matplotlib.pyplot as plt

import tensorflow

print(keras.__version__)
print(tensorflow.__version__)

train_data_path = "/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/dataset/train"
validation_data_path = "/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/dataset/valid"

# show augmented images
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20, 20))
    axes = axes.flatten()
    for img, ax in zip(images_arr, axes):
        ax.imshow(img)
    plt.tight_layout()
    plt.show()

# this is the augmentation configuration we will use for training
# It generate more images using below parameters
training_datagen = ImageDataGenerator(rescale=1./255,
                                      rotation_range=40,
                                      width_shift_range=0.2,
                                      height_shift_range=0.2,
                                      shear_range=0.2,
                                      zoom_range=0.2,
                                      horizontal_flip=True,
                                      fill_mode='nearest')

# this is a generator that will read pictures found in
# at train_data_path, and indefinitely generate
# batches of augmented image data
training_data = training_datagen.flow_from_directory(train_data_path, # this is the target directory
                                      target_size=(200, 200), # all images will be resized to 150x150
                                      batch_size=128,
                                      class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels

training_data.class_indices

# this is the augmentation configuration we will use for validation:
# only rescaling
valid_datagen = ImageDataGenerator(rescale=1./255)

# this is a similar generator, for validation data
valid_data = valid_datagen.flow_from_directory(validation_data_path,
                                  target_size=(200,200),
                                  batch_size=128,
                                  class_mode='binary')

# showing augmented images
images = [training_data[0][0][0] for i in range(5)]
plotImages(images)

# save best model using vall accuracy
model_path = '/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/model/model.h5'
checkpoint = ModelCheckpoint(model_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

#Building cnn model
cnn_model = keras.models.Sequential([
                                    keras.layers.Conv2D(filters=32, kernel_size=5, input_shape=[200, 200, 3]),
                                    keras.layers.MaxPooling2D(pool_size=(4,4)),
                                    keras.layers.Conv2D(filters=64, kernel_size=4),
                                    keras.layers.MaxPooling2D(pool_size=(3,3)),
                                    keras.layers.Conv2D(filters=128, kernel_size=3),
                                    keras.layers.MaxPooling2D(pool_size=(2,2)),                                    
                                    keras.layers.Conv2D(filters=256, kernel_size=2),
                                    keras.layers.MaxPooling2D(pool_size=(2,2)),

                                    keras.layers.Dropout(0.5),                                                                        
                                    keras.layers.Flatten(), # neural network beulding
                                    keras.layers.Dense(units=128, activation='relu'), # input layers
                                    keras.layers.Dropout(0.1),                                    
                                    keras.layers.Dense(units=256, activation='relu'),                                    
                                    keras.layers.Dropout(0.25),                                    
                                    keras.layers.Dense(units=2, activation='softmax') # output layer
])


# compile cnn model
cnn_model.compile(optimizer = Adam(lr=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
#cnn_model.compile(optimizer = Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])

# train cnn model
history = cnn_model.fit(training_data, 
                          epochs=50, 
                          verbose=1, 
                          validation_data= valid_data,
                          callbacks=callbacks_list) # time start 14.25

cnn_model.save('/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/model/model_last.h5')

# train cnn model
history = cnn_model.fit(training_data, 
                          epochs=50, 
                          verbose=1, 
                          validation_data= valid_data,
                          callbacks=callbacks_list) # time start 14.25

cnn_model.save('/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/model/model_last.h5')

import matplotlib.pyplot as plt
# plot the loss
plt.plot(cnn_model.history['loss'], label='train loss')
plt.plot(cnn_model.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')

# plot the accuracy
plt.plot(cnn_model.history['accuracy'], label='train acc')
plt.plot(cnn_model.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')

Training Model using Resnet152V2 CNN Architecture

import datetime
datetime.datetime.now()

pip install tensorflow==2.1.0

pip install keras==2.3.1

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

# import the libraries as shown below

from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
import numpy as np
from glob import glob
import matplotlib.pyplot as plt

import tensorflow
import keras
print(tensorflow.__version__)
print(keras.__version__)

# re-size all the images to this
IMAGE_SIZE = [224, 224]

train_path = "/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/dataset/train"
valid_path  = "/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/dataset/valid"

# Import the ResNet152V2 library as shown below and add preprocessing layer to the front of ResNet152V2
# Here we will be using imagenet weights
import tensorflow
resnet152V2 =tensorflow.keras.applications.ResNet152V2(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

# don't train existing weights
for layer in resnet152V2.layers:
    layer.trainable = False

# useful for getting number of output classes
folders = glob('/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/dataset/train/*')

# our layers - you can add more if you want
x = Flatten()(resnet152V2.output)

prediction = Dense(len(folders), activation='softmax')(x)

# create a model object
model = Model(inputs=resnet152V2.input, outputs=prediction)

# view the structure of the model
model.summary()

# tell the model what cost and optimization method to use
model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

# Use the Image Data Generator to import the images from the dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

# Make sure you provide the same target size as initialied for the image size
training_set = train_datagen.flow_from_directory(train_path,
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory(valid_path,
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'categorical')

# save best model using vall accuracy
model_path = '/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/model/resnet152v2.h5'
checkpoint = ModelCheckpoint(model_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

# fit the model
# Run the cell. It will take some time to execute
r = model.fit(
  training_set,
  validation_data=test_set,
  epochs=10,
  steps_per_epoch=len(training_set),
  validation_steps=len(test_set),
  callbacks=callbacks_list
)

from tensorflow.keras.models import load_model

model.save('/content/drive/My Drive/My ML Project /DL Project/CNN/mask detection/model/last_resnet152v2.h5')

import matplotlib.pyplot as plt
# plot the loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')

# plot the accuracy
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')

datetime.datetime.now()

Live Face Mask Detection App fron Video

#Transfer learning - resnet152V2 .ipynb

# import packages
import cv2
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np

#load model # Accuracy=99.3 , validation Accuracy = 99.3 # heavy model, size =226MB
model_res = load_model('model/resnet152v2_acc_vacc_99_3.h5') #resnet transfer learning

# model accept below hight and width of the image
img_width, img_hight = 224, 224

#------------------------------or-----------------------

#Own cnn  architecture - mask detection CNN.ipynb

# import packages
import cv2
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np

#load model # Accuracy=97.4 , validation Accuracy = 99.1 # very light model, size =5MB
model = load_model('model/model_acc_974_vacc_991.h5') # cnn

# model accept below hight and width of the image
img_width, img_hight = 200, 200

#......................................


# Load the Cascade face Classifier
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#startt  web cam
cap = cv2.VideoCapture(0) # for webcam
#cap = cv2.VideoCapture('videos/Mask - 34775.mp4') # for video

img_count_full = 0

#parameters for text
# font 
font = cv2.FONT_HERSHEY_SIMPLEX 
# org 
org = (1, 1)
class_lable=' '      
# fontScale 
fontScale = 1 #0.5
# Blue color in BGR 
color = (255, 0, 0) 
# Line thickness of 2 px 
thickness = 2 #1

#sart reading images and prediction
while True:
    img_count_full += 1
    
    #read image from webcam
    responce, color_img = cap.read()
    #color_img = cv2.imread('sandeep.jpg')
    
    #if respoce False the break the loop
    if responce == False:
        break    
    
    # Convert to grayscale
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
    
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray_img, 1.1, 6) # 1.1, 3) for 1.mp4
    
    #take face then predict class mask or not mask then draw recrangle and text then display image
    img_count = 0
    for (x, y, w, h) in faces:
        org = (x-10,y-10)
        img_count +=1 
        color_face = color_img[y:y+h,x:x+w] # color face
        cv2.imwrite('faces/input/%d%dface.jpg'%(img_count_full,img_count),color_face)
        img = load_img('faces/input/%d%dface.jpg'%(img_count_full,img_count), target_size=(img_width,img_hight))
        
        img = img_to_array(img)/255
        img = np.expand_dims(img,axis=0)
        pred_prob = model.predict(img)
        #print(pred_prob[0][0].round(2))
        pred=np.argmax(pred_prob)
            
        if pred==0:
            print("User with mask - predic = ",pred_prob[0][0])
            class_lable = "Mask"
            color = (255, 0, 0)
            cv2.imwrite('faces/with_mask/%d%dface.jpg'%(img_count_full,img_count),color_face)
                 
        else:
            print('user not wearing mask - prob = ',pred_prob[0][1])
            class_lable = "No Mask"
            color = (0, 255, 0)
            cv2.imwrite('faces/without_mask/%d%dface.jpg'%(img_count_full,img_count),color_face)
                
        cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
        # Using cv2.putText() method 
        cv2.putText(color_img, class_lable, org, font,  
                                   fontScale, color, thickness, cv2.LINE_AA) 
    
    # display image
    cv2.imshow('LIVE face mask detection', color_img)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

Live Face Mask Detection App From Images

Live Face Mask Detection App From Single Images

#Own cnn  architecture - mask detection CNN.ipynb

# import packages
import cv2
import os
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np

#load model # Accuracy=97.4 , validation Accuracy = 99.1 # very light model, size =5MB
model = load_model('model/model_acc_974_vacc_991.h5') # cnn

# model accept below hight and width of the image
img_width, img_hight = 200, 200


# Load the Cascade face Classifier
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#parameters for text
# font 
font = cv2.FONT_HERSHEY_SIMPLEX 
# org 
org = (1, 1)
class_lable=' '      
# fontScale 
fontScale = 1 #0.5
# Blue color in BGR 
color = (255, 0, 0) 
# Line thickness of 2 px 
thickness = 2 #1

#read image from webcam
color_img = cv2.imread('images/mask-5136259_1280.jpg')
    
# Convert to grayscale
gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
    
# Detect the faces
faces = face_cascade.detectMultiScale(gray_img, 1.1, 6) 
    
#take face then predict class mask or not mask then draw recrangle and text then display image
img_count = 0
for (x, y, w, h) in faces:
    org = (x-10,y-10)
    img_count +=1 
    color_face = color_img[y:y+h,x:x+w] # color face
    cv2.imwrite('faces/input/%dface.jpg'%(img_count),color_face)
    img = load_img('faces/input/%dface.jpg'%(img_count), target_size=(img_width,img_hight))
        
    img = img_to_array(img)/255
    img = np.expand_dims(img,axis=0)
    pred_prob = model.predict(img)
    #print(pred_prob[0][0].round(2))
    pred=np.argmax(pred_prob)
            
    if pred==0:
        print("User with mask - predic = ",pred_prob[0][0])
        class_lable = "Mask"
        color = (255, 0, 0)
        cv2.imwrite('faces/with_mask/%dface.jpg'%(img_count),color_face)
        cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
        # Using cv2.putText() method 
        cv2.putText(color_img, class_lable, org, font,  
                                   fontScale, color, thickness, cv2.LINE_AA) 
        cv2.imwrite('faces/with_mask/%dmask.jpg'%(img_count),color_img)

    else:
        print('user not wearing mask - prob = ',pred_prob[0][1])
        class_lable = "No Mask"
        color = (0, 255, 0)
        cv2.imwrite('faces/without_mask/%dface.jpg'%(img_count),color_face)
        cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
        # Using cv2.putText() method 
        cv2.putText(color_img, class_lable, org, font,  
                                   fontScale, color, thickness, cv2.LINE_AA)         
        cv2.imwrite('faces/with_mask/%dno_mask.jpg'%(img_count),color_img)
                

    
# display image
cv2.imshow('LIVE face mask detection', color_img)
    
cv2.waitKey() 

#close all windows
cv2.destroyAllWindows()

Live Face Mask Detection App From Multile Images

#Own cnn  architecture - mask detection CNN.ipynb

# import packages
import cv2
import os
from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np

#load model # Accuracy=97.4 , validation Accuracy = 99.1 # very light model, size =5MB
model = load_model('model/model_acc_974_vacc_991.h5') # cnn

# model accept below hight and width of the image
img_width, img_hight = 200, 200

# Load the Cascade face Classifier
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#parameters for text
# font 
font = cv2.FONT_HERSHEY_SIMPLEX 
# org 
org = (1, 1)
class_lable=' '      
# fontScale 
fontScale = 1 #0.5
# Blue color in BGR 
color = (255, 0, 0) 
# Line thickness of 2 px 
thickness = 2 #1

img_path = os.path.join('images')
img_list = os.listdir(img_path)

for img_name in img_list:
    #read image from webcam
    color_img = cv2.imread('images/%s'%img_name)
    
    #resize image with 50 % ratio
    scale = 20  
    width = int(color_img.shape[1] * scale / 100)  
    height = int(color_img.shape[0] * scale / 100)  
    dim = (width, height)  
    # resize image  
    color_img = cv2.resize(color_img, dim, interpolation=cv2.INTER_AREA) 

    # Convert to grayscale
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)

    # Detect the faces
    faces = face_cascade.detectMultiScale(gray_img, 1.1, 6) 

    #take face then predict class mask or not mask then draw recrangle and text then display image
    img_count = 0
    for (x, y, w, h) in faces:
        org = (x-10,y-10)
        img_count +=1 
        color_face = color_img[y:y+h,x:x+w] # color face
        cv2.imwrite('faces/input/%dface.jpg'%(img_count),color_face)
        img = load_img('faces/input/%dface.jpg'%(img_count), target_size=(img_width,img_hight))

        img = img_to_array(img)/255
        img = np.expand_dims(img,axis=0)
        pred_prob = model.predict(img)
        #print(pred_prob[0][0].round(2))
        pred=np.argmax(pred_prob)

        if pred==0:
            print("User with mask - predic = ",pred_prob[0][0])
            class_lable = "Mask"
            color = (255, 0, 0)
            cv2.imwrite('faces/with_mask/%dface.jpg'%(img_count),color_face)
            cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
            # Using cv2.putText() method 
            cv2.putText(color_img, class_lable, org, font,  
                                   fontScale, color, thickness, cv2.LINE_AA) 
            cv2.imwrite('faces/with_mask/%dmask.jpg'%(img_count),color_img)

        else:
            print('user not wearing mask - prob = ',pred_prob[0][1])
            class_lable = "No Mask"
            color = (0, 255, 0)
            cv2.imwrite('faces/without_mask/%dface.jpg'%(img_count),color_face)
            cv2.imwrite('faces/with_mask/%dno_mask.jpg'%(img_count),color_img)

        cv2.rectangle(color_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
        # Using cv2.putText() method 
        cv2.putText(color_img, class_lable, org, font,  
                                       fontScale, color, thickness, cv2.LINE_AA) 

    # display image
    cv2.imshow('LIVE face mask detection', color_img)

    cv2.waitKey() 

    #close all windows
    cv2.destroyAllWindows()

Download Full Project with model training files, Dataset, Deployment Project, models

Leave a Reply