【深度学习】dlib 人脸关键点实时疲劳检测

发布时间:2026/7/12 6:38:55

【深度学习】dlib 人脸关键点实时疲劳检测 文章目录完整代码一览环境准备与模型下载核心原理眼睛纵横比EAR逐行代码详解导入库EAR 计算函数中文绘制函数绘制眼睛轮廓主程序主循环完整代码一览import numpy as np import dlib import cv2 from sklearn.metrics.pairwise import euclidean_distances from PIL import Image,ImageDraw,ImageFont # 计算眼睛EAR纵横比 defeye_aspect_ratio(eye):Aeuclidean_distances(eye[1].reshape(1,2),eye[5].reshape(1,2))Beuclidean_distances(eye[2].reshape(1,2),eye[4].reshape(1,2))Ceuclidean_distances(eye[0].reshape(1,2),eye[3].reshape(1,2))ear((AB)/2.0)/Creturnear # 绘制中文文字 defcv2AddChineseText(img,text,position,textColor(255,0,0),textSize50):ifisinstance(img,np.ndarray):imgImage.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))drawImageDraw.Draw(img)fontStyleImageFont.truetype(simhei.ttf,textSize,encodingutf-8)draw.text(position,text,textColor,fontStyle)returncv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)# 绘制眼睛轮廓 defdrawEye(eye):eyeHullcv2.convexHull(eye)cv2.drawContours(frame,[eyeHull],-1,(0,255,0),2)COUNTER0detectordlib.get_frontal_face_detector()predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)capcv2.VideoCapture(0)whileTrue:ret,framecap.read()ifnot ret:breakfacesdetector(frame,0)forface in faces:shapepredictor(frame,face)shapenp.array([[p.x,p.y]forp in shape.parts()])rightEyeshape[36:42]leftEyeshape[42:48]rightEAReye_aspect_ratio(rightEye)leftEAReye_aspect_ratio(leftEye)ear(leftEARrightEAR)/2.0ifear0.3:COUNTER1ifCOUNTER30:framecv2AddChineseText(frame,!!!!危险!!!!,(250,250))else:COUNTER0drawEye(leftEye)drawEye(rightEye)infoEAR: {:.2f}.format(ear[0][0])framecv2AddChineseText(frame,info,(0,30))cv2.imshow(Frame,frame)ifcv2.waitKey(1)27:breakcap.release()cv2.destroyAllWindows()环境准备与模型下载import numpy as np import dlib import cv2 from sklearn.metrics.pairwise import euclidean_distances #计算欧氏距离 from PIL import Image,ImageDraw,ImageFontdlib安装可以看博主的另外一篇文章了解本文不过多赘述【深度学习】dlib 人脸关键点检测核心原理眼睛纵横比EAREAREye Aspect Ratio是衡量眼睛张开程度的一个指标。上眼睑左点A上眼睑右点B右眼角C下眼睑右点D下眼睑左点E左眼角FEAR (||A-E|| ||B-D||) / (2 * ||F-C||)分子是两只眼睛垂直方向的平均距离约等于眼睛高度分母是水平方向的距离约等于眼睛宽度。当眼睛睁开时EAR 值较大约 0.25~0.3当眼睛闭合时EAR 急剧减小接近 0。我们设定一个阈值如 0.3即可判断睁闭眼。逐行代码详解导入库import numpy as np #数组操作 import dlib #人脸检测 import cv2 #图像处理 from sklearn.metrics.pairwise import euclidean_distances #计算欧氏距离 from PIL import Image,ImageDraw,ImageFontEAR 计算函数defeye_aspect_ratio(eye):Aeuclidean_distances(eye[1].reshape(1,2),eye[5].reshape(1,2))Beuclidean_distances(eye[2].reshape(1,2),eye[4].reshape(1,2))Ceuclidean_distances(eye[0].reshape(1,2),eye[3].reshape(1,2))ear((AB)/2.0)/CreturnearEAR公式(AB)/(2*C)eye 是包含 6 个点坐标的 NumPy 数组形状为 (6, 2)。按照 dlib 的索引顺序0 和 3 是水平两端1和5、2和4 是垂直两对。计算三组距离代入公式得到 EAR。注意返回的 ear 是一个 (1,1) 数组后面取值时需用 ear[0][0]。中文绘制函数defcv2AddChineseText(img,text,position,textColor(255,0,0),textSize50):ifisinstance(img,np.ndarray):imgImage.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))drawImageDraw.Draw(img)fontStyleImageFont.truetype(simhei.ttf,textSize,encodingutf-8)draw.text(position,text,textColor,fontStyle)returncv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)将 OpenCV 的 BGR 图像转为 PIL 的 RGB 图像绘制文字后再转回 BGR。绘制眼睛轮廓defdrawEye(eye):eyeHullcv2.convexHull(eye)cv2.drawContours(frame,[eyeHull],-1,(0,255,0),2)cv2.convexHull 计算眼睛点的凸包然后绘制绿色轮廓线用于直观显示眼睛区域。主程序COUNTER0detectordlib.get_frontal_face_detector()predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)capcv2.VideoCapture(0)COUNTER 用于统计连续闭眼帧数。初始化检测器、预测器打开默认摄像头。主循环whileTrue:ret,framecap.read()ifnot ret:breakfacesdetector(frame,0)逐帧读取检测人脸。forface in faces:shapepredictor(frame,face)shapenp.array([[p.x,p.y]forp in shape.parts()])rightEyeshape[36:42]leftEyeshape[42:48]rightEAReye_aspect_ratio(rightEye)leftEAReye_aspect_ratio(leftEye)ear(leftEARrightEAR)/2.0提取 68 点分别截取左右眼的 6 个点注意 dlib 中右眼是 36~41左眼是 42~47。分别计算两眼的 EAR取平均值作为最终的 ear。ifear0.3:COUNTER1ifCOUNTER30:framecv2AddChineseText(frame,!!!!危险!!!!,(250,250))else:COUNTER0如果 EAR 低于阈值 0.3说明眼睛闭合计数器加 1。当连续闭眼超过 30 帧判定为疲劳瞌睡在画面中央显示红色警告文字。一旦睁眼计数器清零。drawEye(leftEye)drawEye(rightEye)infoEAR: {:.2f}.format(ear[0][0])framecv2AddChineseText(frame,info,(0,30))绘制眼睛轮廓并在屏幕左上角显示当前的 EAR 数值方便调试。cv2.imshow(Frame,frame)ifcv2.waitKey(1)27:break显示画面按 ESC 退出。

相关新闻