Skip to content

Instantly share code, notes, and snippets.

@merishnaSuwal
Last active May 24, 2022 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merishnaSuwal/8dbe193c987c30010d28767bfba3b881 to your computer and use it in GitHub Desktop.
Save merishnaSuwal/8dbe193c987c30010d28767bfba3b881 to your computer and use it in GitHub Desktop.
import numpy as np # linear algebra
import cv2 # opencv
import matplotlib.pyplot as plt # image plotting
# keras
from keras import Sequential
from keras.layers import Flatten, Dense
from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
# Load train and test set
train_dir = "data/Face Mask Dataset/Train"
test_dir = "data/Face Mask Dataset/Test"
val_dir = "data/Face Mask Dataset/Validation"
# Read a sample image
img = cv2.imread("sample_images/image (1).png")
# Keep a copy of coloured image
orig_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # colored output image
# Convert image to grayscale
img = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
# loading haarcascade_frontalface_default.xml
face_detection_model = cv2.CascadeClassifier("data/haarcascade_frontalface_default.xml")
# detect faces in the given image
return_faces = face_detection_model.detectMultiScale(
img, scaleFactor=1.07, minNeighbors=4
) # returns a list of (x,y,w,h) tuples
# plotting the returned values
for (x, y, w, h) in return_faces:
cv2.rectangle(orig_img, (x, y), (x + w, y + h), (0, 0, 255), 1)
plt.figure(figsize=(12, 12))
plt.imshow(orig_img) # display the image
# Data preprocessing
# Train data
train_datagenerator = ImageDataGenerator(
rescale=1.0 / 255, horizontal_flip=True, zoom_range=0.2, shear_range=0.2
)
train_generator = train_datagenerator.flow_from_directory(
directory=train_dir, target_size=(128, 128), class_mode="categorical", batch_size=32
)
# Validation data
val_generator = val_datagenerator.flow_from_directory(
directory=val_dir, target_size=(128, 128), class_mode="categorical", batch_size=32
)
# Test data
test_generator = test_datagenerator.flow_from_directory(
directory=val_dir, target_size=(128, 128), class_mode="categorical", batch_size=32
)
# Initializing the VGG19 model
vgg19_model = VGG19(weights="imagenet", include_top=False, input_shape=(128, 128, 3))
for layer in vgg19_model.layers:
layer.trainable = False
# Initialize a sequential model
model = Sequential()
model.add(vgg19_model)
model.add(Flatten())
model.add(Dense(2, activation="sigmoid"))
model.summary()
# Compiling the model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics="accuracy")
# Fit the model on train data along with validation data
model_history = model.fit_generator(
generator=train_generator,
steps_per_epoch=len(train_generator) // 32,
epochs=20,
validation_data=val_generator,
validation_steps=len(val_generator) // 32,
)
# Evaluate model performance on test data
model_loss, model_acc = model.evaluate(test_generator)
print("Model has a loss of %.2f and accuracy %.2f%%" % (model_loss, model_acc*100))
model.save('data/saved_model.h5')
# label for mask detection
mask_det_label = {0: "Mask", 1: "No Mask"}
mask_det_label_colour = {0: (0, 255, 0), 1: (255, 0, 0)}
pad_y = 1 # padding for result text
main_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # colored output image
# For detected faces in the image
for i in range(len(return_faces)):
(x, y, w, h) = return_faces[i]
cropped_face = main_img[y : y + h, x : x + w]
cropped_face = cv2.resize(cropped_face, (128, 128))
cropped_face = np.reshape(cropped_face, [1, 128, 128, 3]) / 255.0
mask_result = model.predict(cropped_face) # make model prediction
print_label = mask_det_label[mask_result.argmax()] # get mask/no mask based on prediction
label_colour = mask_det_label_colour[mask_result.argmax()] # green for mask, red for no mask
# Print result
(t_w, t_h), _ = cv2.getTextSize(
print_label, cv2.FONT_HERSHEY_SIMPLEX, 0.4, 1
) # getting the text size
cv2.rectangle(
main_img,
(x, y + pad_y),
(x + t_w, y - t_h - pad_y - 6),
label_colour,
-1,
) # draw rectangle
cv2.putText(
main_img,
print_label,
(x, y - 6),
cv2.FONT_HERSHEY_DUPLEX,
0.4,
(255, 255, 255), # white
1,
) # print text
cv2.rectangle(
main_img,
(x, y),
(x + w, y + h),
label_colour,
1,
) # draw bounding box on face
plt.figure(figsize=(10, 10))
plt.imshow(main_img) # display image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment