
Image Classification Model

Code
####################imports####################
#do not change
import cv2
import numpy as np
import tensorflow as tf
sprite = Sprite("Tobi")
#do not change
####################imports####################
#Following are the model and video capture configurations
#do not change
model = tf.keras.models.load_model('saved_model.h5',
                                   custom_objects=None,
                                   compile=True,
                                   options=None)
cap = cv2.VideoCapture(0)  # Using device's camera to capture video
text_color = (206, 235, 135)
org = (50, 50)
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
thickness = 3
class_list = ['Mask Off', 'Mask On', 'Mask Wrong']  # List of all the classes
#do not change
###############################################
def checkmask(predicted_class):
  if predicted_class == 'Mask On':
    sprite.say("Thank you for wearing the mask")
  elif predicted_class == 'Mask Off':
    sprite.say("Please wear a mask")
  else:
    sprite.say("Please wear the mask propertly")
#This is the while loop block, computations happen here
while True:
  ret, image_np = cap.read()  # Reading the captured images
  image_np = cv2.flip(image_np, 1)
  image_resized = cv2.resize(image_np, (224, 224))
  img_array = tf.expand_dims(image_resized,
                             0)  # Expanding the image array dimensions
  predict = model.predict(img_array)  # Making an initial model prediction
  predict_index = np.argmax(predict[0],
                            axis=0)  # Generating index out of the prediction
  predicted_class = class_list[
      predict_index]  # Tallying the index with class list
  image_np = cv2.putText(
      image_np, "Image Classification Output: " + str(predicted_class), org,
      font, fontScale, text_color, thickness, cv2.LINE_AA)
  print(predict)
  cv2.imshow("Image Classification Window",
             image_np)  # Displaying the classification window
  checkmask(predicted_class)
  if cv2.waitKey(25) & 0xFF == ord(
      'q'):  # Press 'q' to close the classification window
    break
cap.release()  # Stops taking video input
cv2.destroyAllWindows()  #Closes input window 
		


 
															 
								