YOLOV8 Onnxruntime OpenCV C++ VS2027部署推理 - 2、打开VS2019创建一个空项目
背景
最近需要开发一款目标检测的应用,需要在windows设备上基于CPU部署测试,虽然相关目标检测模型部署的帖子很多,但是参考了几个操作下来还是有奇奇怪怪的各种问题,我花了两天时间终于跑起来了,在这里记录一下操作流程,免得以后有需要了还要从头来一遍。
参考案例
YOLOv8部署到C++上(综合版笔记)_yolov8 c++-CSDN博客
C++ OpenCV onnxruntime调用yolov8 onnx模型_哔哩哔哩_bilibili
操作步骤
1、首先到github获取项目案例代码
ultralytics/examples/YOLOv8-ONNXRuntime-CPP at main · ultralytics/ultralytics (github.com)
2、打开VS2019创建一个空项目
我放在自己的路径下了
E:\Coding_task\yolov8_onnxruntime
3、把上面的三个代码文件添加到项目中来
4、本项目需要使用opencv和onnxruntime,需要提前下载编译好的包放在本地
opencv下载地址:Releases · opencv/opencv (github.com)
我下载的是opecv4.5.2
onnxruntime下载地址:Releases · microsoft/onnxruntime (github.com)
我下载的是onnxruntime-win-x64-1.15.1
下载完后把这两个压缩包解压到自己的某个路径下
opencv最好配置一下系统环境变量
5、在项目路径下建立bin、include、lib三个文件夹,把需要的opencv和onnxruntime依赖放进去
bin目录,我这里编译Release版本,所以用Release版本的动态库
include目录
lib目录
6、配置项目属性
7、完成上述配置后,修改代码
main.cpp
#include
#include
#include "inference.h"
#include
#include
#include 拼多多帮助力入口
void Detector(YOLO_V8*& p) {
std::filesystem::path current_path = std::filesystem::current_path();
std::filesystem::path imgs_path = current_path / "images";
for (auto& i : std::filesystem::directory_iterator(imgs_path))
{
if (i.path().extension() == ".jpg" || i.path().extension() == ".png" || i.path().extension() == ".jpeg")
{
std::string img_path = i.path().string();
cv::Mat img = cv::imread(img_path);
std::vectorres;
p->RunSession(img, res);
for (auto& re : res)
{
cv::RNG rng(cv::getTickCount());
cv::Scalar color(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256
YOLOV8 Onnxruntime OpenCV C++ VS2019部署推理
参考文章