创建一个基于YOLOv8+PyQt界面的驾驶员疲劳驾驶检测系统 实现对驾驶员疲劳状态的打哈欠检测,头部下垂 疲劳眼睛检测识别

如何使用Yolov8创建一个基于YOLOv8的驾驶员疲劳驾驶检测系统

文章目录

1

疲劳驾驶检测数据集。yolo标签。标签类别序号为0,1,2,3。注意编号从0开始计数,共4类。

创建一个基于YOLOv8的驾驶员疲劳驾驶检测系统,并且带有PyQt界面,我们可以按照以下步骤进行。请注意,由于YOLOv8在撰写此回答时并不是一个实际发布的模型版本,我们将基于YOLOv5的流程和假设YOLOv8有类似的API进行说明。请根据实际情况调整代码以适配YOLOv8的具体实现。

文章及代码仅供参考。

文章目录

1. 数据集准备

首先,确保你的数据集已经准备好,并按照YOLO格式标注(即每行代表一个对象,格式为class_id x_center y_center width height,所有值均为相对值)。对于疲劳驾驶检测,假设我们有4种类别:

  • 0: 疲劳的眼睛
  • 1: 打哈欠
  • 2: 头部下垂
  • 3: 正常状态

2. 安装依赖

安装必要的依赖库:

bash 复制代码
pip install torch torchvision torchaudio
git clone https://github.com/ultralytics/yolov5  # 假设YOLOv8有相似的仓库结构
cd yolov5
pip install -r requirements.txt

3. 创建PyQt界面

创建一个简单的PyQt界面用于展示检测结果、选择图片或视频文件以及启动摄像头实时检测。

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QFileDialog
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import cv2
import torch
from ultralytics import YOLO  # 假设YOLOv8使用类似接口

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Driver Fatigue Detection")
        self.setGeometry(100, 100, 640, 480)

        self.initUI()
        self.model = YOLO('yolov8n.pt')  # 使用适合的预训练模型或自定义模型

    def initUI(self):
        self.image_label = QLabel(self)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.image_label)

        self.detect_image_button = QPushButton("Detect Image", self)
        self.detect_image_button.clicked.connect(self.detect_image)
        self.layout.addWidget(self.detect_image_button)

        self.detect_video_button = QPushButton("Detect Video", self)
        self.detect_video_button.clicked.connect(self.detect_video)
        self.layout.addWidget(self.detect_video_button)

        self.camera_button = QPushButton("Camera", self)
        self.camera_button.clicked.connect(self.start_camera)
        self.layout.addWidget(self.camera_button)

        central_widget = QWidget()
        central_widget.setLayout(self.layout)
        self.setCentralWidget(central_widget)

    def detect_image(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "Images (*.png *.xpm *.jpg *.bmp)")
        if file_path:
            img = cv2.imread(file_path)
            results = self.model(img)
            annotated_img = results[0].plot()
            self.show_image(annotated_img)

    def detect_video(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Video", "", "Videos (*.mp4 *.avi *.mov)")
        if file_path:
            cap = cv2.VideoCapture(file_path)
            while True:
                ret, frame = cap.read()
                if not ret: break
                results = self.model(frame)
                annotated_frame = results[0].plot()
                self.show_image(annotated_frame)
                if cv2.waitKey(1) & 0xFF == ord('q'): break
            cap.release()

    def start_camera(self):
        self.cap = cv2.VideoCapture(0)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(30)

    def update_frame(self):
        ret, frame = self.cap.read()
        if ret:
            results = self.model(frame)
            annotated_frame = results[0].plot()
            self.show_image(annotated_frame)

    def show_image(self, img):
        h, w, ch = img.shape
        bytes_per_line = ch * w
        qimg = QImage(img.data, w, h, bytes_per_line, QImage.Format_BGR888)
        pixmap = QPixmap.fromImage(qimg)
        self.image_label.setPixmap(pixmap)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

4. 模型训练

如果你需要重新训练模型,可以参考前面提供的关于YOLOv5训练的示例代码进行调整。记得修改data.yaml文件中的类别数量和名称以匹配你的数据集。

为了创建一个基于YOLOv8的驾驶员疲劳驾驶检测系统,并且带有PyQt界面,我们需要详细说明模型训练和界面开发的步骤。以下是详细的代码和解释。

1. 数据集准备

确保你的数据集已经准备好,并按照YOLO格式标注(即每行代表一个对象,格式为class_id x_center y_center width height,所有值均为相对值)。对于疲劳驾驶检测,假设我们有4种类别:

  • 0: 疲劳的眼睛
  • 1: 打哈欠
  • 2: 头部下垂
  • 3: 正常状态

2. 模型训练

数据集配置文件 (data.yaml)
yaml 复制代码
train: ./images/train
val: ./images/val
test: ./images/test

nc: 4  # number of classes
names: ['open_eye', 'yawn', 'head_down', 'normal']
训练脚本 (train.py)
python 复制代码
import torch
from ultralytics import YOLO

# Load the model
model = YOLO('yolov8n.yaml')  # or yolov8s, yolov8x, custom

# Train the model
results = model.train(
    data='path/to/data.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    name='driver_fatigue'
)

3. PyQt界面开发

创建一个简单的PyQt界面用于展示检测结果、选择图片或视频文件以及启动摄像头实时检测。

主程序 (MainProgram.py)
python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QPushButton, QFileDialog
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import cv2
import torch
from ultralytics import YOLO

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Driver Fatigue Detection")
        self.setGeometry(100, 100, 640, 480)

        self.initUI()
        self.model = YOLO('runs/train/driver_fatigue/weights/best.pt')

    def initUI(self):
        self.image_label = QLabel(self)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.image_label)

        self.detect_image_button = QPushButton("Detect Image", self)
        self.detect_image_button.clicked.connect(self.detect_image)
        self.layout.addWidget(self.detect_image_button)

        self.detect_video_button = QPushButton("Detect Video", self)
        self.detect_video_button.clicked.connect(self.detect_video)
        self.layout.addWidget(self.detect_video_button)

        self.camera_button = QPushButton("Camera", self)
        self.camera_button.clicked.connect(self.start_camera)
        self.layout.addWidget(self.camera_button)

        central_widget = QWidget()
        central_widget.setLayout(self.layout)
        self.setCentralWidget(central_widget)

    def detect_image(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "Images (*.png *.xpm *.jpg *.bmp)")
        if file_path:
            img = cv2.imread(file_path)
            results = self.model(img)
            annotated_img = results[0].plot()
            self.show_image(annotated_img)

    def detect_video(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Select Video", "", "Videos (*.mp4 *.avi *.mov)")
        if file_path:
            cap = cv2.VideoCapture(file_path)
            while True:
                ret, frame = cap.read()
                if not ret: break
                results = self.model(frame)
                annotated_frame = results[0].plot()
                self.show_image(annotated_frame)
                if cv2.waitKey(1) & 0xFF == ord('q'): break
            cap.release()

    def start_camera(self):
        self.cap = cv2.VideoCapture(0)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(30)

    def update_frame(self):
        ret, frame = self.cap.read()
        if ret:
            results = self.model(frame)
            annotated_frame = results[0].plot()
            self.show_image(annotated_frame)

    def show_image(self, img):
        h, w, ch = img.shape
        bytes_per_line = ch * w
        qimg = QImage(img.data, w, h, bytes_per_line, QImage.Format_BGR888)
        pixmap = QPixmap.fromImage(qimg)
        self.image_label.setPixmap(pixmap)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

4. 运行项目

  1. 安装依赖

    bash 复制代码
    pip install torch torchvision torchaudio
    git clone https://github.com/ultralytics/yolov5  # 假设YOLOv8有相似的仓库结构
    cd yolov5
    pip install -r requirements.txt
  2. 运行训练脚本

    bash 复制代码
    python train.py
  3. 运行主程序

    bash 复制代码
    python MainProgram.py

5. 关键代码解释

数据集配置文件 (data.yaml)
  • train, val, test: 数据集路径。
  • nc: 类别数量。
  • names: 类别名称。
训练脚本 (train.py)
  • YOLO('yolov8n.yaml'): 加载YOLOv8模型。
  • model.train(): 开始训练模型。
主程序 (MainProgram.py)
  • YOLO('runs/train/driver_fatigue/weights/best.pt'): 加载训练好的模型。
  • detect_image(), detect_video(), start_camera(): 分别处理图像、视频和摄像头检测。
  • show_image(): 显示检测结果。

通过以上步骤,tongxue 你就构建一个完整的驾驶员疲劳驾驶检测系统,并带有PyQt界面进行交互。

相关推荐
lxmyzzs4 小时前
【图像算法 - 16】庖丁解牛:基于YOLO12与OpenCV的车辆部件级实例分割实战(附完整代码)
人工智能·深度学习·opencv·算法·yolo·计算机视觉·实例分割
Coovally AI模型快速验证1 天前
SOD-YOLO:基于YOLO的无人机图像小目标检测增强方法
人工智能·yolo·目标检测·机器学习·计算机视觉·目标跟踪·无人机
飞翔的佩奇1 天前
【完整源码+数据集+部署教程】食品分类与实例分割系统源码和数据集:改进yolo11-AggregatedAttention
python·yolo·计算机视觉·数据集·yolo11·食品分类与实例分割
Virgil1391 天前
用PaddleDetection套件训练自己的数据集,PP-YOLO-SOD训练全流程
yolo
AntBlack2 天前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt
前端市界2 天前
前端视角: PyQt6+Vue3 跨界开发实战
前端·qt·pyqt
Coovally AI模型快速验证2 天前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机
程序猿小D3 天前
【完整源码+数据集+部署教程】孔洞检测系统源码和数据集:改进yolo11-RetBlock
yolo·计算机视觉·毕业设计·数据集·yolo11·孔洞检测
钓了猫的鱼儿4 天前
无人机航拍数据集|第14期 无人机水体污染目标检测YOLO数据集3000张yolov11/yolov8/yolov5可训练
yolo·目标检测·猫脸码客·yolo数据集·无人机航拍数据集·无人机水体污染目标检测
AI大法师4 天前
Python:PyQt5 全栈开发教程,构建跨平台桌面应用
python·pyqt