一、功能与环境说明
程序功能简介: 利用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头文件代码
MOKI
MOKI是美图推出的一款AI短片创作工具,旨在通过AI技术自动生成分镜图并转为视频素材。
375 查看详情
#ifndef SDK_THREAD_H#define SDK_THREAD_Hinclude
include
include "opencv2/core/core.hpp"
include "opencv2/core/core_c.h"
include "opencv2/objdetect.hpp"
include "opencv2/highgui.hpp"
include "opencv2/imgproc.hpp"
include
include
include
include
include
include
include
using namespace cv;using namespace std;using namespace dnn;
//视频音频编码线程class SDK_Thread: public QThread{Q_OBJECTpublic:void postprocess(Mat& frame, const vector& outs, float confThreshold, float nmsThreshold);void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);vector getOutputsNames(Net&net);QImage Mat2QImage(const Mat& mat);Mat QImage2cvMat(QImage image);protected:void run();signals:void LogSend(QString text);void VideoDataOutput(QImage); //输出信号};
extern QImage save_image;extern bool sdk_run_flag;extern string names_file;extern String model_def;extern String weights;
endif // SDK_THREAD_H
2.2 .cpp文件代码
#include "sdk_thread.h"QImage save_image; //用于行为分析的图片bool sdk_run_flag=1;
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)image.constBits(), image.bytesPerLine());break;case QImage::Format_RGB888:mat = Mat(image.height(), image.width(), CV_8UC3, (void)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;}
QImage SDK_Thread::Mat2QImage(const Mat& mat){// 8-bits unsigned, NO. OF CHANNELS = 1if(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 Matuchar pSrc = mat.data;for(int row = 0; row < mat.rows; row ++){uchar pDest = image.scanLine(row);memcpy(pDest, pSrc, mat.cols);pSrc += mat.step;}return image;}// 8-bits unsigned, NO. OF CHANNELS = 3else if(mat.type() == CV_8UC3){// Copy input Matconst uchar pSrc = (const uchar)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);return image.rgbSwapped();}else if(mat.type() == CV_8UC4){// Copy input Matconst uchar pSrc = (const uchar)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);return image.copy();}else{return QImage();}}
vector SDK_Thread::getOutputsNames(Net&net){static vector names;if (names.empty()){//Get the indices of the output layers, i.e. the layers with unconnected outputsvector outLayers = net.getUnconnectedOutLayers();//get the names of all the layers in the networkvector layersNames = net.getLayerNames();// Get the names of the output layers in namesnames.resize(outLayers.size());for (size_t i = 0; i < outLayers.size(); ++i){names[i] = layersNames[outLayers[i] - 1];}}return names;}
void SDK_Thread::postprocess(Mat& frame, const vector& outs, float confThreshold, float nmsThreshold){vector classIds;vector confidences;vector 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 data = (float)outs[i].data;for (int j = 0; j confThreshold){int centerX = (int)(data[0] frame.cols);int centerY = (int)(data[1] frame.rows);int width = (int)(data[2] frame.cols);int height = (int)(data[3] 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 confidencesvector 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);}}
void SDK_Thread::run(){Net net = readNetFromDarknet(model_def, weights);net.setPreferableBackend(DNN_BACKEND_OPENCV);net.setPreferableTarget(DNN_TARGET_CPU);
ifstream ifs(names_file.c_str());string line;vector 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_blob; imagesFromBlob(blob, mat_blob); //Sets the input to the network net.setInput(blob); // 运行前向传递以获取输出层的输出 vector outs; net.forward(outs, getOutputsNames(net)); postprocess(frame, outs, thresh, nms_thresh); vector 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());}}
三、开发测试效果图
四、车载角度测试效果图
以上就是深度学习:驾驶行为分析的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/815229.html
微信扫一扫
支付宝扫一扫