yolo 海思嵌入式_YOLO动态链接库的编译和使用 悠悠 2022-11-01 05:41 121阅读 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 即可看到结果 ![3ae57d93aaeefdb4f0504f78c56cffe2.png][] 讲到这里,其实没有什么难点,基本也没什么值得写一篇博客的东西,下面就放出点硬货,看看怎么用简单的代码调用Yolo的dll,代码如下: \#include \#ifdef \_WIN32 \#define OPENCV \#define GPU \#endif \#include "yolo\_v2\_class.hpp"// imported functions from DLL \#include // C++ \#include "opencv2/highgui/highgui.hpp" \#pragma comment(lib, "opencv\_world340.lib")//引入链接库 void draw\_boxes(cv::Mat mat\_img, std::vector result\_vec, std::vector<: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<:string> objects\_names\_from\_file(std::string const filename) \{ std::ifstream file(filename); std::vector<: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 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训练自己的数据集。 [3ae57d93aaeefdb4f0504f78c56cffe2.png]: /images/20221024/850c0c46ccc34f9b82294709dc67958c.png
还没有评论,来说两句吧...