基于深度学习的驾驶员分心驾驶行为 疲劳危险行为预 警系统使用改进网络结构YOLOV5PERCLOS实现。文章目录1. 安装依赖2. 数据准备3. 模型配置3.1 加载YOLOv5模型3.2 配置PERCLOSPercentage of Eye Closure4. 实时检测与预警4.1 实时摄像头检测5. GUI 应用程序5.1 安装PySide25.2 构建GUI1. 安装依赖2. GUI界面3. 实时摄像头检测与疲劳检测4. 结合GUI和实时检测疲劳驾驶司机异常驾驶行为检测及预警系统实现目标1.基于深度学习的驾驶员分心驾驶行为 疲劳危险行为预警系统使用改进网络结构YOLOV5PERCLOS实现。2.可以检测是否存在玩手机、抽烟、喝水这三种行为可以通过计算眼睛和嘴巴的开合程度来判断是存在否闭眼或者打哈欠并计算疲劳程度可直接调用摄像头实时检测有可视化界面。3.包括疲劳检测打哈欠低头闭眼吸烟检测吃喝东西检测打电话检测摄像头实时检测视频导入检测。4.python疲劳驾驶预警系统构建一个基于深度学习的驾驶员疲劳驾驶预警系统可以检测多种异常驾驶行为如玩手机、抽烟、喝水等并通过计算眼睛和嘴巴的开合程度来判断是否存在闭眼或打哈欠的行为。以下是详细的步骤和代码示例仅参考。1. 安装依赖确保安装了以下依赖库pipinstalltorch torchvision opencv-python dlib numpy imutils PySide22. 数据准备假设你已经有了标注好的数据集包括人脸、眼睛、嘴巴等关键点的标注。3. 模型配置3.1 加载YOLOv5模型fromultralyticsimportYOLO# 加载预训练的YOLOv5模型modelYOLO(yolov5s.pt)3.2 配置PERCLOSPercentage of Eye ClosurePERCLOS是通过计算眼睛闭合的时间百分比来评估疲劳程度的方法。4. 实时检测与预警4.1 实时摄像头检测importcv2importdlibimportnumpyasnpimportimutilsfromimutilsimportface_utilsfromscipy.spatialimportdistanceasdistdefeye_aspect_ratio(eye):# 计算眼睛的长宽比Adist.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(mouth):# 计算嘴巴的长宽比Adist.euclidean(mouth[2],mouth[10])# 51, 59Bdist.euclidean(mouth[4],mouth[6])# 53, 57Cdist.euclidean(mouth[0],mouth[8])# 49, 55mar(AB)/(2.0*C)returnmardefdetect_fatigue(frame,landmarks):# 提取眼睛和嘴巴的关键点left_eyelandmarks[36:42]right_eyelandmarks[42:48]mouthlandmarks[48:68]# 计算EAR和MARleft_eareye_aspect_ratio(left_eye)right_eareye_aspect_ratio(right_eye)ear(left_earright_ear)/2.0marmouth_aspect_ratio(mouth)# 判断是否闭眼或打哈欠ifear0.25:return闭眼ifmar0.7:return打哈欠returnNonedefmain():# 初始化摄像头capcv2.VideoCapture(0)# 加载面部检测器detectordlib.get_frontal_face_detector()predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)whileTrue:ret,framecap.read()ifnotret:breakgraycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)rectsdetector(gray,0)forrectinrects:shapepredictor(gray,rect)shapeface_utils.shape_to_np(shape)# 检测疲劳fatigue_statusdetect_fatigue(frame,shape)iffatigue_status:print(f检测到{fatigue_status})# 绘制关键点for(x,y)inshape:cv2.circle(frame,(x,y),1,(0,255,0),-1)cv2.imshow(Fatigue Detection,frame)ifcv2.waitKey(1)0xFFord(q):breakcap.release()cv2.destroyAllWindows()if__name____main__:main()5. GUI 应用程序使用PySide2构建GUI应用程序。5.1 安装PySide2pipinstallPySide25.2 构建GUIimportsysfromPySide2.QtWidgetsimportQApplication,QMainWindow,QLabel,QVBoxLayout,QWidget,QPushButton,QFileDialog,QComboBoxfromPySide2.QtGuiimportQPixmap,QImagefromPySide2.QtCoreimportQTimerimportcv2importdlibimportnumpyasnpimportimutilsfromimutilsimportface_utilsfromscipy.spatialimportdistanceasdistclassFatigueDetectionApp(QMainWindow):def__init__(self):super().__init__()self.setWindowTitle(疲劳驾驶预警系统)self.setGeometry(100,100,800,600)self.image_labelQLabel(self)self.buttonQPushButton(开始检测,self)self.button.clicked.connect(self.start_detection)vboxQVBoxLayout()vbox.addWidget(self.image_label)vbox.addWidget(self.button)self.setLayout(vbox)self.capcv2.VideoCapture(0)self.detectordlib.get_frontal_face_detector()self.predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)self.timerQTimer(self)self.timer.timeout.connect(self.update_frame)self.timer.start(30)defupdate_frame(self):ret,frameself.cap.read()ifnotret:returngraycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)rectsself.detector(gray,0)forrectinrects:shapeself.predictor(gray,rect)shapeface_utils.shape_to_np(shape)# 检测疲劳fatigue_statusdetect_fatigue(frame,shape)iffatigue_status:print(f检测到{fatigue_status})# 绘制关键点for(x,y)inshape:cv2.circle(frame,(x,y),1,(0,255,0),-1)height,width,channelframe.shape bytes_per_line3*width q_imgQImage(frame.data,width,height,bytes_per_line,QImage.Format_RGB888).rgbSwapped()pixmapQPixmap.fromImage(q_img)self.image_label.setPixmap(pixmap)defstart_detection(self):self.cap.open(0)defcloseEvent(self,event):self.cap.release()event.accept()if__name____main__:appQApplication(sys.argv)demoFatigueDetectionApp()demo.show()sys.exit(app.exec_())为了帮助你构建一个完整的驾驶员疲劳检测系统我将提供详细的代码示例。这个系统包括以下几个部分GUI界面使用tkinter库创建一个简单的图形用户界面。摄像头实时检测使用cv2库进行视频流处理。面部关键点检测使用dlib库检测面部关键点。疲劳检测算法计算眼睛闭合程度PERCLOS和嘴巴开合程度MAR。1. 安装依赖确保安装了以下依赖库pipinstallopencv-python dlib numpy imutils tkinter2. GUI界面使用tkinter创建一个简单的GUI界面。importtkinterastkfromtkinterimportmessageboxclassFatigueDetectionApp:def__init__(self,root):self.rootroot self.root.title(驾驶员疲劳检测)self.root.geometry(600x400)# 创建标签labeltk.Label(root,text驾驶员疲劳检测,font(Helvetica,16))label.pack(pady20)# 创建按钮btn_open_cameratk.Button(root,text测试1: 打开相机,commandself.open_camera)btn_open_camera.pack(pady10)btn_open_camera_recordtk.Button(root,text测试2: 打开相机并记录,commandself.open_camera_and_record)btn_open_camera_record.pack(pady10)btn_open_camera_detecttk.Button(root,text测试3: 打开摄像头并检测,commandself.open_camera_and_detect)btn_open_camera_detect.pack(pady10)btn_detect_recordtk.Button(root,text测试4: 检测记录,commandself.detect_record)btn_detect_record.pack(pady10)btn_blink_detect_recordtk.Button(root,text测试5: 检测眨眼并录制声音,commandself.blink_detect_and_record)btn_blink_detect_record.pack(pady10)btn_exittk.Button(root,text退出,commandroot.quit)btn_exit.pack(pady10)defopen_camera(self):messagebox.showinfo(提示,打开相机功能未实现)defopen_camera_and_record(self):messagebox.showinfo(提示,打开相机并记录功能未实现)defopen_camera_and_detect(self):messagebox.showinfo(提示,打开摄像头并检测功能未实现)defdetect_record(self):messagebox.showinfo(提示,检测记录功能未实现)defblink_detect_and_record(self):messagebox.showinfo(提示,检测眨眼并录制声音功能未实现)if__name____main__:roottk.Tk()appFatigueDetectionApp(root)root.mainloop()3. 实时摄像头检测与疲劳检测使用cv2和dlib库进行实时摄像头检测并计算眼睛闭合程度PERCLOS和嘴巴开合程度MAR。importcv2importdlibimportnumpyasnpimportimutilsfromimutilsimportface_utilsfromscipy.spatialimportdistanceasdistdefeye_aspect_ratio(eye):Adist.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(mouth):Adist.euclidean(mouth[2],mouth[10])# 51, 59Bdist.euclidean(mouth[4],mouth[6])# 53, 57Cdist.euclidean(mouth[0],mouth[8])# 49, 55mar(AB)/(2.0*C)returnmardefdetect_fatigue(frame,landmarks):left_eyelandmarks[36:42]right_eyelandmarks[42:48]mouthlandmarks[48:68]left_eareye_aspect_ratio(left_eye)right_eareye_aspect_ratio(right_eye)ear(left_earright_ear)/2.0marmouth_aspect_ratio(mouth)ifear0.25:return闭眼ifmar0.7:return打哈欠returnNonedefmain():capcv2.VideoCapture(0)detectordlib.get_frontal_face_detector()predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)whileTrue:ret,framecap.read()ifnotret:breakgraycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)rectsdetector(gray,0)forrectinrects:shapepredictor(gray,rect)shapeface_utils.shape_to_np(shape)fatigue_statusdetect_fatigue(frame,shape)iffatigue_status:print(f检测到{fatigue_status})for(x,y)inshape:cv2.circle(frame,(x,y),1,(0,255,0),-1)cv2.imshow(Fatigue Detection,frame)ifcv2.waitKey(1)0xFFord(q):breakcap.release()cv2.destroyAllWindows()if__name____main__:main()4. 结合GUI和实时检测将上述代码结合在一起创建一个完整的应用程序。importtkinterastkfromtkinterimportmessageboximportcv2importdlibimportnumpyasnpimportimutilsfromimutilsimportface_utilsfromscipy.spatialimportdistanceasdistclassFatigueDetectionApp:def__init__(self,root):self.rootroot self.root.title(驾驶员疲劳检测)self.root.geometry(600x400)# 创建标签labeltk.Label(root,text驾驶员疲劳检测,font(Helvetica,16))label.pack(pady20)# 创建按钮btn_open_cameratk.Button(root,text测试1: 打开相机,commandself.open_camera)btn_open_camera.pack(pady10)btn_open_camera_recordtk.Button(root,text测试2: 打开相机并记录,commandself.open_camera_and_record)btn_open_camera_record.pack(pady10)btn_open_camera_detecttk.Button(root,text测试3: 打开摄像头并检测,commandself.open_camera_and_detect)btn_open_camera_detect.pack(pady10)btn_detect_recordtk.Button(root,text测试4: 检测记录,commandself.detect_record)btn_detect_record.pack(pady10)btn_blink_detect_recordtk.Button(root,text测试5: 检测眨眼并录制声音,commandself.blink_detect_and_record)btn_blink_detect_record.pack(pady10)btn_exittk.Button(root,text退出,commandroot.quit)btn_exit.pack(pady10)self.capcv2.VideoCapture(0)self.detectordlib.get_frontal_face_detector()self.predictordlib.shape_predictor(shape_predictor_68_face_landmarks.dat)defopen_camera(self):messagebox.showinfo(提示,打开相机功能未实现)defopen_camera_and_record(self):messagebox.showinfo(提示,打开相机并记录功能未实现)defopen_camera_and_detect(self):whileTrue:ret,frameself.cap.read()ifnotret:breakgraycv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)rectsself.detector(gray,0)forrectinrects:shapeself.predictor(gray,rect)shapeface_utils.shape_to_np(shape)fatigue_statusdetect_fatigue(frame,shape)iffatigue_status:print(f检测到{fatigue_status})for(x,y)inshape:cv2.circle(frame,(x,y),1,(0,255,0),-1)cv2.imshow(Fatigue Detection,frame)ifcv2.waitKey(1)0xFFord(q):breakself.cap.release()cv2.destroyAllWindows()defdetect_record(self):messagebox.showinfo(提示,检测记录功能未实现)defblink_detect_and_record(self):messagebox.showinfo(提示,检测眨眼并录制声音功能未实现)defeye_aspect_ratio(eye):Adist.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(mouth):Adist.euclidean(mouth[2],mouth[10])# 51, 59Bdist