实时驾驶员状态检测(毕业设计+代码)

环境要求

使用Python,结合OpenCV和Dlib库,基于实时的网络摄像头视频流,实现驾驶员注意力状态检测和监测。
安装

该项目在Python 3.9上运行,需要以下库:

  • numpy OpenCV(opencv-python)
  • Dlib
  • 和cmake

面部关键点的Dlib预测器已包含在“predictor”文件夹中。

  1. 重要提示:Dlib的需求和库安装
  2. Dlib是一个需要安装C/C++编译器和Cmake库的库。请按照此指南在您的计算机上正确安装dlib。
  3. 如果您的计算机已经具备安装dlib和cmake的所有先决条件,您可以使用存储库中提供的requirements.txt文件:

在这里插入图片描述

算法

这个脚本首先寻找驾驶员的面部,然后使用dlib库来预测68个面部关键点。所有面部关键点的枚举和位置可以在这里看到。

根据这些关键点,计算以下得分:

**EAR(眼睛纵横比):**标准化后的平均眼孔口径,用于判断眼睛的张开程度
**Gaze Score(注视得分):**眼睛中心和瞳孔之间的L2范数(欧几里得距离),用于判断驾驶员是否看向其他地方
**Head Pose(头部姿态):**驾驶员头部的滚转、俯仰和偏航角。这些角度用于判断驾驶员是否没有看向前方或头部姿态不正(可能是昏迷状态)
**PERCLOS(闭眼时间百分比):**一分钟内闭眼时间的百分比。在此情况下,采用0.2的阈值(一分钟的20%),并利用EAR得分来估计闭眼时间。
驾驶员状态可以分类为:

正常:不会打印任何信息。
疲劳:当PERCLOS得分> 0.2时,在屏幕上会打印一个警告信息。
**睡着了:**当眼睛闭合(EAR < closure_threshold)一段时间时,在屏幕上会打印一个警告信息。
**看向其他地方:**当注视得分高于某个阈值并持续一段时间时,在屏幕上会打印一个警告信息。
分心:当头部姿态得分高于某个阈值并持续一段时间时,在屏幕上会打印一个警告信息。

眼睛模型

眼睛纵横比(EAR)是一个标准化得分,有助于了解眼睛的张开程度。使用每只眼睛的dlib关键点(每个眼睛都有6个关键点),估计眼睛的长度和宽度,并使用这些数据计算EAR得分,如下图所示:
在这里插入图片描述
结果:
计算注视得分的处理方法有更新:
在这里插入图片描述

在第一个版本中,使用自适应阈值处理来帮助Hough变换检测瞳孔位置。在更新的版本中,经过双边滤波器去除一些噪声后,仅使用Hough变换,并且将眼睛的ROI区域大小缩小了。

使用方法和代码

class HeadPoseEstimator:

    def __init__(self, camera_matrix=None, dist_coeffs=None, show_axis: bool = False):
        """
        Head Pose estimator class that contains the get_pose method for computing the three euler angles
        (roll, pitch, yaw) of the head. It uses the image/frame, the dlib detected landmarks of the head and,
        optionally the camera parameters
        Parameters
        ----------
        camera_matrix: numpy array
            Camera matrix of the camera used to capture the image/frame
        dist_coeffs: numpy array
            Distortion coefficients of the camera used to capture the image/frame
        show_axis: bool
            If set to True, shows the head pose axis projected from the nose keypoint and the face landmarks points
            used for pose estimation (default is False)
        """

        self.show_axis = show_axis
        self.camera_matrix = camera_matrix
        self.dist_coeffs = dist_coeffs

    def get_pose(self, frame, landmarks):
        """
        Estimate head pose using the head pose estimator object instantiated attribute
        Parameters
        ----------
        frame: numpy array
            Image/frame captured by the camera
        landmarks: dlib.rectangle
            Dlib detected 68 landmarks of the head
        Returns
        --------
        - if successful: image_frame, roll, pitch, yaw (tuple)
        - if unsuccessful: None,None,None,None (tuple)
        """
        self.keypoints = landmarks  # dlib 68 landmarks
        self.frame = frame  # opencv image array

        self.axis = np.float32([[200, 0, 0],
                                [0, 200, 0],
                                [0, 0, 200]])
        # array that specify the length of the 3 projected axis from the nose

        if self.camera_matrix is None:
            # if no camera matrix is given, estimate camera parameters using picture size
            self.size = frame.shape
            self.focal_length = self.size[1]
            self.center = (self.size[1] / 2, self.size[0] / 2)
            self.camera_matrix = np.array(
                [[self.focal_length, 0, self.center[0]],
                 [0, self.focal_length, self.center[1]],
                 [0, 0, 1]], dtype="double"
            )

        if self.dist_coeffs is None:  # if no distorsion coefficients are given, assume no lens distortion
            self.dist_coeffs = np.zeros((4, 1))

        # 3D Head model world space points (generic human head)
        self.model_points = np.array([
            (0.0, 0.0, 0.0),  # Nose tip
            (0.0, -330.0, -65.0),  # Chin
            (-225.0, 170.0, -135.0),  # Left eye left corner
            (225.0, 170.0, -135.0),  # Right eye right corner
            (-150.0, -150.0, -125.0),  # Left Mouth corner
            (150.0, -150.0, -125.0)  # Right mouth corner

        ])

运行

python main.py
``
#驾驶状态检测
检测到闭眼睛就会警报

```bash
python main.py --ear_time_tresh 5

5秒闭眼则在屏幕显示
在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/0a3221ddd在这里插入图片描述
c6342c5953cb9418a38cf52.png)

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
驾驶员精神状态检测是一个较为复杂的问题,需要使用多个传感器和算法进行处理。在这里,我提供一个简单的示例代码,以心率为指标,使用支持向量机算法进行分类。 首先,我们需要安装相应的库,如numpy、pandas、scikit-learn等。可以使用pip进行安装: ``` pip install numpy pandas scikit-learn ``` 然后,我们可以使用以下代码进行模型训练和预测: ```python import pandas as pd import numpy as np from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # 读取数据集 data = pd.read_csv('data.csv') # 提取特征和标签 X = data.drop('label', axis=1) y = data['label'] # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 建立SVM模型 model = SVC(kernel='linear', C=1, gamma='auto') # 训练模型 model.fit(X_train, y_train) # 预测测试集 y_pred = model.predict(X_test) # 计算准确率 accuracy = accuracy_score(y_test, y_pred) print('Accuracy:', accuracy) ``` 这里假设我们的数据集是一个csv文件,其中包含了心率和标签数据。我们将数据集读入后,使用train_test_split函数将数据集划分为训练集和测试集。然后,我们使用SVM算法建立模型,并将训练集输入模型进行训练。最后,使用测试集对模型进行测试,并计算准确率。 需要注意的是,这只是一个简单的示例代码,实际情况中需要根据实际情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿利同学

一角两角不嫌少

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值