-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_drow_exe.py
66 lines (56 loc) · 2.65 KB
/
driver_drow_exe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import cv2
import numpy as np
from keras.models import load_model
from pygame import mixer
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
model = load_model(r'C:\Users\susmitha racherla\PycharmProjects\Major\dd-models\model.h5')
mixer.init()
sound= mixer.Sound(r'C:\Users\susmitha racherla\PycharmProjects\Major\alarm.wav')
cap = cv2.VideoCapture(0)
Score = 0
while True:
ret, frame = cap.read()
height,width = frame.shape[0:2]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces= face_cascade.detectMultiScale(gray, scaleFactor= 1.2, minNeighbors=3)
eyes= eye_cascade.detectMultiScale(gray, scaleFactor= 1.1, minNeighbors=1)
cv2.rectangle(frame, (0,height-50),(200,height),(0,0,0),thickness=cv2.FILLED)
for (x,y,w,h) in faces:
cv2.rectangle(frame,pt1=(x,y),pt2=(x+w,y+h), color= (255,0,0), thickness=3 )
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(frame,pt1=(ex,ey),pt2=(ex+ew,ey+eh), color= (255,0,0), thickness=3 )
# preprocessing steps
eye= frame[ey:ey+eh,ex:ex+ew]
eye= cv2.resize(eye,(80,80))
eye= eye/255
eye= eye.reshape(80,80,3)
eye= np.expand_dims(eye,axis=0)
# preprocessing is done now model prediction
prediction = model.predict(eye)
# if eyes are closed
if prediction[0][0]>0.30:
cv2.putText(frame,'closed',(10,height-20),fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale=1,color=(255,255,255),
thickness=1,lineType=cv2.LINE_AA)
cv2.putText(frame,'Score'+str(Score),(100,height-20),fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale=1,color=(255,255,255),
thickness=1,lineType=cv2.LINE_AA)
Score=Score+1
if(Score>15):
try:
sound.play()
except:
pass
# if eyes are open
elif prediction[0][1]>0.90:
cv2.putText(frame,'open',(10,height-20),fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale=1,color=(255,255,255),
thickness=1,lineType=cv2.LINE_AA)
cv2.putText(frame,'Score'+str(Score),(100,height-20),fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,fontScale=1,color=(255,255,255),
thickness=1,lineType=cv2.LINE_AA)
Score = Score-2
if (Score<0):
Score=0
cv2.imshow('frame',frame)
if cv2.waitKey(33) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()