微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
前言
01
创建DLL
#pragma once
#ifdef _DLL_EXPORTS
#define DLL_API _declspec(dllexport)
#else
#define DLL_API _declspec(dllimport)
#endif
#include
//=========导出函数C++调用接口============
class DLL_API MyTestDLL {
public:
int addData(int a, int b);
~MyTestDLL();
};
#define _DLL_EXPORTS
#include
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测试程序
#include "opencv2/opencv.hpp"
#include "mytest.h"
int main(int argc, char** argv) {
std::cout << "test mydll..." << std::endl;
std::shared_ptr mydll(new MyTestDLL());
int sum = mydll->addData(3, 5);
std::cout << "DLL invoke result : " << sum << std::endl;
return 0;
}
YOLOv8对象检测DLL测试
#include "yolov8_infer.h"
#include
#include
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 detector(new YOLOv5ORTDetector());
std::shared_ptr 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 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;
}
推荐阅读
OpenCV4.8+YOLOv8对象检测C++推理演示
总结 | OpenCV4 Mat操作全接触
三行代码实现 TensorRT8.6 C++ 深度学习模型部署
实战 | YOLOv8+OpenCV 实现DM码定位检测与解析
对象检测边界框损失 – 从IOU到ProbIOU
YOLOv8 OBB实现自定义旋转对象检测
初学者必看 | 学习深度学习的五个误区
YOLOv8自定义数据集训练实现安全帽检测