下载yolov4的框架
https://github.com/AlexeyAB/darknet
解压darknet-master.zip
打开darknet-master文件夹
在CMaketLists.txt中找到if(ENABLE_OPENCV)
添加 set(OpenCV_DIR E:/cuda_opencv/opencv/build/install)
打开powershell控制台
输入 .\build.ps1
可能会出现没有权限, 跟打开cmd一样在外部打开,解锁权限
训练自己的数据集,参考
https://mp.youkuaiyun.com/console/editor/html/107311683
生成dll与lib文件
打开H:\darknet-master\build\darknet文件,如果是GPU版本,点击yolo_cpp_dll.sln,改为Release X64运行
打开yolo_cpp_dll.vcxproj,如果cuda版本不是10.0,需要修改
配置yolo_cpp_dll.sln
项目 --> 属性 --> 配置属性 --> C/C++ --> 预处理器 --> 取消预处理器定义,查看所需预处理器是否被禁用
项目 --> 属性 --> 配置属性 -->CUDA C/C++ --> Device --> Coda Generation
将compute_30, sm_30,compute_75, sm_75改为自己的GPU运算能力
compute_61, sm_61(GTX 1080Ti)
通过https://blog.youkuaiyun.com/zyb418/article/details/87972608中显卡配置,找到自己电脑的计算能力
如果opencv还是不行,配置opencv
项目 --> 属性 --> 配置属性 -->VC++目录 --> 包含目录
E:\cuda_opencv\opencv\build\install\include
E:\cuda_opencv\opencv\build\install\include\opencv2
项目 --> 属性 --> 配置属性 -->VC++目录 --> 库目录
E:\cuda_opencv\opencv\build\install\x64\vc15\lib
项目 --> 属性 --> 配置属性 -->链接器 --> 输入 --> 附加依赖项
添加opencv_world420.lib
配置yolo_cpp_dll.sln的项目 --> 属性 --> 配置属性 --> C/C++ --> 附加包含目录
将第一行改为E:\cuda_opencv\opencv\build\install\include
C++调用 (Release X64)
项目 --> 属性 --> 配置属性 -->VC++目录 --> 包含目录
H:\darknet-master\include
E:\cuda_opencv\opencv\build\install\include
E:\cuda_opencv\opencv\build\install\include\opencv2
项目 --> 属性 --> 配置属性 -->链接器 --> 输入 --> 附加依赖项
添加opencv_world420.lib、yolo_cpp_dll.lib
将lib、dll文件放入D:\Q\c++\Project1\x64\Release
#include <iostream>
#ifdef _WIN32
#define OPENCV
#define GPU
#endif
#include <opencv2/opencv.hpp>
#include "darknet/yolo_v2_class.hpp" //引用动态链接库中的头文件
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
#pragma comment(lib, "yolo_cpp_dll.lib") //引入YOLO动态链接库
//#pragma comment(lib, "opencv_world340d.lib") //引入OpenCV链接库,我在vs中配置过了,如果没配置请对应修改
using namespace cv;
//以下两段代码来自yolo_console_dll.sln
void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
int current_det_fps = -1, int current_cap_fps = -1)
{
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);
}
}
if (current_det_fps >= 0 && current_cap_fps >= 0) {
std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) + " FPS capture: " + std::to_string(current_cap_fps);
putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2);
}
}
std::vector<std::string> 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;
}
int main()
{
std::string names_file = "E:/darknet-master-gpu/build/darknet/x64/data/myData.names";
std::string cfg_file = "E:/darknet-master-gpu/build/darknet/x64/cfg/my_yolov4.cfg";
std::string weights_file = "E:/darknet-master-gpu/build/darknet/x64/backup/my_yolov4_final.weights";
Detector detector(cfg_file, weights_file, 0); //初始化检测器
std::vector<std::string> obj_names;
std::ifstream ifs(names_file.c_str());
std::string line;
while (getline(ifs, line)) obj_names.push_back(line);
//测试是否成功读入分类对象文件
//for (size_t i = 0; i < obj_names.size(); i++)
//{
// std::cout << obj_names[i] << std::endl;
//}
cv::Mat frame = imread("D:/hyd/xinfeng/test/0.jpg");
std::cout << detector.cur_gpu_id << std::endl;
std::vector<bbox_t> result_vec = detector.detect(frame);
draw_boxes(frame, result_vec, obj_names);
cv::imshow("test", frame);
waitKey();
return 0;
}

本文档详述了如何在Windows环境下配置YoloV4的GPU版本,包括下载darknet框架,修改CMakeLists.txt文件设置OpenCV路径,解决权限问题,配置解决方案以匹配GPU计算能力,并调整opencv相关库的路径和依赖项。
6527

被折叠的 条评论
为什么被折叠?



