forked from SnA2502/HAR_2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
152 lines (118 loc) · 5.18 KB
/
utils.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
import cv2
import numpy as np
from keras.models import load_model
from keras.utils import img_to_array
from playsound import playsound
from threading import Thread
import tensorflow as tf
import time
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config)
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
classes = ['Closed', 'Open']
face_cascade = cv2.CascadeClassifier("model/data/haarcascade_frontalface_default.xml")
left_eye_cascade = cv2.CascadeClassifier("model/data/haarcascade_lefteye_2splits.xml")
right_eye_cascade = cv2.CascadeClassifier("model/data/haarcascade_righteye_2splits.xml")
model = load_model("model/drowiness_new7.h5")
alarm_on = False
alarm_sound = "model/data/alarm.mp3"
status1 = ''
status2 = ''
class Camera():
def __init__(self):
self.video = cv2.VideoCapture(0)
self.start_time = time.time()
self.stop_time = self.start_time + 20
def __del__(self):
self.video.release()
def get_feed(self):
stat, frame = self.video.read()
ret, jpeg = cv2.imencode('.jpg', frame)
is_decoded = (time.time() >= self.stop_time) # stop stream after 3 seconds
return jpeg.tobytes(), is_decoded
# ---
def get_camera():
return Camera()
def gen(camera):
while True:
frame, is_decoded = camera.get_feed()
if is_decoded:
print('stop stream')
# insert code here
break
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
def detect_drowsiness():
count = 0
camera = cv2.VideoCapture(0)
while True:
success, frame = camera.read() # read the camera frame
if not success:
break
else:
height = frame.shape[0]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 1)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
left_eye = left_eye_cascade.detectMultiScale(roi_gray)
right_eye = right_eye_cascade.detectMultiScale(roi_gray)
for (x1, y1, w1, h1) in left_eye:
cv2.rectangle(roi_color, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 1)
eye1 = roi_color[y1:y1+h1, x1:x1+w1]
eye1 = cv2.resize(eye1, (145, 145))
eye1 = eye1.astype('float') / 255.0
eye1 = img_to_array(eye1)
eye1 = np.expand_dims(eye1, axis=0)
pred1 = model.predict(eye1)
status1=np.argmax(pred1)
#print(status1)
#status1 = classes[pred1.argmax(axis=-1)[0]]
break
for (x2, y2, w2, h2) in right_eye:
cv2.rectangle(roi_color, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 1)
eye2 = roi_color[y2:y2 + h2, x2:x2 + w2]
eye2 = cv2.resize(eye2, (145, 145))
eye2 = eye2.astype('float') / 255.0
eye2 = img_to_array(eye2)
eye2 = np.expand_dims(eye2, axis=0)
pred2 = model.predict(eye2)
status2=np.argmax(pred2)
#print(status2)
#status2 = classes[pred2.argmax(axis=-1)[0]]
break
# If the eyes are closed, start counting
if status1 == 2 and status2 == 2:
#if pred1 == 2 and pred2 == 2:
count += 1
cv2.putText(frame, "Eyes Closed, Frame count: " + str(count), (10, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1)
# if eyes are closed for 10 consecutive frames, start the alarm
if count >= 10:
cv2.putText(frame, "Drowsiness Alert!!!", (100, height-20), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
if not alarm_on:
alarm_on = True
# play the alarm sound in a new thread
# t = Thread(target=start_alarm, args=(alarm_sound,))
t = Thread(target=playsound('model/data/alarm.mp3'))
t.daemon = True
t.start()
else:
cv2.putText(frame, "Eyes Open", (10, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 1)
count = 0
alarm_on = False
# cv2.imshow("Drowsiness Detector", frame)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# def start_alarm(sound):
# """Play the alarm sound"""
# playsound('data/alarm.mp3')