1、环境准备:
1.1、下载Yolov3(darknet)
下载链接:https://github.com/AlexeyAB/darknet1.2、下载权重文件(yolov3.weights)
下载链接:https://link.csdn.net/?target=https%3A%2F%2Fpjreddie.com%2Fmedia%2Ffiles%2Fyolov3.weights 下载完成后,将yolov3.weights文件复制到E:\yolo\darknet-master\darknet-master\build\darknet\x64目录下。1.3、下载CUDA 9.0
下载链接:链接:https://pan.baidu.com/s/1LLMmFVOCSLvaY4GzTuVYcg 提取码:kkvb 安装过程网上搜教程就行了,挺简单的(PS:CUDA版本需根据自己电脑配置选择,我电脑显卡是:NVIDIA GTX 950M的,一开始我装CUDA10.0和11.0都不行,后来改为9.0可以了,也是一波三折)1.4、下载CUDNN 9.0
下载链接:链接:https://pan.baidu.com/s/1d-yigqIoy9q6Ha5-fdJkSA 提取码:74kw 将里面3个文件夹里的每个文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0目录下对应的文件夹中即可。1.4、opencv3.4.9和VS2015
网上搜索安装即可并将opencv配置到VS2015中2、编译Yolo
2.1、编译darknet.exe
2.1.1、找到darknet.vcxproj用EditPlus打开CTRL+F搜索CUDA 11.1全部改为9.0然后保存。共2处
2.1.2、打开darknet.sln
修改为Release X64模式,然后生成即可。3、运行yolov3
双击darknet_yolo_v3.cmd识别图片实例;而darknet_web_cam_voc.cmd是打开笔记本摄像头动态检测识别。4、编译Yolov3动态链接库
打开yolo_cpp_dll.sln在Release X64模式下生成(也要修改yolo_cpp_dll.vcxproj中的cuda版本) 会生成yolo_cpp_dll.dll和yolo_cpp_dll.lib的库文件5、使用yolo的动态链接库进行开发
使用yolo编译好的yolo_cpp_dll.dll和yolo_cpp_dll.lib的库文件和API:yolo_v2_class.hpp头文件 mainwindow.h#pragma once //这段代码一定要加,这是在yolo_v2_class.hpp文件中使用opencv函数 #ifdef _WIN32 #define OPENCV #define GPU #endif #include <QtWidgets/QMainWindow> #include "ui_mianwindow.h" #include "yolo_v2_class.hpp" #include "opencv\highgui.h" #include "opencv2\opencv.hpp" #include "opencv2\core\core.hpp" #include "opencv2\highgui\highgui.hpp" #include <iostream> #include <stdio.h> #pragma comment(lib, "yolo_cpp_dll.lib")//引入yolo链接库 #pragma execution_character_set("utf-8") using namespace cv; class mianwindow : public QMainWindow { Q_OBJECT public: mianwindow(QWidget *parent = Q_NULLPTR); void draw_boxes(Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names); std::vector<std::string> objects_names_from_file(std::string const filename); QImage cvMat2QImage(const Mat& mat); Mat QImage2cvMat(QImage image); private: Ui::mianwindowClass ui; };
mainwindow.cpp
#include "mianwindow.h" #include "yolo_v2_class.hpp" #include <QDebug> mianwindow::mianwindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QImage q_image(QString("E:/TX_workCode/DataInfo/images/dog.jpg")); ui.label_img->setPixmap(QPixmap::fromImage(q_image)); std::string names_file = "coco.names"; std::string cfg_file = "yolov3.cfg"; std::string weights_file = "yolov3.weights"; Detector detector(cfg_file, weights_file); //初始化Detector std::vector<std::string> obj_names; obj_names = objects_names_from_file(names_file); //测试是否成功读入分类对象文件 for (int i = 0; i < obj_names.size(); i++) { qDebug() << "第i个:" << QString(QString::fromLocal8Bit(obj_names[i].c_str())); } Mat frame = QImage2cvMat(q_image); std::vector<bbox_t> result_vec = detector.detect(frame); draw_boxes(frame, result_vec, obj_names); QImage det_image = cvMat2QImage(frame); ui.label_detectorImg->setPixmap(QPixmap::fromImage(det_image)); } //以下draw_boxes和objects_names_from_file两段代码来自yolo_console_dll.sln //给Mat类型的图像画出检测出对象框 void mianwindow::draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names) { int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } }; for (auto &i : result_vec) { cv::Scalar color = obj_id_to_color(i.obj_id); cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), color, 2); if (obj_names.size() > i.obj_id) { std::string obj_name = obj_names[i.obj_id]; if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id); cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0); int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2); cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 30, 0)), cv::Point2f(std::min((int)i.x + max_width, mat_img.cols - 1), std::min((int)i.y, mat_img.rows - 1)), color, CV_FILLED, 8, 0); putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2); } } } //读names文件中对象类别名称保存在vector<std::string>容器中 std::vector<std::string> mianwindow::objects_names_from_file(std::string const filename) { std::ifstream file(filename); std::vector<std::string> file_lines; if (!file.is_open()) return file_lines; for (std::string line; getline(file, line);) file_lines.push_back(line); std::cout << "object names loaded \n"; return file_lines; } //---------QImage与Mat类型相互转换---------------------------------------------------------- QImage mianwindow::cvMat2QImage(const cv::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 *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 = 3 else if (mat.type() == CV_8UC3) { // Copy input Mat const uchar *pSrc = (const uchar*)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) { qDebug() << "CV_8UC4"; // Copy input Mat const uchar *pSrc = (const uchar*)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 { qDebug() << "ERROR: Mat could not be converted to QImage."; return QImage(); } } cv::Mat mianwindow::QImage2cvMat(QImage image) { cv::Mat mat; qDebug() << image.format(); switch (image.format()) { case QImage::Format_ARGB32: case QImage::Format_RGB32: case QImage::Format_ARGB32_Premultiplied: mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine()); break; case QImage::Format_RGB888: mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine()); cv::cvtColor(mat, mat, CV_BGR2RGB); break; case QImage::Format_Indexed8: mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine()); break; } return mat; }