WIN10下实现基于Opencv+dlib的疲劳检测Demo
本文在window10系统下,采用anaconda配置的python3.7虚拟环境,基于opencv+dlib库源码实现疲劳检测小Demo
文章目录
前言
基于Dlib的人脸检测
Dlib 本身是一个用 C++ 编写的机器学习和信号处理相关的开源工具包,里面包含了很多人工智能相关理论的实现方,人脸检测与识别是其中的一个子功能。利用其提供的预训练模型,除了可以检测人脸位置外,还可以对人脸中的 68 个标志点(landmarks)进行检测。
闭眼检测原理,计算眼部37到48相关对应点的距离,判定是否闭眼;通过闭眼时间判定是否疲劳
一、虚拟环境配置
cmd打开终端
# 创建名为env_dlib的python3.7的虚拟环境
conda create -n env_dlib python=3.7
# 接着进入虚拟环境
conda activate env_dlib
二、库环境安装(虚拟环境下)
1.安装cv2
pip install opencv-python
2.安装imutils
pip install imutils
3.安装dlib
本文采用whl文件安装的方式进行安装
GitHub链接: 下载python3.7对应的whl文件
- python3.7对应文件:dlib-19.19.0-cp37-cp37m-win_amd64.whl
- python3.8对应文件:dlib-19.19.0-cp38-cp38-win_amd64.whl
- python3.6对应文件可在这查找: dlib-19.8.1-cp36-cp36m-win_amd64.whl
# 安装cmake
pip install cmake
# 首先cd到刚刚下载whl所在的文件夹安装whl文件
# 在python3.7下则对应cp37的文件,其他版本以此类推
pip install dlib-19.19.0-cp37-cp37m-win_amd64.whl
也可以尝试其他安装方法链接: dlib 安装教程(三种方法)
反正直接用下面这个方法大概率安装不上 可以尝试下
# 先安装cmake,再安装dlib
conda install cmake
pip install dlib==19.6.0
4.安装scipy
pip install scipy
三、运行代码
1.源码下载
首先去GitHub下载对应源码
链接: Drowsiness_Detection
用VS code打开对应工程 选择对应虚拟环境env_dlib
2.运行源码
from scipy.spatial import distance
from imutils import face_utils
import imutils
import dlib
import cv2
def eye_aspect_ratio(eye):
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
thresh = 0.25
frame_check = 20
detect = dlib.get_frontal_face_detector()
# 此处的代码路径改为自己dat模型的绝对路径(我这边相对路径运行不了),同时千万不能包含中文
# 不然报错 RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
# predict = dlib.shape_predictor("models/shape_predictor_68_face_landmarks.dat")# Dat file is the crux of the code
predict = dlib.shape_predictor("D:/Drowsiness-Detection/Drowsiness_Detection/models/shape_predictor_68_face_landmarks.dat")# Dat file is the crux of the code
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
cap=cv2.VideoCapture(0)
flag=0
while True:
ret, frame=cap.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
subjects = detect(gray, 0)
for subject in subjects:
shape = predict(gray, subject)
shape = face_utils.shape_to_np(shape)#converting to NumPy Array
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
if ear < thresh:
flag += 1
print (flag)
if flag >= frame_check:
cv2.putText(frame, "****************ALERT!****************", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "****************ALERT!****************", (10,325),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
#print ("Drowsy")
else:
flag = 0
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
cap.release()
3.可能出现的bug
参考链接: RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
# 此处的代码路径改为自己dat模型的绝对路径(我这边相对路径运行不了),同时千万不能包含中文
# 不然报错 RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
# #predict = dlib.shape_predictor("models/shape_predictor_68_face_landmarks.dat")# Dat file is the crux of the code
predict = dlib.shape_predictor("D:/Drowsiness-Detection/Drowsiness_Detection/models/shape_predictor_68_face_landmarks.dat")# Dat
4.运行效果
睁眼效果
闭眼效果
四、参考链接
- 参考1 使用dlib实现的简单疲劳检测程序
- 参考2 dlib 安装教程(三种方法)