School of Computer Science & Engineering
BCSE306L –Artificial Intelligence– C1 Slot – WIN 2023 -24
Project Report Source Code
DA-2
Project Title : Face Mask Detection using different algorithms
Review Number : II
Date of Submission : 27th March 2024
Members
1. Diya Das (21BCE5081)
2. Armaan Saini (21BCE5115)
Faculty In-charge:
Dr. Abirami S
Professor
SCOPE
Code for the normal CNN layers Implemented manually
# %%
## Importing libraries
import pandas as pd
import numpy as np
import cv2
import os
import glob
from [Link] import distance
from sklearn.model_selection import train_test_split
from [Link] import classification_report, confusion_matrix
import tensorflow as tf
from [Link] import Sequential, models
from [Link] import Flatten, Dense, Conv2D, MaxPool2D
from [Link] import ImageDataGenerator
import [Link] as plt
import seaborn as sns
# %%
path = "kaggle/input/face-mask-12k-images-dataset/Face Mask Dataset/"
# %%
dataset = {
"image_path": [],
"mask_status": [],
"where": []
}
for where in [Link](path):
for status in [Link](path+"/"+where):
for image in [Link](path+where+"/"+status+"/"+"*.png"):
dataset["image_path"].append(image)
dataset["mask_status"].append(status)
dataset["where"].append(where)
dataset = [Link](dataset)
[Link]()
# %%
## Choosing a random image to detect the face in the image
face_model =
[Link]('../input/haarcascades/haarcascade_frontalface_default.x
ml')
## Choosing the image from the directory
img = [Link]("../input/face-mask-detection/images/[Link]")
## Converting the image to grayscale to apply haarcascade algorithm
img = [Link](img, cv2.IMREAD_GRAYSCALE)
## Returns the x, y, w, h co-ordinates as numpy arrays for all the detected
faces
detected_face = face_model.detectMultiScale(img)
## Converting from grayscale to colored image
output_img = [Link](img, cv2.COLOR_RGB2BGR)
## Drawing rectangle box around the faces in the image
for (x, y, w, h) in detected_face:
[Link](output_img, (x,y), (x+w, y+h), (0, 0, 200), 2)
## Displaying the image
[Link](figsize = (15, 15))
[Link](output_img)
# %%
## Checking for total number of images in the dataset
print(f"With Mask:", dataset.value_counts("mask_status")[0])
print(f"Without Mask:", dataset.value_counts("mask_status")[1])
## Plotting the numbers
[Link](x = dataset["mask_status"])
# %%
[Link](figsize = (15, 10))
for i in range(9):
random = [Link](1, len(dataset))
[Link](3, 3, i+1)
[Link]([Link]([Link][random,"image_path"]))
[Link]([Link][random,"mask_status"], size = 15)
[Link]([])
[Link]([])
[Link]()
# %%
## Splitting train test and Validation Dataset
train_df = dataset[dataset["where"] == "Train"]
test_df = dataset[dataset["where"] == "Test"]
valid_df = dataset[dataset["where"] == "Validation"]
print(train_df.head(10))
## Shuffling the dataset
train_df = train_df.sample(frac = 1)
test_df = test_df.sample(frac = 1)
valid_df = valid_df.sample(frac = 1)
print("\n After Shuffling \n")
print(train_df.head(10))
# %%
[Link](figsize = (15, 5))
[Link](1, 3, 1)
[Link](x = train_df["mask_status"])
[Link]("Training Dataset", size = 10)
[Link](1, 3, 2)
[Link](x = test_df["mask_status"])
[Link]("Test Dataset", size = 10)
[Link](1, 3, 3)
[Link](x = valid_df["mask_status"])
[Link]("Validation Dataset", size = 10)
[Link]()
# %%
train_df = train_df.reset_index().drop("index", axis = 1)
train_df.head()
# %%
## Reading all the image into a list and changing the size of the image to
(150,150)
data = []
image_size = 150
for i in range(len(train_df)):
## Converting the image into grayscale
img_array = [Link](train_df["image_path"][i], cv2.IMREAD_GRAYSCALE)
## Resizing the array
new_image_array = [Link](img_array, (image_size, image_size))
##Encoding the image with the label
if train_df["mask_status"][i] == "WithMask":
[Link]([new_image_array, 1])
else:
[Link]([new_image_array, 0])
# %%
data = [Link](data,dtype=object)
# %%
data[0][0].shape
# %%
## Shuffling the data to make sure everything is not in order
[Link](data)
# %%
## Looking at the training samples
fig, ax = [Link](2, 3, figsize=(10, 10))
for row in range(2):
for col in range(3):
image_index = row*100+col
ax[row, col].axis("off")
ax[row,col].imshow(data[image_index][0], cmap = "gray")
if data[image_index][1] == 0:
ax[row, col].set_title("Without Mask")
else:
ax[row, col].set_title("With Mask")
[Link]()
# %%
X = []
y = []
## Seperating X and y
for image in data:
[Link](image[0])
[Link](image[1])
## Converting X and y to numpy array as Tensorflow accepts only numpy arrays
X = [Link](X)
y = [Link](y)
# %%
### Normalizing the data
X = X/255
### Train Test Split
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2,
random_state = 42)
# %%
model = Sequential()
[Link](Conv2D(64, (3, 3), activation = "relu"))
[Link](Conv2D(64, (3, 3), activation = "relu"))
[Link](MaxPool2D(pool_size=(3, 3)))
[Link](Flatten())
[Link](Dense(128, activation = "relu"))
[Link](Dense(1, activation = "sigmoid"))
# [Link]()
# %%
[Link](optimizer='adam',
loss=[Link](),
metrics=['accuracy'])
# %%
# X_train = X_train.reshape(-1, 32, 150, 150)
## Reshaping training set to match Conc2D
X_train = X_train.reshape(len(X_train), X_train.shape[1], X_train.shape[2], 1)
X_val = X_val.reshape(len(X_val), X_val.shape[1], X_val.shape[2], 1)
history = [Link](X_train, y_train, epochs=5, batch_size = 32)
# %%
[Link]()
# %%
def visualize_data(history):
# Plotting accuracy
[Link]([Link]['accuracy'])
[Link]('Model Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link](['Train', 'Validation'], loc='upper left')
[Link]()
# Plotting loss
[Link]([Link]['loss'])
[Link]('Model Loss')
[Link]('Epoch')
[Link]('Loss')
[Link](['Train', 'Validation'], loc='upper left')
[Link]()
visualize_data(history)
# %%
prediction = ([Link](X_val) > 0.5).astype("int32")
# %%
print(classification_report(y_val, prediction))
print(confusion_matrix(y_val, prediction))
Code for the VGG algorithm implemented.
# %%
# %%
## Importing libraries
import pandas as pd
import numpy as np
import cv2
import os
import glob
from [Link] import distance
from sklearn.model_selection import train_test_split
from [Link] import classification_report, confusion_matrix
from [Link].vgg19 import VGG19
import tensorflow as tf
from [Link] import Sequential, models
from [Link] import Flatten, Dense, Conv2D, MaxPool2D
from [Link] import ImageDataGenerator
import [Link] as plt
import seaborn as sns
# %%
# %%
path = "/kaggle/input/face-mask-12k-images-dataset/Face Mask Dataset/"
# %%
# %%
dataset = {
"image_path": [],
"mask_status": [],
"where": []
}
for where in [Link](path):
for status in [Link](path+"/"+where):
for image in [Link](path+where+"/"+status+"/"+"*.png"):
dataset["image_path"].append(image)
dataset["mask_status"].append(status)
dataset["where"].append(where)
dataset = [Link](dataset)
[Link]()
# %%
# %%
## Choosing a random image to detect the face in the image
face_model =
[Link]('/kaggle/input/haarcascade/haarcascade_frontalface_defau
[Link]')
## Choosing the image from the directory
img = [Link]("/kaggle/input/face-mask-
detection/images/[Link]")
## Converting the image to grayscale to apply haarcascade algorithm
img = [Link](img, cv2.IMREAD_GRAYSCALE)
## Returns the x, y, w, h co-ordinates as numpy arrays for all the detected
faces
detected_face = face_model.detectMultiScale(img)
## Converting from grayscale to colored image
output_img = [Link](img, cv2.COLOR_RGB2BGR)
## Drawing rectangle box around the faces in the image
for (x, y, w, h) in detected_face:
[Link](output_img, (x,y), (x+w, y+h), (0, 0, 200), 2)
## Displaying the image
[Link](figsize = (15, 15))
[Link](output_img)
# %%
# %%
## Checking for total number of images in the dataset
print(f"With Mask:", dataset.value_counts("mask_status")[0])
print(f"Without Mask:", dataset.value_counts("mask_status")[1])
## Plotting the numbers
[Link](x = dataset["mask_status"])
# %%
# %%
[Link](figsize = (15, 10))
for i in range(9):
random = [Link](1, len(dataset))
[Link](3, 3, i+1)
[Link]([Link]([Link][random,"image_path"]))
[Link]([Link][random,"mask_status"], size = 15)
[Link]([])
[Link]([])
[Link]()
# %%
# %%
## Splitting train test and Validation Dataset
train_df = dataset[dataset["where"] == "Train"]
test_df = dataset[dataset["where"] == "Test"]
valid_df = dataset[dataset["where"] == "Validation"]
print(train_df.head(10))
## Shuffling the dataset
train_df = train_df.sample(frac = 1)
test_df = test_df.sample(frac = 1)
valid_df = valid_df.sample(frac = 1)
print("\n After Shuffling \n")
print(train_df.head(10))
# %%
# %%
[Link](figsize = (15, 5))
[Link](1, 3, 1)
[Link](x = train_df["mask_status"])
[Link]("Training Dataset", size = 10)
[Link](1, 3, 2)
[Link](x = test_df["mask_status"])
[Link]("Test Dataset", size = 10)
[Link](1, 3, 3)
[Link](x = valid_df["mask_status"])
[Link]("Validation Dataset", size = 10)
[Link]()
# %%
# %%
train_df = train_df.reset_index().drop("index", axis = 1)
train_df.head()
# %%
# %%
## Reading all the image into a list and changing the size of the image to
(150,150)
data = []
image_size = 150
for i in range(len(train_df)):
## Converting the image into grayscale
img_array = [Link](train_df["image_path"][i], cv2.IMREAD_GRAYSCALE)
## Resizing the array
new_image_array = [Link](img_array, (image_size, image_size))
##Encoding the image with the label
if train_df["mask_status"][i] == "WithMask":
[Link]([new_image_array, 1])
else:
[Link]([new_image_array, 0])
# %%
# %%
data = [Link](data,dtype="object")
# %%
# %%
data[0][0].shape
# %%
# %%
## Shuffling the data to make sure everything is not in order
[Link](data)
# %%
# %%
## Looking at the training samples
fig, ax = [Link](2, 3, figsize=(10, 10))
for row in range(2):
for col in range(3):
image_index = row*100+col
ax[row, col].axis("off")
ax[row,col].imshow(data[image_index][0], cmap = "gray")
if data[image_index][1] == 0:
ax[row, col].set_title("Without Mask")
else:
ax[row, col].set_title("With Mask")
[Link]()
# %%
# %%
# datagen = ImageDataGenerator(rescale = 1./255)
# train_generator=datagen.flow_from_dataframe(
# dataframe=train_df,
# directory="../input",
# x_col="image_path",
# y_col="mask_status",
# batch_size=80,
# seed=42,
# shuffle=False,
# class_mode="binary",
# target_size=(150,150))
# %%
# %%
X = []
y = []
## Seperating X and y
for image in data:
[Link](image[0])
[Link](image[1])
## Converting X and y to numpy array as Tensorflow accepts only numpy arrays
X = [Link](X)
y = [Link](y)
# %%
# %%
### Normalizing the data
X = X/255
### Train Test Split
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2,
random_state = 42)
# %%
# %%
vgg19 = VGG19(weights='imagenet',include_top=False,input_shape=(128, 128, 3))
for layer in [Link]:
[Link] = False
model = Sequential()
[Link](vgg19)
[Link](Flatten(name="layer1"))
[Link](Dense(128, activation='relu',name="layer2"))
[Link](Dense(2,activation='sigmoid',name="layer3"))
[Link]()
# %%
# %%
[Link](optimizer='adam',
loss=[Link](),
metrics=['accuracy'])
# %%
# %%
train_dir = '../input/face-mask-12k-images-dataset/Face Mask Dataset/Train'
test_dir = '../input/face-mask-12k-images-dataset/Face Mask Dataset/Test'
val_dir = '../input/face-mask-12k-images-dataset/Face Mask Dataset/Validation'
# %%
train_datagen = ImageDataGenerator(rescale=1.0/255, horizontal_flip=True,
zoom_range=0.2,shear_range=0.2)
train_generator =
train_datagen.flow_from_directory(directory=train_dir,target_size=(128,128),cl
ass_mode='categorical',batch_size=32)
val_datagen = ImageDataGenerator(rescale=1.0/255)
val_generator =
train_datagen.flow_from_directory(directory=val_dir,target_size=(128,128),clas
s_mode='categorical',batch_size=32)
test_datagen = ImageDataGenerator(rescale=1.0/255)
test_generator =
train_datagen.flow_from_directory(directory=val_dir,target_size=(128,128),clas
s_mode='categorical',batch_size=32)
history = [Link](train_generator,
epochs=5,
validation_data=val_generator)
# %%
# %%
def visualize_data(history):
# Plotting accuracy
[Link]([Link]['accuracy'])
[Link]('Model Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link](['Train', 'Validation'], loc='upper left')
[Link]()
# Plotting loss
[Link]([Link]['loss'])
[Link]('Model Loss')
[Link]('Epoch')
[Link]('Loss')
[Link](['Train', 'Validation'], loc='upper left')
[Link]()
visualize_data(history)
# %%
[Link]()
# %%
# %%
# Evaluate the model on the test data
evaluation = [Link](test_generator)
# Print the evaluation results
print("Test Loss:", evaluation[0])
print("Test Accuracy:", evaluation[1])