
import cv2
import numpy as np
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array

#load model
model = model_from_json(open("fer.json", "r").read())            #change the path accoring to files
#load weights
model.load_weights('fer.h5')                    #change the path accoring to files


detection_model_path="C:/Users/panur/.spyder-py3/FaceMaskDetection/cascadeH5.xml"    #change the path accoring to files
face_detection = cv2.CascadeClassifier(detection_model_path)                         

ret=1
flag=True
cap = cv2.VideoCapture(0)   #default 0 for webcam 
frameRate = cap.get(30)

while(cap.isOpened()):

 ret, fm=cap.read()

 fm = cv2.resize(fm, (224, 224))
 file = cv2.cvtColor(fm, cv2.COLOR_BGR2RGB)
 
 orig_frame = file
 frame = file
 faces = face_detection.detectMultiScale(frame,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
 
 if len(faces) :
    faces = sorted(faces, reverse=True,key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
    (fX, fY, fW, fH) = faces
    roi = frame[fY:fY + fH, fX:fX + fW]
    roi = cv2.resize(roi, (48, 48),3)
    roi = frame.astype("float") / 255.0
    
    roi = img_to_array(roi)
    roi = np.expand_dims(roi, axis=0)
    
    preds=model.predict_classes(roi)[0]
    if preds==0:
      print("Mask worn")
      test='Mask worn'
    elif preds==1:
      print("Danger: No Mask")
      test='Danger: No Mask'
 
    cv2.putText(fm,test, (fX-15, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
    cv2.rectangle(fm, (fX, fY), (fX + fW, fY + fH),(0, 0, 255), 2)
 
   
 cv2.imshow("Live Video", fm)

 k=cv2.waitKey(25)   #Press ESC to stop/exit
 if k == 27: 
    ret=0        
    break

print("closed")
cap.release()   
cv2.destroyAllWindows()
[((14, 17, 14, 60), 'cv2.CascadeClassifier', 'cv2.CascadeClassifier', ({(14, 39, 14, 59): 'detection_model_path'}, {}), '(detection_model_path)', False, 'import cv2\n'), ((18, 6, 18, 25), 'cv2.VideoCapture', 'cv2.VideoCapture', ({(18, 23, 18, 24): '0'}, {}), '(0)', False, 'import cv2\n'), ((63, 0, 63, 23), 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ({}, {}), '()', False, 'import cv2\n'), ((25, 6, 25, 32), 'cv2.resize', 'cv2.resize', ({(25, 17, 25, 19): 'fm', (25, 21, 25, 31): '(224, 224)'}, {}), '(fm, (224, 224))', False, 'import cv2\n'), ((26, 8, 26, 43), 'cv2.cvtColor', 'cv2.cvtColor', ({(26, 21, 26, 23): 'fm', (26, 25, 26, 42): 'cv2.COLOR_BGR2RGB'}, {}), '(fm, cv2.COLOR_BGR2RGB)', False, 'import cv2\n'), ((54, 1, 54, 29), 'cv2.imshow', 'cv2.imshow', ({(54, 12, 54, 24): '"""Live Video"""', (54, 26, 54, 28): 'fm'}, {}), "('Live Video', fm)", False, 'import cv2\n'), ((56, 3, 56, 18), 'cv2.waitKey', 'cv2.waitKey', ({(56, 15, 56, 17): '25'}, {}), '(25)', False, 'import cv2\n'), ((36, 10, 36, 37), 'cv2.resize', 'cv2.resize', ({(36, 21, 36, 24): 'roi', (36, 26, 36, 34): '(48, 48)', (36, 35, 36, 36): '3'}, {}), '(roi, (48, 48), 3)', False, 'import cv2\n'), ((39, 10, 39, 27), 'keras.preprocessing.image.img_to_array', 'img_to_array', ({(39, 23, 39, 26): 'roi'}, {}), '(roi)', False, 'from keras.preprocessing.image import img_to_array\n'), ((40, 10, 40, 37), 'numpy.expand_dims', 'np.expand_dims', (), '', True, 'import numpy as np\n'), ((50, 4, 50, 90), 'cv2.putText', 'cv2.putText', ({(50, 16, 50, 18): 'fm', (50, 19, 50, 23): 'test', (50, 25, 50, 41): '(fX - 15, fY - 10)', (50, 43, 50, 67): 'cv2.FONT_HERSHEY_SIMPLEX', (50, 69, 50, 73): '(0.45)', (50, 75, 50, 86): '(0, 0, 255)', (50, 88, 50, 89): '(2)'}, {}), '(fm, test, (fX - 15, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (\n    0, 0, 255), 2)', False, 'import cv2\n'), ((51, 4, 51, 66), 'cv2.rectangle', 'cv2.rectangle', ({(51, 18, 51, 20): 'fm', (51, 22, 51, 30): '(fX, fY)', (51, 32, 51, 50): '(fX + fW, fY + fH)', (51, 51, 51, 62): '(0, 0, 255)', (51, 64, 51, 65): '(2)'}, {}), '(fm, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)', False, 'import cv2\n')]