0

0

深度学习:驾驶行为分析

爱谁谁

爱谁谁

发布时间:2025-07-13 09:48:14

|

846人浏览过

|

来源于php中文网

原创

一、功能与环境说明

程序功能简介: 利用yolo进行训练,通过OpenCV调用实现对打哈欠、使用手机、抽烟、系安全带以及佩戴口罩的检测。

已测试的系统环境包括: Windows系统、Linux系统、32位嵌入式Linux系统、64位嵌入式Linux系统。

已测试的硬件环境包括: 普通笔记本电脑(搭载i3、i5、i7处理器)、RK3399、树莓派4B。

yolo环境搭建方法:请参考https://pjreddie.com/darknet/yolo/

darknet框架安装教程:请参考https://pjreddie.com/darknet/install/

二、OpenCV调用代码

2.1 .h头文件代码

免费语音克隆
免费语音克隆

这是一个提供免费语音克隆服务的平台,用户只需上传或录制一段 5 秒以上的清晰语音样本,平台即可生成与用户声音高度一致的 AI 语音克隆。

下载
#ifndef SDK_THREAD_H
#define SDK_THREAD_H
<h1>include <qthread></h1><h1>include <qimage></h1><h1>include "opencv2/core/core.hpp"</h1><h1>include "opencv2/core/core_c.h"</h1><h1>include "opencv2/objdetect.hpp"</h1><h1>include "opencv2/highgui.hpp"</h1><h1>include "opencv2/imgproc.hpp"</h1><h1>include <fstream></h1><h1>include <sstream></h1><h1>include <iostream></h1><h1>include <opencv2></h1><h1>include <opencv2></h1><h1>include <vector></h1><h1>include <qdebug></h1><p>using namespace cv;
using namespace std;
using namespace dnn;</p><p>//视频音频编码线程
class SDK_Thread: public QThread
{
Q_OBJECT
public:
void postprocess(Mat& frame, const vector<Mat>& outs, float confThreshold, float nmsThreshold);
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);
vector<string> getOutputsNames(Net&net);
QImage Mat2QImage(const Mat& mat);
Mat QImage2cvMat(QImage image);
protected:
void run();
signals:
void LogSend(QString text);
void VideoDataOutput(QImage); //输出信号
};</p><p>extern QImage save_image;
extern bool sdk_run_flag;
extern string names_file;
extern String model_def;
extern String weights;</p><h1>endif // SDK_THREAD_H

2.2 .cpp文件代码

#include "sdk_thread.h"</h1><p>QImage save_image; //用于行为分析的图片
bool sdk_run_flag=1;</p><p>Mat SDK_Thread::QImage2cvMat(QImage image)
{
Mat mat;
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = Mat(image.height(), image.width(), CV_8UC4, (void<em>)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = Mat(image.height(), image.width(), CV_8UC3, (void</em>)image.constBits(), image.bytesPerLine());
cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}</p><p>QImage SDK_Thread::Mat2QImage(const Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar <em>pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar </em>pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar <em>pSrc = (const uchar</em>)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
// Copy input Mat
const uchar <em>pSrc = (const uchar</em>)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}</p><p>vector<string> SDK_Thread::getOutputsNames(Net&net)
{
static vector<string> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector<string> layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
{
names[i] = layersNames[outLayers[i] - 1];
}
}
return names;
}</p><p>void SDK_Thread::postprocess(Mat& frame, const vector<Mat>& outs, float confThreshold, float nmsThreshold)
{
vector<int> classIds;
vector<float> confidences;
vector<Rect> boxes;
for (size_t i = 0; i < outs.size(); ++i)
{
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class with the highest score.
float<em> data = (float</em>)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confThreshold)
{
int centerX = (int)(data[0] <em> frame.cols);
int centerY = (int)(data[1] </em> frame.rows);
int width = (int)(data[2] <em> frame.cols);
int height = (int)(data[3] </em> frame.rows);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame);
}
}</p><p>void SDK_Thread::run()
{
Net net = readNetFromDarknet(model_def, weights);
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);</p><pre class="brush:php;toolbar:false;"><code>ifstream ifs(names_file.c_str());
string line;
vector<string> classes;
while (getline(ifs, line)) classes.push_back(line);

Mat frame, blob;
int inpWidth = 416;
int inpHeight = 416;
float thresh = 0.5;
float nms_thresh = 0.4;

while(sdk_run_flag)
{
    //capture >> frame;
    //得到源图片
    //use_image.load("D:/linux-share-dir/car_1.jpg");
    frame_src=QImage2cvMat(save_image);
    cvtColor(frame_src, frame, CV_RGB2BGR);
    //frame=imread("D:/linux-share-dir/car_1.jpg");
    //frame=imread("D:/linux-share-dir/5.jpg");
    //in_w=save_image.width();
    //in_h=save_image.height();
    blobFromImage(frame, blob, 1/255.0, cvSize(inpWidth, inpHeight), Scalar(0,0,0), true, false);
    vector<Mat> mat_blob;
    imagesFromBlob(blob, mat_blob);
    //Sets the input to the network
    net.setInput(blob);
    // 运行前向传递以获取输出层的输出
    vector<Mat> outs;
    net.forward(outs, getOutputsNames(net));
    postprocess(frame, outs, thresh, nms_thresh);
    vector<double> layersTimes;
    double freq = getTickFrequency() / 1000;
    double t = net.getPerfProfile(layersTimes) / freq;
    // string label = format("time : %.2f ms", t);
    //putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
    LogSend(tr("识别结束---消耗的时间:%1 秒\n").arg(t/1000));
    //得到处理后的图像
    use_image=Mat2QImage(frame);
    use_image=use_image.rgbSwapped();
    VideoDataOutput(use_image.copy());
}

}

三、开发测试效果图

深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析

四、车载角度测试效果图

深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析深度学习:驾驶行为分析

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
windows查看端口占用情况
windows查看端口占用情况

Windows端口可以认为是计算机与外界通讯交流的出入口。逻辑意义上的端口一般是指TCP/IP协议中的端口,端口号的范围从0到65535,比如用于浏览网页服务的80端口,用于FTP服务的21端口等等。怎么查看windows端口占用情况呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

1496

2023.07.26

查看端口占用情况windows
查看端口占用情况windows

端口占用是指与端口关联的软件占用端口而使得其他应用程序无法使用这些端口,端口占用问题是计算机系统编程领域的一个常见问题,端口占用的根本原因可能是操作系统的一些错误,服务器也可能会出现端口占用问题。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

1170

2023.07.27

windows照片无法显示
windows照片无法显示

当我们尝试打开一张图片时,可能会出现一个错误提示,提示说"Windows照片查看器无法显示此图片,因为计算机上的可用内存不足",本专题为大家提供windows照片无法显示相关的文章,帮助大家解决该问题。

835

2023.08.01

windows查看端口被占用的情况
windows查看端口被占用的情况

windows查看端口被占用的情况的方法:1、使用Windows自带的资源监视器;2、使用命令提示符查看端口信息;3、使用任务管理器查看占用端口的进程。本专题为大家提供windows查看端口被占用的情况的相关的文章、下载、课程内容,供大家免费下载体验。

463

2023.08.02

windows无法访问共享电脑
windows无法访问共享电脑

在现代社会中,共享电脑是办公室和家庭的重要组成部分。然而,有时我们可能会遇到Windows无法访问共享电脑的问题。这个问题可能会导致数据无法共享,影响工作和生活的正常进行。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

2361

2023.08.08

windows自动更新
windows自动更新

Windows操作系统的自动更新功能可以确保系统及时获取最新的补丁和安全更新,以提高系统的稳定性和安全性。然而,有时候我们可能希望暂时或永久地关闭Windows的自动更新功能。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

874

2023.08.10

windows boot manager
windows boot manager

windows boot manager无法开机的解决方法:1、系统文件损坏,使用Windows安装光盘或USB启动盘进入恢复环境,选择修复计算机,然后选择自动修复;2、引导顺序错误,进入恢复环境,选择命令提示符,输入命令"bootrec /fixboot"和"bootrec /fixmbr",然后重新启动计算机;3、硬件问题,使用硬盘检测工具进行扫描和修复;4、重装操作系统。本专题还提供其他解决

1976

2023.08.28

windows锁屏快捷键
windows锁屏快捷键

windows锁屏快捷键是Windows键+L、Ctrl+Alt+Del、Windows键+D、Windows键+P和Windows键+R。本专题为大家提供windows相关的文章、下载、课程内容,供大家免费下载体验。

1670

2023.08.30

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 10.5万人学习

Git 教程
Git 教程

共21课时 | 4.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号