C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
(会自动合并到对应文件夹)nvcc -V
进入官网直接下载, 提取并安装 https://opencv.org/opencv-3-4.html
注意将dll所在的目录D:\opencv\build\x64\vc15\bin
添加到环境变量.
安装社区版, 成功后, 将CUDA的扩展文件复制到VS目录.
注: 可以先在VS中编译DarkNet, 根据错误信息, 确定具体目录. 我这里是从下图
复制到这里 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\BuildCustomizations
.\darknet-master\build\darknet
目录, 进入.\darknet-master\build\darknet
中, 没有GPU的打开darknet_no_gpu.sln,有GPU的打开 darknet.sln.右键项目-->重定向项目
. 升不升级无所谓.将项目换成 yolo_cpp_dll.sln, 按上述步骤重新生成即可.
yolov3.weights
放到 .\darknet-master\build\darknet\x64
目录darknet_yolo_v3.cmd
, 出现下图所示即运行成功.darknet.exe detector demo data\coco.data yolov3.cfg yolov3.weights
其他请参考 https://blog.csdn.net/KID_yuan/article/details/88380269
整个项目已经上传 点击下载 , 只需要更改opencv的属性表配置即可release运行
.\darknet-master\build\darknet\x64
)
.\darknet-master\build\darknet\x64\data
.\darknet-master\build\darknet\x64
下载链接: https://pjreddie.com/media/files/yolov3.weights
.\darknet-master\include
#include
#define OPENCV // 启用opencv
#define GPU // 启用GPU
#include "yolo_v2_class.hpp"
#include
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);
}
}
}
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 = "../params/coco.names";
std::string cfg_file = "../params/yolov3.cfg";
std::string weights_file = "../params/yolov3.weights";
Detector detector(cfg_file, weights_file);//初始化检测器
auto obj_names = objects_names_from_file(names_file);//获得分类对象名称
// 打开默认摄像头
cv::VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
printf("摄像头打开失败");
return -1;
}
// 实时检测
cv::Mat frame;
while(true)
{
capture >> frame;
std::vector<bbox_t> result_vec = detector.detect(frame);
draw_boxes(frame, result_vec, obj_names);
cv::imshow("YOLO v3 Camera", frame);
if (cv::waitKey(1) == 27) break; // ESC键退出
}
cv::destroyAllWindows();
return 0;
}