-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.0.py
439 lines (392 loc) · 18.3 KB
/
main1.0.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import socket
import cv2
import numpy as np
import threading
import queue
import time
import dlib
from imutils import face_utils
from scipy.spatial import distance as dist
from collections import deque
# 创建共享队列
frame_queue = queue.Queue()
# 创建VideoWriter对象
fps = 30 # 设置帧率
frame_width, frame_height = 640, 480 # 设置帧的宽度和高度
receive_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
send_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定socket到一个可用的端口上
receive_server_address = ('192.168.200.1', 8888) # 设置PC端的IP地址和端口号
receive_server_socket.bind(receive_server_address)
send_server_address = ('192.168.200.1', 8880) # 设置PC端的IP地址和端口号
send_server_socket.bind(send_server_address)
# 监听连接
receive_server_socket.listen(1)
send_server_socket.listen(1)
def receive_frames():
while True:
# 接受客户端连接
client_socket, client_address = receive_server_socket.accept() # 接收一次消息
print('等待客户端连接...')
print('客户端已连接:', client_address)
# 接收图像数据流并解码
image_data = b''
while True:
data = client_socket.recv(1024)
if not data:
print("No data")
break
image_data += data
# 检查接收到的数据流是否已经包含完整的图片
if image_data.endswith(b'\xff\xd9'):
# 将接收到的数据流转换为图像
nparr = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print("Get Pic")
# 将图像写入队列
frame_queue.put(image)
# 显示解码后的图像
#cv2.imshow("1", image)
cv2.waitKey(1) # 设置适当的等待时间,单位为毫秒
# 清空图像数据,准备接收下一张图片
image_data = b''
# 关闭客户端socket
client_socket.close()
# 关闭服务器socket
receive_server_socket.close()
def send_command():
while True:
# 接受客户端连接
client_socket, client_address = send_server_socket.accept() # 接收一次消息
print('8880等待客户端连接...')
print('8880客户端已连接:', client_address)
if 30 <= fatigue_detector.score <= 55:
message = "0"
client_socket.send(message.encode())
time.sleep(15) # 不同的疲劳度,间隔时间应该不同
elif 55 < fatigue_detector.score <= 75:
message = "1"
client_socket.send(message.encode())
time.sleep(15) # 不同的疲劳度,间隔时间应该不同
elif fatigue_detector.score > 75:
message = "2"
client_socket.send(message.encode())
time.sleep(15) # 不同的疲劳度,间隔时间应该不同
else: # 测试
message = "1"
client_socket.send(message.encode())
time.sleep(15) # 不同的疲劳度,间隔时间应该不同
# 关闭客户端socket
client_socket.close()
# 关闭VideoWriter对象和服务器socket
send_server_socket.close()
class Fatigue_detecting:
def __init__(self):
# 默认为摄像头0
self.VIDEO_STREAM = 0
self.CAMERA_STYLE = False # False未打开摄像头,True摄像头已打开
self.AR_CONSEC_FRAMES_check = 3
self.OUT_AR_CONSEC_FRAMES_check = 5
# 闪烁阈值(秒)
# 眼睛长宽比
self.EYE_AR_THRESH = 0.2
self.EYE_AR_CONSEC_FRAMES = 3
# 打哈欠长宽比
self.MAR_THRESH = 0.7
self.MOUTH_AR_CONSEC_FRAMES = 3
# 瞌睡点头
self.HAR_THRESH = 3
self.NOD_AR_CONSEC_FRAMES = 5
"""计数"""
# 初始化帧计数器和眨眼总数
self.COUNTER = 0
self.TOTAL = 0
self.BCOUNT = 1
# 初始化帧计数器和打哈欠总数
self.mCOUNTER = 0
self.mTOTAL = 0
# 初始化帧计数器和点头总数
self.hCOUNTER = 0
self.hTOTAL = 0
# 离职时间长度
self.oCOUNTER = 0
# 初始化眨眼频率,点头频率,打哈欠频率
self.frequency = 0
self.hfrequency = 0
self.yfrequency = 0
self.bfequnency = 0
# 初始化疲劳程度
self.score = 0
self.blink_start_frame = None
self.continuous_blink_frames = 0
self.blink_durations = []
self.blink_durations = deque(maxlen=100) # 记录最近100次闭眼事件的持续时间和时间戳
self.running = True # 标记是否继续运行
self.ONE_MINUTE = 60 # 一分钟的秒数
self.start_time = time.time() # 记录计时开始的时间戳
self.FPS = 30 # 设置帧率为30帧每秒
"""姿态"""
self.object_pts = np.float32([[6.825897, 6.760612, 4.402142],
[1.330353, 7.122144, 6.903745],
[-1.330353, 7.122144, 6.903745],
[-6.825897, 6.760612, 4.402142],
[5.311432, 5.485328, 3.987654],
[1.789930, 5.393625, 4.413414],
[-1.789930, 5.393625, 4.413414],
[-5.311432, 5.485328, 3.987654],
[2.005628, 1.409845, 6.165652],
[-2.005628, 1.409845, 6.165652],
[2.774015, -2.080775, 5.048531],
[-2.774015, -2.080775, 5.048531],
[0.000000, -3.116408, 6.097667],
[0.000000, -7.415691, 4.070434]])
self.K = [6.5308391993466671e+002, 0.0, 3.1950000000000000e+002,
0.0, 6.5308391993466671e+002, 2.3950000000000000e+002,
0.0, 0.0, 1.0]
self.D = [7.0834633684407095e-002, 6.9140193737175351e-002, 0.0, 0.0, -1.3073460323689292e+000]
self.cam_matrix = np.array(self.K).reshape(3, 3).astype(np.float32)
self.dist_coeffs = np.array(self.D).reshape(5, 1).astype(np.float32)
self.reprojectsrc = np.float32([[10.0, 10.0, 10.0],
[10.0, 10.0, -10.0],
[10.0, -10.0, -10.0],
[10.0, -10.0, 10.0],
[-10.0, 10.0, 10.0],
[-10.0, 10.0, -10.0],
[-10.0, -10.0, -10.0],
[-10.0, -10.0, 10.0]])
self.line_pairs = [[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7]]
self.running = False
def __del__(self):
pass
def get_head_pose(self, shape):
image_pts = np.float32([shape[17], shape[21], shape[22], shape[26], shape[36],
shape[39], shape[42], shape[45], shape[31], shape[35],
shape[48], shape[54], shape[57], shape[8]])
_, rotation_vec, translation_vec = cv2.solvePnP(self.object_pts, image_pts, self.cam_matrix, self.dist_coeffs)
reprojectdst, _ = cv2.projectPoints(self.reprojectsrc, rotation_vec, translation_vec, self.cam_matrix,
self.dist_coeffs)
reprojectdst = tuple(map(tuple, reprojectdst.reshape(8, 2)))
rotation_mat, _ = cv2.Rodrigues(rotation_vec)
pose_mat = cv2.hconcat((rotation_mat, translation_vec))
_, _, _, _, _, _, euler_angle = cv2.decomposeProjectionMatrix(pose_mat)
return reprojectdst, euler_angle
def eye_aspect_ratio(self, eye):
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
def mouth_aspect_ratio(self, mouth):
A = dist.euclidean(mouth[2], mouth[10])
B = dist.euclidean(mouth[4], mouth[8])
C = dist.euclidean(mouth[0], mouth[6])
mar = (A + B) / (2.0 * C)
return mar
def _learning_face(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor('./model/shape_predictor_68_face_landmarks.dat')
while self.running:
if not frame_queue.empty():
frame = frame_queue.get()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = self.detector(gray, 0)
for rect in rects:
shape = self.predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
leftEye = shape[face_utils.FACIAL_LANDMARKS_IDXS["left_eye"][0]:
face_utils.FACIAL_LANDMARKS_IDXS["left_eye"][1]]
rightEye = shape[face_utils.FACIAL_LANDMARKS_IDXS["right_eye"][0]:
face_utils.FACIAL_LANDMARKS_IDXS["right_eye"][1]]
mouth = shape[face_utils.FACIAL_LANDMARKS_IDXS["mouth"][0]:
face_utils.FACIAL_LANDMARKS_IDXS["mouth"][1]]
leftEAR = self.eye_aspect_ratio(leftEye)
rightEAR = self.eye_aspect_ratio(rightEye)
mar = self.mouth_aspect_ratio(mouth)
ear = (leftEAR + rightEAR) / 2.0
print(ear)
if ear < self.EYE_AR_THRESH:
self.COUNTER += 1
if self.blink_start_frame is None:
self.blink_start_frame = self.COUNTER
# 计算闭眼时长
blink_frames = self.COUNTER - self.blink_start_frame
blink_duration = blink_frames / self.FPS
self.blink_durations.append(blink_duration)
if blink_duration > 1.2:
self.BCOUNT += 2 # 增加闭眼次数
else:
if self.COUNTER >= self.EYE_AR_CONSEC_FRAMES:
self.TOTAL += 1
self.COUNTER = 0
self.blink_start_frame = None
if mar > self.MAR_THRESH:
self.mCOUNTER += 1
else:
if self.mCOUNTER >= self.MOUTH_AR_CONSEC_FRAMES:
self.mTOTAL += 1
self.mCOUNTER = 0
reprojectdst, euler_angle = self.get_head_pose(shape)
har = euler_angle[0, 0]
if har > self.HAR_THRESH:
self.hCOUNTER += 1
else:
if self.hCOUNTER >= self.NOD_AR_CONSEC_FRAMES:
self.hTOTAL += 1
self.hCOUNTER = 0
if ear < self.EYE_AR_THRESH and mar > self.MAR_THRESH and har > self.HAR_THRESH:
self.oCOUNTER += 1
else:
if self.oCOUNTER >= self.OUT_AR_CONSEC_FRAMES_check:
self.oCOUNTER = 0
self.score += 1
if self.score >= 30 and self.score <= 55:
cv2.putText(frame, "mid fatigue", (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
if self.score > 55 and self.score <= 75:
cv2.putText(frame, "moderate fatigue", (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
if self.score > 75:
cv2.putText(frame, "severe fatigue", (10, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
# print("score :",self.score)
# 显示结果
cv2.putText(frame, "Blinks: {}".format(self.TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 0, 255), 2)
cv2.putText(frame, "Yawns: {}".format(self.mTOTAL), (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 0, 255), 2)
cv2.putText(frame, "Nods: {}".format(self.hTOTAL), (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255),
2)
cv2.putText(frame, "scores: {}".format(self.score), (10, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 0, 255),
2)
# cv2.putText(frame, "severe fatigue", (350, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
if self.blink_durations:
blink_duration_text = "Last Blink Duration: {:.2f}s".format(self.blink_durations[-1])
cv2.putText(frame, blink_duration_text, (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.imshow("Frame", frame)
cv2.waitKey(1) # 设置适当的等待时间,单位为毫秒
def count(self, event):
t = 0
bcount = 0
while self.running:
# 计算眨眼、打哈欠、点头的频率并更新疲劳分数
# (这部分实现依赖于具体需求)
fBOUNT = self.BCOUNT
fTOTAL = self.TOTAL
fmTOTAL = self.mTOTAL
fhTOTAL = self.hTOTAL
time.sleep(5)
lBOUNT = self.BCOUNT
lTOTAL = self.TOTAL
lmTOTAL = self.mTOTAL
lhTOTAL = self.hTOTAL
# 计算眨眼频率
self.frequency = (lTOTAL - fTOTAL) / 5
# 计算点头频率
self.hfrequency = (lhTOTAL - fhTOTAL) / 5
# 计算打哈欠频率
self.yfrequency = (lmTOTAL - fmTOTAL) / 5
self.bfequnency = (lBOUNT - fBOUNT) / 5
if self.score >= 100:
self.score = 100
if self.score <= 0:
self.score = 0
# if self.frequency > 0.47 and self.frequency < 0.61:
# self.score = self.score + 10
# if self.frequency > 0.62 and self.frequency < 0.95:
# self.score = self.score + 15
# if self.frequency > 0.96:
# self.score = self.score + 20
# if self.frequency < 0.47 and self.score >= 0:
# self.score = self.score - 5
print("b:", self.bfequnency)
if self.bfequnency >= 1.5:
self.score = self.score + (5 * self.bfequnency)
if self.bfequnency <= 1 and self.bfequnency > 0:
self.score -= 5
if self.yfrequency >= 0.2 and self.yfrequency <= 0.4:
self.score = self.score + 10
if self.yfrequency > 0.4 and self.yfrequency <= 0.6:
self.score = self.score + 15
if self.yfrequency > 0.6:
self.score = self.score + 20
if self.yfrequency < 0.2 and self.score >= 0:
self.score = self.score - 10
if self.hfrequency >= 0.2 and self.hfrequency <= 0.4:
self.score = self.score + 15
if self.hfrequency > 0.4 and self.hfrequency <= 0.6:
self.score = self.score + 20
if self.hfrequency > 0.6:
self.score = self.score + 25
if self.hfrequency < 0.2 and self.score >= 0:
self.score = self.score - 20
if self.score >= 100:
self.score = 100
if self.score <= 0:
self.score = 0
# def count(self, event):
# for i in range(500):
# # while True:
# fTOTAL = self.TOTAL
# fmTOTAL = self.mTOTAL
# fhTOTAL = self.hTOTAL
# time.sleep(5)
# lTOTAL = self.TOTAL
# lmTOTAL = self.mTOTAL
# lhTOTAL = self.hTOTAL
# # 计算眨眼频率
# self.frequency = (lTOTAL - fTOTAL) / 5
# # 计算点头频率
# self.hfrequency = (lhTOTAL - fhTOTAL) / 5
# # 计算打哈欠频率
# self.yfrequency = (lmTOTAL - fmTOTAL) / 5
#
# if self.score >= 100:
# self.score = 100
# if self.score <= 0:
# self.score = 0
# if self.frequency < 0.2and self.frequency > 0:
# self.score = self.score + 10
# if self.frequency > 0.15and self.frequency >0:
# self.score = self.score + 15
# if self.frequency < 0.1:
# self.score = self.score + 20
# if self.frequency > 0.33 and self.score >= 0:
# self.score = self.score - 5
# if self.yfrequency >= 0.2 and self.yfrequency <= 0.4:
# self.score = self.score + 10
# if self.yfrequency > 0.4 and self.yfrequency <= 0.6:
# self.score = self.score + 15
# if self.yfrequency > 0.6:
# self.score = self.score + 20
# if self.yfrequency < 0.2 and self.score >= 0:
# self.score = self.score - 10
# if self.hfrequency >= 0.2 and self.hfrequency <= 0.4:
# self.score = self.score + 15
# if self.hfrequency > 0.4 and self.hfrequency <= 0.6:
# self.score = self.score + 20
# if self.hfrequency > 0.6:
# self.score = self.score + 25
# if self.hfrequency < 0.2 and self.score >= 0:
# self.score = self.score - 20
# if self.score >= 100:
# self.score = 100
# if self.score <= 0:
# self.score = 0
def run(self):
self.running = True
self._learning_face()
# 创建并启动接收和帧和发送指令的线程
receive_thread = threading.Thread(target=receive_frames)
receive_thread.start()
send_thread = threading.Thread(target=send_command)
send_thread.start()
# 创建并启动疲劳检测的实例
fatigue_detector = Fatigue_detecting()
fatigue_detector_thread = threading.Thread(target=fatigue_detector.run)
fatigue_detector_thread.start()
# 等待线程结束
receive_thread.join()
send_thread.join()
fatigue_detector_thread.join()
# 释放资源
cv2.destroyAllWindows()