# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---



# +
#Import openCV
import cv2
import datetime
from keras.preprocessing import image
import numpy as np
from keras.models import load_model
mymodel = load_model('Facemask_.h5')

#Enable camera
camera = cv2.VideoCapture(0)
#loading the Haar Cascade Classifier Model to detect the face
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

while (True):
    _,img = camera.read()
    face = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=4)
    for (x,y,w,h) in face:
        face_img = img[x:x+w, y:y+h]
        cv2.imwrite('temp.jpg', face_img)
        
        #Load image saved using cv2
        test_img = image.load_img('temp.jpg', target_size=(150,150,3))
        test_img = image.img_to_array(test_img)
        test_img = np.expand_dims(test_img, axis=0)
        predicte = mymodel.predict_classes(test_img)
        
        #If condition to determine the class
        if predicte[0][0]==0:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
            cv2.putText(img,'With Mask',((x+w)//2,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),3)
        else:
            cv2.rectangle(img, (x,y),(x+w,y+h),(0,0,255),3)
            cv2.putText(img,'NO MASK',((x+w//2,y+h+20)), cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3)
        #datetim = str(datetime.datetime.now())
        #cv2.putText(img,datetim,(400,450),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),3)
        
    cv2.imshow('img',img)
        
    #Terminaton condition
    if cv2.waitKey(1) & 0xFF==ord('q'): #Press q to exit
        break
            
#Clean up resources
camera.release()
cv2.destroyAllWindows()
# -



StarcoderdataJupyterScriptsDedupFiltered