java opencv 人眼检测,Python+opencv实时的人眼识别+眨眼检测+闭眼检测源代码及详细教程...

【实例简介】

压缩包中含有【人眼识别+眨眼识别】源代码以及详细使用教程,利用pyrhon+opencv在ubuntu系统上运行,实现实时的检测。

【实例截图】

【核心代码】

ed25065c-8023-475e-93ed-844f9da44350

└── 新建文件夹

├── imutils-master

│   ├── bin

│   │   └── range-detector

│   ├── demo_images

│   │   ├── bridge.jpg

│   │   ├── cactus.jpg

│   │   ├── notecard.png

│   │   ├── pyimagesearch_logo.jpg

│   │   ├── shapes.png

│   │   └── workspace.jpg

│   ├── demos

│   │   ├── encode_image.py

│   │   ├── feature_demo.py

│   │   ├── finding_function_names.py

│   │   ├── fps_demo.py

│   │   ├── image_basics.py

│   │   ├── image_paths.py

│   │   ├── opencv_versions.py

│   │   ├── perspective_transform.py

│   │   ├── picamera_fps_demo.py

│   │   ├── sorting_contours.py

│   │   └── temp_file.py

│   ├── docs

│   │   └── images

│   │   ├── auto_canny.png

│   │   ├── matplotlib.png

│   │   ├── perspective_transform.png

│   │   ├── resizing.png

│   │   ├── rotation.png

│   │   ├── skeletonization.png

│   │   ├── sorting_contours.png

│   │   ├── translation_eq.png

│   │   ├── translation.png

│   │   └── url_to_image.png

│   ├── imutils

│   │   ├── contours.py

│   │   ├── convenience.py

│   │   ├── encodings.py

│   │   ├── face_utils

│   │   │   ├── facealigner.py

│   │   │   ├── helpers.py

│   │   │   └── __init__.py

│   │   ├── feature

│   │   │   ├── factories.py

│   │   │   ├── gftt.py

│   │   │   ├── helpers.py

│   │   │   └── __init__.py

│   │   ├── __init__.py

│   │   ├── io

│   │   │   ├── __init__.py

│   │   │   └── tempfile.py

│   │   ├── meta.py

│   │   ├── object_detection.py

│   │   ├── paths.py

│   │   ├── perspective.py

│   │   └── video

│   │   ├── count_frames.py

│   │   ├── filevideostream.py

│   │   ├── fps.py

│   │   ├── __init__.py

│   │   ├── pivideostream.py

│   │   ├── videostream.py

│   │   └── webcamvideostream.py

│   ├── LICENSE.txt

│   ├── MANIFEST

│   ├── README.md

│   ├── setup.cfg

│   └── setup.py

├── shape_predictor_68_face_landmarks.dat

├── Wink.py

└── 教程.pdf

12 directories, 60 files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是OpenCVPython和Dlib实现面部标定、眨眼和疲劳检测的代码: 首先,需要安装OpenCVPython和Dlib库。在Python中,可以使用pip安装这些库: ``` pip install opencv-python pip install dlib ``` 接下来,导入必要的库: ```python import cv2 import dlib import numpy as np from scipy.spatial import distance as dist ``` 然后,定义一些常量和函数: ```python EYE_AR_THRESH = 0.25 # 眼睛长宽比阈值 EYE_AR_CONSEC_FRAMES = 3 # 连续帧数 YAWN_THRESH = 20 # 打哈欠阈值 ALARM_SOUND_PATH = "alarm.wav" # 警报声音文件路径 def eye_aspect_ratio(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(mouth): # 计算嘴巴长宽比 A = dist.euclidean(mouth[14], mouth[18]) B = dist.euclidean(mouth[12], mouth[16]) C = dist.euclidean(mouth[0], mouth[6]) mar = (A + B) / (2.0 * C) return mar def play_alarm_sound(path): # 播放警报声音 import os os.system("aplay " + path + " &") ``` 现在,让我们加载Dlib的人脸检测器和68个面部标定点模型: ```python detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") ``` 最后,我们可以开始处理视频流或摄像头输入: ```python cap = cv2.VideoCapture(0) # 摄像头输入 ear_history = [] # 眼睛长宽比历史记录 mar_history = [] # 嘴巴长宽比历史记录 alarm_on = False # 是否播放警报声音 while True: ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: # 检测人脸并标定面部 shape = predictor(gray, rect) shape = np.array([(p.x, p.y) for p in shape.parts()]) # 计算眼睛长宽比和嘴巴长宽比 left_eye = shape[36:42] right_eye = shape[42:48] mouth = shape[48:68] ear = (eye_aspect_ratio(left_eye) + eye_aspect_ratio(right_eye)) / 2.0 mar = mouth_aspect_ratio(mouth) # 在视频中显示眼睛和嘴巴区域 cv2.drawContours(frame, [cv2.convexHull(left_eye)], -1, (0, 255, 0), 1) cv2.drawContours(frame, [cv2.convexHull(right_eye)], -1, (0, 255, 0), 1) cv2.drawContours(frame, [cv2.convexHull(mouth)], -1, (0, 255, 0), 1) # 更新眼睛长宽比历史记录 ear_history.append(ear) if len(ear_history) > EYE_AR_CONSEC_FRAMES: ear_history.pop(0) # 更新嘴巴长宽比历史记录 mar_history.append(mar) if len(mar_history) > EYE_AR_CONSEC_FRAMES: mar_history.pop(0) # 判断是否眨眼和打哈欠 ear_avg = np.mean(ear_history) mar_avg = np.mean(mar_history) if ear_avg < EYE_AR_THRESH and mar_avg > YAWN_THRESH: # 开始播放警报声音 if not alarm_on: alarm_on = True play_alarm_sound(ALARM_SOUND_PATH) else: # 停止播放警报声音 alarm_on = False cv2.imshow("Frame", frame) key = cv2.waitKey(1) if key == 27: break cap.release() cv2.destroyAllWindows() ``` 这段代码中,我们使用OpenCV从摄像头或视频流中读取帧。然后,我们将每个帧转换为灰度图像,并使用Dlib检测人脸和面部标定点。 接下来,我们计算眼睛长宽比和嘴巴长宽比,并绘制出眼睛和嘴巴的轮廓。然后,我们更新眼睛长宽比和嘴巴长宽比历史记录,并检查是否有连续的帧满足眨眼和打哈欠的条件。如果是,我们播放警报声音。 最后,我们在视频中显示帧,并等待按下ESC键退出程序。 希望这个例子可以帮助你开始使用OpenCVPython和Dlib实现面部标定、眨眼和疲劳检测

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值