|  | 
|  | 1 | +import cv2 | 
|  | 2 | +import os | 
|  | 3 | +from keras.models import load_model | 
|  | 4 | +import numpy as np | 
|  | 5 | +from pygame import mixer | 
|  | 6 | +import time | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | +mixer.init() | 
|  | 10 | +sound = mixer.Sound('alarm.wav') | 
|  | 11 | + | 
|  | 12 | +face = cv2.CascadeClassifier('haar cascade files\haarcascade_frontalface_alt.xml') | 
|  | 13 | +leye = cv2.CascadeClassifier('haar cascade files\haarcascade_lefteye_2splits.xml') | 
|  | 14 | +reye = cv2.CascadeClassifier('haar cascade files\haarcascade_righteye_2splits.xml') | 
|  | 15 | + | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +lbl=['Close','Open'] | 
|  | 19 | + | 
|  | 20 | +model = load_model('models/cnncat2.h5') | 
|  | 21 | +path = os.getcwd() | 
|  | 22 | +cap = cv2.VideoCapture(0) | 
|  | 23 | +font = cv2.FONT_HERSHEY_COMPLEX_SMALL | 
|  | 24 | +count=0 | 
|  | 25 | +score=0 | 
|  | 26 | +thicc=2 | 
|  | 27 | +rpred=[99] | 
|  | 28 | +lpred=[99] | 
|  | 29 | + | 
|  | 30 | +while(True): | 
|  | 31 | + ret, frame = cap.read() | 
|  | 32 | + height,width = frame.shape[:2]  | 
|  | 33 | + | 
|  | 34 | + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | 
|  | 35 | +  | 
|  | 36 | + faces = face.detectMultiScale(gray,minNeighbors=5,scaleFactor=1.1,minSize=(25,25)) | 
|  | 37 | + left_eye = leye.detectMultiScale(gray) | 
|  | 38 | + right_eye = reye.detectMultiScale(gray) | 
|  | 39 | + | 
|  | 40 | + cv2.rectangle(frame, (0,height-50) , (200,height) , (0,0,0) , thickness=cv2.FILLED ) | 
|  | 41 | + | 
|  | 42 | + for (x,y,w,h) in faces: | 
|  | 43 | + cv2.rectangle(frame, (x,y) , (x+w,y+h) , (100,100,100) , 1 ) | 
|  | 44 | + | 
|  | 45 | + for (x,y,w,h) in right_eye: | 
|  | 46 | + r_eye=frame[y:y+h,x:x+w] | 
|  | 47 | + count=count+1 | 
|  | 48 | + r_eye = cv2.cvtColor(r_eye,cv2.COLOR_BGR2GRAY) | 
|  | 49 | + r_eye = cv2.resize(r_eye,(24,24)) | 
|  | 50 | + r_eye= r_eye/255 | 
|  | 51 | + r_eye= r_eye.reshape(24,24,-1) | 
|  | 52 | + r_eye = np.expand_dims(r_eye,axis=0) | 
|  | 53 | + rpred = model.predict_classes(r_eye) | 
|  | 54 | + if(rpred[0]==1): | 
|  | 55 | + lbl='Open'  | 
|  | 56 | + if(rpred[0]==0): | 
|  | 57 | + lbl='Closed' | 
|  | 58 | + break | 
|  | 59 | + | 
|  | 60 | + for (x,y,w,h) in left_eye: | 
|  | 61 | + l_eye=frame[y:y+h,x:x+w] | 
|  | 62 | + count=count+1 | 
|  | 63 | + l_eye = cv2.cvtColor(l_eye,cv2.COLOR_BGR2GRAY)  | 
|  | 64 | + l_eye = cv2.resize(l_eye,(24,24)) | 
|  | 65 | + l_eye= l_eye/255 | 
|  | 66 | + l_eye=l_eye.reshape(24,24,-1) | 
|  | 67 | + l_eye = np.expand_dims(l_eye,axis=0) | 
|  | 68 | + lpred = model.predict_classes(l_eye) | 
|  | 69 | + if(lpred[0]==1): | 
|  | 70 | + lbl='Open'  | 
|  | 71 | + if(lpred[0]==0): | 
|  | 72 | + lbl='Closed' | 
|  | 73 | + break | 
|  | 74 | + | 
|  | 75 | + if(rpred[0]==0 and lpred[0]==0): | 
|  | 76 | + score=score+1 | 
|  | 77 | + cv2.putText(frame,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA) | 
|  | 78 | + # if(rpred[0]==1 or lpred[0]==1): | 
|  | 79 | + else: | 
|  | 80 | + score=score-1 | 
|  | 81 | + cv2.putText(frame,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA) | 
|  | 82 | +  | 
|  | 83 | +  | 
|  | 84 | + if(score<0): | 
|  | 85 | + score=0  | 
|  | 86 | + cv2.putText(frame,'Score:'+str(score),(100,height-20), font, 1,(255,255,255),1,cv2.LINE_AA) | 
|  | 87 | + if(score>15): | 
|  | 88 | + #person is feeling sleepy so we beep the alarm | 
|  | 89 | + cv2.imwrite(os.path.join(path,'image.jpg'),frame) | 
|  | 90 | + try: | 
|  | 91 | + sound.play() | 
|  | 92 | +  | 
|  | 93 | + except: # isplaying = False | 
|  | 94 | + pass | 
|  | 95 | + if(thicc<16): | 
|  | 96 | + thicc= thicc+2 | 
|  | 97 | + else: | 
|  | 98 | + thicc=thicc-2 | 
|  | 99 | + if(thicc<2): | 
|  | 100 | + thicc=2 | 
|  | 101 | + cv2.rectangle(frame,(0,0),(width,height),(0,0,255),thicc)  | 
|  | 102 | + cv2.imshow('frame',frame) | 
|  | 103 | + if cv2.waitKey(1) & 0xFF == ord('q'): | 
|  | 104 | + break | 
|  | 105 | +cap.release() | 
|  | 106 | +cv2.destroyAllWindows() | 
0 commit comments