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