效果图
基本思路
- 在 OpenCV 中使用VideoCapture方法初始化视频渲染对象
- 创建灰度图像
- 导入预训练模型,识别脸部和人脸标志
- 计算上唇和下唇距离(其它类似)
- 创建唇边距离的If条件,满足则是打哈欠,不满足则只是简单的张嘴
- 显示帧/图像
部分源码
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
|
suc, frame = cam.read() # 读取不到退出 if not suc: break # ---------FPS------------# ctime = time.time() fps = int ( 1 / (ctime - ptime)) ptime = ctime cv2.putText(frame, f 'FPS:{fps}' , (frame.shape[ 1 ] - 120 , frame.shape[ 0 ] - 20 ), cv2.FONT_HERSHEY_PLAIN, 2 , ( 0 , 200 , 0 ), 3 ) # ------检测人脸------# # 转为灰度 img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_model(img_gray) for face in faces: # 检测人脸,框起来-# x1 = face.left() y1 = face.top() x2 = face.right() y2 = face.bottom() # print(face.top()) cv2.rectangle(frame, (x1, y1), (x2, y2), ( 200 , 0 , 00 ), 2 ) # ----------检测人脸标注-----------# shapes = landmark_model(img_gray, face) shape = face_utils.shape_to_np(shapes) # -------检测上下唇--------# lip = shape[ 48 : 60 ] cv2.drawContours(frame, [lip], - 1 , ( 0 , 165 , 255 ), thickness = 3 ) # -------计算上下唇距离-----# lip_dist = cal_yawn(shape) # 打印距离 # print(lip_dist) # 大于设定值,则认定是打哈欠 if lip_dist > yawn_thresh: cv2.putText(frame, f 'User Yawning!' , (frame.shape[ 1 ] / / 2 - 170 , frame.shape[ 0 ] / / 2 ), cv2.FONT_HERSHEY_SIMPLEX, 2 , ( 0 , 0 , 200 ), 2 ) # 按字母q退出 cv2.imshow( 'Webcam' , frame) if cv2.waitKey( 1 ) & 0xFF = = ord ( 'q' ): break |
到此这篇关于基于Python实现打哈欠检测详解的文章就介绍到这了,更多相关Python打哈欠检测内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_46211269/article/details/124105198