- 在新建工程下添加以下文件
模型相关:
obj.names
yolo-obj.cfg
yolo-obj_final.weights
Opencv库文件:
opencv_world320.dll
opencv_world320d.dll
darknet-master\build\darknet\x64
路径下的:
pthreadGC2.dll
pthreadVC2.dll
编译yolo_cpp_dll.sln
产生的:
yolo_cpp_dll.dll
yolo_cpp_dll.lib
2.包含目录:
C:\darknet-master\include
C:\Users\jack\Documents\Visual Studio 2015\Projects\GetScaleImgPart\x64
库目录:
C:\Users\jack\Documents\Visual Studio 2015\Projects\GetScaleImgPart\GetScaleImgPart
预处理器
OPENCV;GPU;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
附加依赖项
yolo_cpp_dll.lib;%(AdditionalDependencies)
2.代码的调用
#include <opencv2/opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2/core/core.hpp>
#include "yolo_v2_class.hpp" // imported functions from DLL
#include <fstream>
#define OPENCV
#define GPU
#pragma comment(lib, "yolo_cpp_dll.lib")
#pragma comment(lib, "opencv_world320.lib")//引入链接库
using namespace std;
using namespace cv;
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(int argc, char* argv[])
{
char* path;
path = "C:\\Users\\jcak\\Desktop\\检测结果\\ROI.bmp";
std::string names_file = "obj.names";
std::string cfg_file = "yolo-obj.cfg";
std::string weights_file = "yolo-obj_final.weights";
Detector detector(cfg_file, weights_file, 0); //初始化检测器
//std::vector<std::string> obj_names = objects_names_from_file(names_file); //调用获得分类对象名称
//或者使用以下四行代码也可实现读入分类对象文件
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::VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
printf("文件打开失败");
}
float m_fProbThres = 0.25;
cv::Mat frame = imread(path);
std::vector<bbox_t> result_vec = detector.detect(path);
draw_boxes(frame, result_vec, obj_names);
cv::namedWindow("test", WINDOW_AUTOSIZE);
cv::imshow("test", frame);
cv::waitKey(30000);
printf("yolo\n");
return 0;
}
3.出现的问题
3.1obj_id_to_color
不可识别,因其定义在宏OPENCV下,但开头的
#define OPENCV
#define GPU
不起作用,因此在属性页的预处理器设置里改为
就解决了>-<