微信公眾號(hào):OpenCV學(xué)堂
關(guān)注獲取更多計(jì)算機(jī)視覺(jué)與深度學(xué)習(xí)知識(shí)
前言
01
創(chuàng)建DLL


#pragma?once
#ifdef?_DLL_EXPORTS
#define?DLL_API?_declspec(dllexport)
#else
#define?DLL_API?_declspec(dllimport)
#endif
#include?
//=========導(dǎo)出函數(shù)C++調(diào)用接口============
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:?"?<std::endl;
????return?sum;
}
02
DLL測(cè)試程序
#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?:?"?<std::endl;
????return?0;
} 

YOLOv8對(duì)象檢測(cè)DLL測(cè)試
#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?<0,?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導(dǎo)出演示",?frame);
????????char?c?=?cv::waitKey(1);
????????if?(c?==?27)?{?//?ESC?退出
????????????break;
????????}
????????//?reset?for?next?frame
????????results.clear();
????}
????return?0;
} 

掃碼查看 CV系統(tǒng)化學(xué)習(xí)路線圖(OpenCV+Pytorch)
