前言
创建DLL有几种不同的方式,最简单直接的的方式就是通过VS自带的动态链接库(DLL)项目,把相关的功能封装成一个类,然后导出几个接口方法,编译成功以后生成DLL文件,就可以在其他地方跟正常的第三方库配置之后一样调用了。
01
创建DLL
首先打开VS创建一个DLL空项目,打开VS的桌面向导如下:
创建DLL链接库项目
然后在头文件中添加mytest.h文件,添加代码如下:
#pragma once
#ifdef _DLL_EXPORTS
#define DLL_API _declspec(dllexport)
#else
#define DLL_API _declspec(dllimport)
#endif
#include <opencv2/opencv.hpp>
//=========导出函数C++调用接口============
class DLL_API MyTestDLL {
public:
int addData(int a, int b);
~MyTestDLL();
};
然后在源文件中添加mytest.cpp文件,添加代码如下:
#define _DLL_EXPORTS
#include <mytest.h>
MyTestDLL::~MyTestDLL() {
std::cout << "destory instance done!" << std::endl;
}
int MyTestDLL::addData(int a, int b) {
int sum = 0;
sum = a + b;
std::cout << "sum: " << sum << std::endl;
return sum;
}
编译运行成功,显示如下:
02
DLL测试程序
创建测试程序,来测试DLL。新建一个控制台空项目,在源文件中添加main.cpp文件,然后添加如下代码:
#include "opencv2/opencv.hpp"
#include "mytest.h"
int main(int argc, char** argv) {
std::cout << "test mydll..." << std::endl;
std::shared_ptr<MyTestDLL> mydll(new MyTestDLL());
int sum = mydll->addData(3, 5);
std::cout << "DLL invoke result : " << sum << std::endl;
return 0;
}
运行结果如下:
YOLOv8对象检测DLL测试
采用相同的方法,基于ONNXRUNTIME深度学习模型库框架, 我对以前ONNXRUNTIME + YOLOv8对象检测C++代码稍作修改,封装了一个YOLOv8对象检测推理类为DLL,支持C++与C#接口调用。客户端调用代码如下:
#include "yolov8_infer.h"
#include <iostream>
#include <fstream>
std::string label_map = "D:/python/yolov5-7.0/classes.txt";
int main(int argc, char** argv) {
std::string names = "10:bike";
int pos = names.find_first_of(":");
std::cout << names.substr(0, pos) << " -->> " << names.substr(pos + 1) << std::endl;
std::vector<std::string> classNames;
std::ifstream fp(label_map);
std::string name;
while (!fp.eof()) {
getline(fp, name);
if (name.length()) {
classNames.push_back(name);
}
}
fp.close();
// std::shared_ptr<YOLOv5ORTDetector> detector(new YOLOv5ORTDetector());
std::shared_ptr<YOLOv8ORTDetector> detector(new YOLOv8ORTDetector());
detector->initConfig("D:/python/my_yolov8_train_demo/yolov8n.onnx", 640, 640, 0.25f, 0.5);
cv::VideoCapture capture("D:/images/video/sample.mp4");
cv::Mat frame;
std::vector<DetectResult> results;
while (true) {
bool ret = capture.read(frame);
if (frame.empty()) {
break;
}
int64 start = cv::getTickCount();
detector->detect(frame, results);
float fps = static_cast<float>(cv::getTickFrequency()) / (cv::getTickCount() - start);
cv::putText(frame, cv::format("FPS: %.2f", fps), cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 0, 255), 2, 8);
for (DetectResult dr : results) {
cv::Rect box = dr.box;
cv::putText(frame, classNames[dr.classId], cv::Point(box.tl().x, box.tl().y - 10), cv::FONT_HERSHEY_SIMPLEX, .5, cv::Scalar(0, 0, 0));
}
cv::imshow("YOLOv8 + ONNXRUNTIME - DLL导出演示", frame);
char c = cv::waitKey(1);
if (c == 27) { // ESC 退出
break;
}
// reset for next frame
results.clear();
}
return 0;
}
运行截图如下: