1.编译cpu版本darknet生成动态链接库
先将cpu版本的yolo_cpp_dll_no_gpu进行编译,在Release版本下编译生成yolo_cpp_dll.dll和yolo_cpp_dll_no_gpu.lib,并将yolo_cpp_dll.dll改名为yolo_cpp_dll_no_gpu.dll
2.新建一个qt工程,release文件下放入4个动态链接库dll文件,包括opencv_world340.dll,另外3个dll文件都在darknet-master\build\darknet\x64下找到
3.main.cpp所在目录下放入yolo_cpp_dll_no_gpu.lib文件,3个Yolo模型文件voc.names/cfg/weights,以及yolo_v2_class.hpp头文件,并在qt工程的头文件点击添加现有文件yolo_v2_class.hpp(yolo_v2_class.hpp在darknet-master\build\darknet\yolo_console_dll打开后可以看到,复制到记事本里)
4.加入代码
#include "imageProcess.h"
#include "stdafx.h"
#include <fstream>//忘记添加导致出现"不允许使用不完整的类型"错误
#define OPENCV //define OPENCV必须要在include "yolo_v2_class.hpp" 之前
#include "yolo_v2_class.hpp" //引用动态链接库中的头文件
#pragma comment(lib, "yolo_cpp_dll_no_gpu.lib") //引入YOLO动态链接库
CImgProcess::CImgProcess()
{
}
CImgProcess::~CImgProcess()
{
// Stop media playback
}
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;
}
void CImgProcess::ImgProcess_second()//图像处理函数
{
std::string names_file = "voc.names";
std::string cfg_file = "yolov3-voc.cfg";
std::string weights_file = "yolov3-voc_last.weights";
Detector detector(cfg_file, weights_file, 1); //初始化检测器
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);
double time0 = (double)(getTickCount());
std::vector<bbox_t> result_vec = detector.detect(img);
for (auto &i : result_vec)
{
cv::Scalar color = obj_id_to_color(i.obj_id);
cv::rectangle(img, cv::Rect(i.x, i.y, i.w, i.h), color, 2);
}
totaltime = 1000 * ((double)getTickCount() - time0) / getTickFrequency();//1000 *总次数/一秒内重复的次数= 时间(ms)
}