• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

YOLO动态链接库的编译和使用

人工智能 _寒潭雁影 1443次浏览 0个评论

前面一篇文章我们介绍了怎么在win10下搭建Yolo v3的开发环境。

 

这篇文章我们将着重介绍YOLO动态链接库的编译,还有如何正确的使用编译出来的Yolo动态链接库进行目标的识别。

 

首先是编译,对应的工程是“yolo_cpp_dll.sln”,其环境的配置跟上篇文章所讲的“darknet.sln”可以说完全一样,因此不再赘述。需要注意的是这里我们直接编译“release x64”版本,不去管debug版本(因为debug用的不是OpenCV库)。

 

做完上面的操作,会在“xx\build\darknet\x64”路径下生成“yolo_cpp_dll.lib”和“yolo_cpp_dll.dll”两个文件,这两个东西就是yolo的动态链接库。

 

使用链接库的示例工程是“yolo_console_dll.sln”,其配置基本同上(不同的是不需要改CUDA项)。编译出来后,“xx\build\darknet\x64”路径下多出一个“yolo_console_dll.exe”可执行文件,用控制台打开并输入

 

.\yolo_console_dll.exe data/coco.names cfg/yolov3.cfg yolov3.weights test.avi

即可看到结果

  YOLO动态链接库的编译和使用  

讲到这里,其实没有什么难点,基本也没什么值得写一篇博客的东西,下面就放出点硬货,看看怎么用简单的代码调用Yolo的dll,代码如下:

 

#include <iostream>
 
#ifdef _WIN32
#define OPENCV
#define GPU
#endif
 
#include "yolo_v2_class.hpp"	// imported functions from DLL
#include <opencv2/opencv.hpp>	// C++
#include "opencv2/highgui/highgui.hpp"  
 
#pragma comment(lib, "opencv_world340.lib")//引入链接库
 
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 = "data/coco.names";
	std::string  cfg_file = "cfg/yolov3.cfg";
	std::string  weights_file = "yolov3.weights";
	Detector detector(cfg_file, weights_file);//初始化检测器
	auto obj_names = objects_names_from_file(names_file);//获得分类对象名称
 
	cv::VideoCapture capture;
	capture.open("test.avi");
	if (!capture.isOpened())
	{
		printf("文件打开失败");
	}
	//获取整个帧数  
	long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
	cv::Mat frame;
	for (size_t i = 0; i < totalFrameNumber; i++)
	{
		capture >> frame;
		std::vector<bbox_t> result_vec = detector.detect(frame);
		draw_boxes(frame, result_vec, obj_names);
		cv::imshow("window name1", frame);
		cv::waitKey(3);
	}
	return 0;
}

 

注意,上面的代码并不是最简的,但为了能有较好的演示效果,本人只简化到这一步,如果有需要可以再把“draw_boxes”和“objects_names_from_file”进行改造。

 

另外,如果想要自己创建一个工程然后添加Yolo的dll库,有下面的点需要注意

 

1.需要包含OpenCV库

 

2.需要cfg,names,weights这三个网络相关文件

 

3.需要如下所示的五个链接库

 

4.要注意“yolo_v2_class.hpp”文件的包含

 

OK,没有了,下篇将介绍怎么用YOLO训练自己的数据集。

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明YOLO动态链接库的编译和使用
喜欢 (0)

您必须 登录 才能发表评论!

加载中……