智慧车站车辆-基于YOLOv8与dlib的驾驶员疲劳检测系统本系统是一款基于计算机视觉和深度学习技术的智能监测系统能够实时检测驾驶员的疲劳状态通过分析眼睛、嘴部等面部特征及时发出疲劳预警有效预防疲劳驾驶带来的安全隐患核心特性实时检测支持摄像头实时监测和视频文件分析多维度分析眼睛闭合、打哈欠、点头、眨眼频率等多指标综合评估行为识别精度非常低暂无解决办法检测抽烟、打电话、喝水等危险驾驶行为数据可视化实时显示疲劳指标和历史趋势分析报告生成支持导出PDF和Excel格式的检测报告参数可调灵活的检测参数配置适应不同场景主要功能摄像头检测使用电脑摄像头进行实时监测视频上传上传预录制的视频文件进行分析实时指标显示EAR眼睛纵横比评估眼睛睁闭程度MAR嘴部纵横比检测打哈欠PERCLOS眼睛闭合时间百分比眨眼频率每分钟眨眼次数疲劳等级评估清醒0-30分轻度疲劳30-60分重度疲劳60-100分行为检测识别抽烟、打电话、喝水等危险行为软件环境①Pycharm Anaconda 或 VSCode Anaconda②Python 3.9, OpenCV, PyQt5, ultralytics, torch1.9等111基于YOLOv8与dlib的驾驶员疲劳检测系统完整构建方案一、项目核心信息表项目详细内容项目名称驾驶员疲劳检测系统技术栈Python 3.9 YOLOv8 dlib Flask/HTML前端核心指标EAR眼睛纵横比、MAR嘴部纵横比、PERCLOS、眨眼频率、点头角度疲劳等级清醒(0-30)、轻度疲劳(30-60)、重度疲劳(60-100)检测方式摄像头实时监测、视频文件上传分析辅助功能危险行为识别抽烟/打电话/喝水、数据可视化、报告生成、参数配置开发环境PyCharm/VSCode Anaconda依赖OpenCV、ultralytics、dlib、Flask等二、项目文件结构Driver Fatigue Detection System/ ├── data/ # 数据文件目录 ├── datasets/ # 数据集目录 ├── logs/ # 运行日志目录 ├── models/ # 模型权重文件 │ ├── yolov8-face.pt # 人脸检测模型 │ └── shape_predictor_68_face_landmarks.dat # dlib人脸关键点模型 ├── static/ # 前端静态资源CSS/JS/图片 ├── templates/ # HTML前端页面 │ ├── index.html # 检测主页面 │ └── settings.html # 参数配置页面 ├── testfiles/ # 测试视频文件 ├── uploads/ # 上传视频存储目录 ├── app.py # Flask后端主程序服务入口 ├── config.py # 系统配置文件 ├── database.py # 数据库操作存储历史记录 ├── diagnose_camera.py # 摄像头诊断脚本 ├── download_models.py # 模型自动下载脚本 ├── fatigue_detector.py # 疲劳检测核心逻辑EAR/MAR/PERCLOS计算 ├── quick_camera_test.py # 摄像头快速测试脚本 ├── report_generator.py # PDF/Excel报告生成工具 ├── requirements.txt # 环境依赖列表 ├── test_camera.py # 摄像头功能测试脚本 ├── test_db.py # 数据库功能测试脚本 ├── test_dlib.py # dlib关键点检测测试脚本 ├── video_processor.py # 视频文件处理脚本 ├── yolo_detector.py # YOLOv8人脸/危险行为检测脚本 └── 【必看】用户使用手册.md # 系统使用说明文档三、环境准备与依赖安装# 创建并激活虚拟环境conda create-nfatigue_detectpython3.9conda activate fatigue_detect# 安装依赖pipinstall-rrequirements.txtrequirements.txt内容flask2.0.1 opencv-python4.5.5.62 ultralytics8.0.200 torch1.9.0 torchvision0.10.0 dlib19.22.0 numpy1.21.6 pandas1.3.5 openpyxl3.0.9 reportlab3.6.12四、核心模块代码实现1. 疲劳检测核心逻辑fatigue_detector.pyimportdlibimportcv2importnumpyasnpfromscipy.spatialimportdistanceasdistclassFatigueDetector:def__init__(self,ear_threshold0.21,mar_threshold0.60,blink_frames5,yawn_frames5,perclos_window60,perclos_threshold0.35):# 加载dlib人脸关键点检测器self.detectordlib.get_frontal_face_detector()self.predictordlib.shape_predictor(models/shape_predictor_68_face_landmarks.dat)# 疲劳检测参数self.ear_thresholdear_threshold self.mar_thresholdmar_threshold self.blink_framesblink_frames self.yawn_framesyawn_frames self.perclos_windowperclos_window self.perclos_thresholdperclos_threshold# 状态变量self.eye_closed_frames0self.yawn_frames_count0self.perclos_history[]self.fatigue_score0defeye_aspect_ratio(self,eye):# 计算眼睛纵横比EARAdist.euclidean(eye[1],eye[5])Bdist.euclidean(eye[2],eye[4])Cdist.euclidean(eye[0],eye[3])ear(AB)/(2.0*C)returneardefmouth_aspect_ratio(self,mouth):# 计算嘴部纵横比MARAdist.euclidean(mouth[2],mouth[10])Bdist.euclidean(mouth[4],mouth[8])Cdist.euclidean(mouth[0],mouth[6])mar(AB)/(2.0*C)returnmardefdetect(self,frame):graycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)facesself.detector(gray)ear1.0mar0.0is_eye_closedFalseis_yawnFalseforfaceinfaces:landmarksself.predictor(gray,face)landmarksnp.array([[p.x,p.y]forpinlandmarks.parts()])# 提取眼睛和嘴部关键点left_eyelandmarks[36:42]right_eyelandmarks[42:48]mouthlandmarks[48:68]# 计算EAR和MARear_leftself.eye_aspect_ratio(left_eye)ear_rightself.eye_aspect_ratio(right_eye)ear(ear_leftear_right)/2.0marself.mouth_aspect_ratio(mouth)# 检测闭眼ifearself.ear_threshold:self.eye_closed_frames1ifself.eye_closed_framesself.blink_frames:is_eye_closedTrueelse:self.eye_closed_frames0# 检测打哈欠ifmarself.mar_threshold:self.yawn_frames_count1ifself.yawn_frames_countself.yawn_frames:is_yawnTrueelse:self.yawn_frames_count0# 更新PERCLOSself.perclos_history.append(1ifis_eye_closedelse0)iflen(self.perclos_history)self.perclos_window*30:# 假设30fpsself.perclos_history.pop(0)perclosnp.mean(self.perclos_history)ifself.perclos_historyelse0# 计算疲劳分数self.fatigue_score0ifis_eye_closed:self.fatigue_score30ifis_yawn:self.fatigue_score20ifperclosself.perclos_threshold:self.fatigue_score40# 限制分数范围self.fatigue_scoremin(100,self.fatigue_score)return{ear:ear,mar:mar,perclos:perclos,fatigue_score:self.fatigue_score,is_eye_closed:is_eye_closed,is_yawn:is_yawn}2. Flask后端主程序app.pyfromflaskimportFlask,render_template,request,jsonify,Responseimportcv2importjsonimporttimefromfatigue_detectorimportFatigueDetectorfromyolo_detectorimportBehaviorDetector appFlask(__name__)detectorFatigueDetector()behavior_detectorBehaviorDetector()app.route(/)defindex():returnrender_template(index.html)app.route(/settings)defsettings():returnrender_template(settings.html)app.route(/update_settings,methods[POST])defupdate_settings():datarequest.json detector.ear_thresholddata.get(ear_threshold,0.21)detector.mar_thresholddata.get(mar_threshold,0.60)detector.blink_framesdata.get(blink_frames,5)detector.yawn_framesdata.get(yawn_frames,5)detector.perclos_thresholddata.get(perclos_threshold,0.35)returnjsonify({status:success})defgenerate_video_frames(video_path):capcv2.VideoCapture(video_path)whilecap.isOpened():ret,framecap.read()ifnotret:break# 疲劳检测fatigue_datadetector.detect(frame)# 危险行为检测behavior_databehavior_detector.detect(frame)# 绘制结果cv2.putText(frame,fEAR:{fatigue_data[ear]:.3f},(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)cv2.putText(frame,fMAR:{fatigue_data[mar]:.3f},(10,70),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)cv2.putText(frame,fFatigue Score:{fatigue_data[fatigue_score]},(10,110),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)# 转码为JPEG流ret,buffercv2.imencode(.jpg,frame)frame_bytesbuffer.tobytes()yield(b--frame\r\nbContent-Type: image/jpeg\r\n\r\nframe_bytesb\r\n)app.route(/video_feed)defvideo_feed():video_pathrequest.args.get(path,0)returnResponse(generate_video_frames(video_path),mimetypemultipart/x-mixed-replace; boundaryframe)if__name____main__:app.run(host0.0.0.0,port5000,debugTrue)3. YOLOv8危险行为检测yolo_detector.pyfromultralyticsimportYOLOimportcv2classBehaviorDetector:def__init__(self,model_pathmodels/yolov8-behavior.pt,conf0.5):self.modelYOLO(model_path)self.confconf self.classes[smoking,phone,drinking]# 抽烟、打电话、喝水defdetect(self,frame):resultsself.model(frame,confself.conf)behaviors[]forboxinresults[0].boxes:cls_idint(box.cls)behaviors.append(self.classes[cls_id])returnbehaviors五、系统运行步骤环境准备创建虚拟环境并安装依赖下载dlib模型和YOLOv8权重启动服务运行app.py访问http://localhost:5000打开主页面选择输入源视频文件点击「浏览」选择本地视频点击「开始检测」摄像头选择「实时摄像头」点击「开始检测」参数配置进入「设置」页面调整YOLO置信度、EAR/MAR阈值、PERCLOS时间窗口等参数查看结果实时显示疲劳分数、EAR、PERCLOS等指标疲劳时触发预警提示报告生成检测结束后可导出PDF/Excel格式的疲劳分析报告六、项目亮点✅ 多维度疲劳评估EAR、MAR、PERCLOS、眨眼频率、点头角度多指标综合分析✅ 双检测模式支持摄像头实时监测和预录制视频分析✅ 灵活参数配置可自定义检测阈值、时间窗口等参数适配不同场景✅ 数据可视化实时显示疲劳指标、历史趋势和预警提示✅ 报告生成支持导出PDF/Excel格式的检测报告方便复盘分析✅ 模块化设计核心逻辑与界面分离便于扩展二次开发
智慧车站车辆-基于YOLOv8与dlib的驾驶员疲劳检测系统 基于计算机视觉和深度学习技术的智能监测系统,能够实时检测驾驶员的疲劳状态,通过分析眼睛、嘴部等面部特征,及时发出疲劳预警,有效预防疲劳驾驶
智慧车站车辆-基于YOLOv8与dlib的驾驶员疲劳检测系统本系统是一款基于计算机视觉和深度学习技术的智能监测系统能够实时检测驾驶员的疲劳状态通过分析眼睛、嘴部等面部特征及时发出疲劳预警有效预防疲劳驾驶带来的安全隐患核心特性实时检测支持摄像头实时监测和视频文件分析多维度分析眼睛闭合、打哈欠、点头、眨眼频率等多指标综合评估行为识别精度非常低暂无解决办法检测抽烟、打电话、喝水等危险驾驶行为数据可视化实时显示疲劳指标和历史趋势分析报告生成支持导出PDF和Excel格式的检测报告参数可调灵活的检测参数配置适应不同场景主要功能摄像头检测使用电脑摄像头进行实时监测视频上传上传预录制的视频文件进行分析实时指标显示EAR眼睛纵横比评估眼睛睁闭程度MAR嘴部纵横比检测打哈欠PERCLOS眼睛闭合时间百分比眨眼频率每分钟眨眼次数疲劳等级评估清醒0-30分轻度疲劳30-60分重度疲劳60-100分行为检测识别抽烟、打电话、喝水等危险行为软件环境①Pycharm Anaconda 或 VSCode Anaconda②Python 3.9, OpenCV, PyQt5, ultralytics, torch1.9等111基于YOLOv8与dlib的驾驶员疲劳检测系统完整构建方案一、项目核心信息表项目详细内容项目名称驾驶员疲劳检测系统技术栈Python 3.9 YOLOv8 dlib Flask/HTML前端核心指标EAR眼睛纵横比、MAR嘴部纵横比、PERCLOS、眨眼频率、点头角度疲劳等级清醒(0-30)、轻度疲劳(30-60)、重度疲劳(60-100)检测方式摄像头实时监测、视频文件上传分析辅助功能危险行为识别抽烟/打电话/喝水、数据可视化、报告生成、参数配置开发环境PyCharm/VSCode Anaconda依赖OpenCV、ultralytics、dlib、Flask等二、项目文件结构Driver Fatigue Detection System/ ├── data/ # 数据文件目录 ├── datasets/ # 数据集目录 ├── logs/ # 运行日志目录 ├── models/ # 模型权重文件 │ ├── yolov8-face.pt # 人脸检测模型 │ └── shape_predictor_68_face_landmarks.dat # dlib人脸关键点模型 ├── static/ # 前端静态资源CSS/JS/图片 ├── templates/ # HTML前端页面 │ ├── index.html # 检测主页面 │ └── settings.html # 参数配置页面 ├── testfiles/ # 测试视频文件 ├── uploads/ # 上传视频存储目录 ├── app.py # Flask后端主程序服务入口 ├── config.py # 系统配置文件 ├── database.py # 数据库操作存储历史记录 ├── diagnose_camera.py # 摄像头诊断脚本 ├── download_models.py # 模型自动下载脚本 ├── fatigue_detector.py # 疲劳检测核心逻辑EAR/MAR/PERCLOS计算 ├── quick_camera_test.py # 摄像头快速测试脚本 ├── report_generator.py # PDF/Excel报告生成工具 ├── requirements.txt # 环境依赖列表 ├── test_camera.py # 摄像头功能测试脚本 ├── test_db.py # 数据库功能测试脚本 ├── test_dlib.py # dlib关键点检测测试脚本 ├── video_processor.py # 视频文件处理脚本 ├── yolo_detector.py # YOLOv8人脸/危险行为检测脚本 └── 【必看】用户使用手册.md # 系统使用说明文档三、环境准备与依赖安装# 创建并激活虚拟环境conda create-nfatigue_detectpython3.9conda activate fatigue_detect# 安装依赖pipinstall-rrequirements.txtrequirements.txt内容flask2.0.1 opencv-python4.5.5.62 ultralytics8.0.200 torch1.9.0 torchvision0.10.0 dlib19.22.0 numpy1.21.6 pandas1.3.5 openpyxl3.0.9 reportlab3.6.12四、核心模块代码实现1. 疲劳检测核心逻辑fatigue_detector.pyimportdlibimportcv2importnumpyasnpfromscipy.spatialimportdistanceasdistclassFatigueDetector:def__init__(self,ear_threshold0.21,mar_threshold0.60,blink_frames5,yawn_frames5,perclos_window60,perclos_threshold0.35):# 加载dlib人脸关键点检测器self.detectordlib.get_frontal_face_detector()self.predictordlib.shape_predictor(models/shape_predictor_68_face_landmarks.dat)# 疲劳检测参数self.ear_thresholdear_threshold self.mar_thresholdmar_threshold self.blink_framesblink_frames self.yawn_framesyawn_frames self.perclos_windowperclos_window self.perclos_thresholdperclos_threshold# 状态变量self.eye_closed_frames0self.yawn_frames_count0self.perclos_history[]self.fatigue_score0defeye_aspect_ratio(self,eye):# 计算眼睛纵横比EARAdist.euclidean(eye[1],eye[5])Bdist.euclidean(eye[2],eye[4])Cdist.euclidean(eye[0],eye[3])ear(AB)/(2.0*C)returneardefmouth_aspect_ratio(self,mouth):# 计算嘴部纵横比MARAdist.euclidean(mouth[2],mouth[10])Bdist.euclidean(mouth[4],mouth[8])Cdist.euclidean(mouth[0],mouth[6])mar(AB)/(2.0*C)returnmardefdetect(self,frame):graycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)facesself.detector(gray)ear1.0mar0.0is_eye_closedFalseis_yawnFalseforfaceinfaces:landmarksself.predictor(gray,face)landmarksnp.array([[p.x,p.y]forpinlandmarks.parts()])# 提取眼睛和嘴部关键点left_eyelandmarks[36:42]right_eyelandmarks[42:48]mouthlandmarks[48:68]# 计算EAR和MARear_leftself.eye_aspect_ratio(left_eye)ear_rightself.eye_aspect_ratio(right_eye)ear(ear_leftear_right)/2.0marself.mouth_aspect_ratio(mouth)# 检测闭眼ifearself.ear_threshold:self.eye_closed_frames1ifself.eye_closed_framesself.blink_frames:is_eye_closedTrueelse:self.eye_closed_frames0# 检测打哈欠ifmarself.mar_threshold:self.yawn_frames_count1ifself.yawn_frames_countself.yawn_frames:is_yawnTrueelse:self.yawn_frames_count0# 更新PERCLOSself.perclos_history.append(1ifis_eye_closedelse0)iflen(self.perclos_history)self.perclos_window*30:# 假设30fpsself.perclos_history.pop(0)perclosnp.mean(self.perclos_history)ifself.perclos_historyelse0# 计算疲劳分数self.fatigue_score0ifis_eye_closed:self.fatigue_score30ifis_yawn:self.fatigue_score20ifperclosself.perclos_threshold:self.fatigue_score40# 限制分数范围self.fatigue_scoremin(100,self.fatigue_score)return{ear:ear,mar:mar,perclos:perclos,fatigue_score:self.fatigue_score,is_eye_closed:is_eye_closed,is_yawn:is_yawn}2. Flask后端主程序app.pyfromflaskimportFlask,render_template,request,jsonify,Responseimportcv2importjsonimporttimefromfatigue_detectorimportFatigueDetectorfromyolo_detectorimportBehaviorDetector appFlask(__name__)detectorFatigueDetector()behavior_detectorBehaviorDetector()app.route(/)defindex():returnrender_template(index.html)app.route(/settings)defsettings():returnrender_template(settings.html)app.route(/update_settings,methods[POST])defupdate_settings():datarequest.json detector.ear_thresholddata.get(ear_threshold,0.21)detector.mar_thresholddata.get(mar_threshold,0.60)detector.blink_framesdata.get(blink_frames,5)detector.yawn_framesdata.get(yawn_frames,5)detector.perclos_thresholddata.get(perclos_threshold,0.35)returnjsonify({status:success})defgenerate_video_frames(video_path):capcv2.VideoCapture(video_path)whilecap.isOpened():ret,framecap.read()ifnotret:break# 疲劳检测fatigue_datadetector.detect(frame)# 危险行为检测behavior_databehavior_detector.detect(frame)# 绘制结果cv2.putText(frame,fEAR:{fatigue_data[ear]:.3f},(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)cv2.putText(frame,fMAR:{fatigue_data[mar]:.3f},(10,70),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)cv2.putText(frame,fFatigue Score:{fatigue_data[fatigue_score]},(10,110),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2)# 转码为JPEG流ret,buffercv2.imencode(.jpg,frame)frame_bytesbuffer.tobytes()yield(b--frame\r\nbContent-Type: image/jpeg\r\n\r\nframe_bytesb\r\n)app.route(/video_feed)defvideo_feed():video_pathrequest.args.get(path,0)returnResponse(generate_video_frames(video_path),mimetypemultipart/x-mixed-replace; boundaryframe)if__name____main__:app.run(host0.0.0.0,port5000,debugTrue)3. YOLOv8危险行为检测yolo_detector.pyfromultralyticsimportYOLOimportcv2classBehaviorDetector:def__init__(self,model_pathmodels/yolov8-behavior.pt,conf0.5):self.modelYOLO(model_path)self.confconf self.classes[smoking,phone,drinking]# 抽烟、打电话、喝水defdetect(self,frame):resultsself.model(frame,confself.conf)behaviors[]forboxinresults[0].boxes:cls_idint(box.cls)behaviors.append(self.classes[cls_id])returnbehaviors五、系统运行步骤环境准备创建虚拟环境并安装依赖下载dlib模型和YOLOv8权重启动服务运行app.py访问http://localhost:5000打开主页面选择输入源视频文件点击「浏览」选择本地视频点击「开始检测」摄像头选择「实时摄像头」点击「开始检测」参数配置进入「设置」页面调整YOLO置信度、EAR/MAR阈值、PERCLOS时间窗口等参数查看结果实时显示疲劳分数、EAR、PERCLOS等指标疲劳时触发预警提示报告生成检测结束后可导出PDF/Excel格式的疲劳分析报告六、项目亮点✅ 多维度疲劳评估EAR、MAR、PERCLOS、眨眼频率、点头角度多指标综合分析✅ 双检测模式支持摄像头实时监测和预录制视频分析✅ 灵活参数配置可自定义检测阈值、时间窗口等参数适配不同场景✅ 数据可视化实时显示疲劳指标、历史趋势和预警提示✅ 报告生成支持导出PDF/Excel格式的检测报告方便复盘分析✅ 模块化设计核心逻辑与界面分离便于扩展二次开发