-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
171 lines (135 loc) · 6.3 KB
/
camera.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import cv2
import mediapipe as mp
from keras.models import load_model
import utils
import numpy as np
import time
from threading import Thread
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
label=['Close','Open']
model = load_model('models/drowsiness_new.h5')
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
count=0
score=0
rpred=[99]
lpred=[99]
ALARM_ON = False
CLOSED_EYES_FRAME = 3
CEF_COUNTER = 0
TOTAL_BLINKS = 0
PERCLOS = 0
Open_frames = 0
Closed_frames = 0
token = ''
# 0-yawn, 1-no_yawn, 2-Closed, 3-Open
class Video(object):
def __init__(self,token):
self.video = cv2.VideoCapture(0)
self.Closed_frames = Closed_frames
self.Open_frames = Open_frames
self.count = count
self.score = score
self.label = label
self.rpred = rpred
self.lpred = lpred
self.CEF_COUNTER = CEF_COUNTER
self.CLOSED_EYES_FRAME = CLOSED_EYES_FRAME
self.TOTAL_BLINKS = TOTAL_BLINKS
self.token = token
self.PERCLOS=PERCLOS
self.ALARM_ON = ALARM_ON
def __del__(self):
self.video.release()
cv2.destroyAllWindows()
def get_frame(self):
with mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as face_mesh:
ret,image = self.video.read()
height,width = image.shape[:2]
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_mesh.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
mesh_coords = utils.landmarksDetection(image,results,False)
image = utils.fillPolyTrans(image,[mesh_coords[p] for p in utils.LEFT_EYE],utils.GREEN,opacity=0.3)
image = utils.fillPolyTrans(image,[mesh_coords[p] for p in utils.RIGHT_EYE],utils.GREEN,opacity=0.3)
# Getting the Blink Ratio
ratio = utils.blinkRatio(image,mesh_coords,utils.RIGHT_EYE,utils.LEFT_EYE)
if ratio > 4.9:
self.CEF_COUNTER += 1
cv2.putText(image, 'Blinks', (200, 30),font, 1.3, utils.PINK, 2)
if self.CEF_COUNTER>self.CLOSED_EYES_FRAME:
self.TOTAL_BLINKS +=1
self.CEF_COUNTER = 0
cv2.putText(image, f'Total Blinks {self.TOTAL_BLINKS}', (100, 150),font, 0.6, utils.PINK, 2)
right_coords = [mesh_coords[p] for p in utils.RIGHT_EYE]
left_coords = [mesh_coords[p] for p in utils.LEFT_EYE]
# Get EAR
leftear =utils.ear(mesh_coords,utils.LEFT_EYE)
rightear =utils.ear(mesh_coords,utils.RIGHT_EYE)
ear = (leftear+rightear)/2
cv2.putText(image, f'EAR {ear}', (100, 100),
font, 1.0, utils.GREEN, 2)
# Return the cropped eyes in gray scale
crop_right, crop_left = utils.eyesExtractor(image, right_coords, left_coords)
# cv2.imshow("right",crop_right)
# cv2.imshow("left",crop_left)
# Check if eyes are open or closed
for x in crop_right:
self.count=self.count+1
r_eye = cv2.cvtColor(crop_right,cv2.IMREAD_COLOR)
r_eye = cv2.resize(r_eye,(145,145))
r_eye= r_eye/255
r_eye= r_eye.reshape(-1,145,145,3)
self.rpred = np.argmax(model.predict(r_eye))
if(self.rpred==3):
self.label='Open'
if(rpred==2):
self.label='Closed'
break
for y in crop_left:
self.count=self.count+1
l_eye = cv2.cvtColor(crop_left,cv2.IMREAD_COLOR)
l_eye = cv2.resize(l_eye,(145,145))
l_eye= l_eye/255
l_eye=l_eye.reshape(-1,145,145,3)
self.lpred = np.argmax(model.predict(l_eye))
if(self.lpred==3):
self.label='Open'
if(self.lpred==2):
self.label='Closed'
break
if(self.rpred==2 and self.lpred==2):
self.score=self.score+1
self.Closed_frames += int(self.video.get(cv2.CAP_PROP_FRAME_COUNT))
cv2.putText(image,"Closed",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
else:
self.score=self.score-1
self.Open_frames += int(self.video.get(cv2.CAP_PROP_FRAME_COUNT))
cv2.putText(image,"Open",(10,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
if(self.score<0):
self.score=0
cv2.putText(image,'Frames:'+str(self.score),(100,height-20), font, 1,(255,255,255),1,cv2.LINE_AA)
# Getting the PERCLOS
self.PERCLOS = str(utils.perclos(self.Closed_frames,self.Open_frames))
print("PERCLOS: " + self.PERCLOS)
utils.request(self.PERCLOS,self.TOTAL_BLINKS,self.token)
#driver is sleepy so we beep the alarm
if self.score > 5:
if not self.ALARM_ON:
self.ALARM_ON = True
t=Thread(target=utils.sound_alarm)
t.daemon=True
t.start()
else:
self.ALARM_ON = False
ret,jpg = cv2.imencode('.jpg',image)
return jpg.tobytes()