yolov8 tensorRT 的 C++ 部署

Yolov8 tensorRT 的 C++ 部署

前几篇写了 yolov8 相关的 瑞芯微rknn、地平线Horizon 的仿真和C++相关的部署,以及tensorRT的python版本部署,本篇说说tensorRT 的 C++ 部署,所使用的模型是基于之前导出的onnx模型。

【完整代码、模型、测试图片】

TensorRT版本:TensorRT-7.1.3.4

1 导出 onnx 模型

导出适配本实例的onnx模型参考【yolov8 导出onnx-2023年11月15日版本】

2 编译

编译前修改 CMakeLists.txt 对应的TensorRT版本在这里插入图片描述

cd yolov8_tensorRT_Cplusplus
mkdir build
cd build
cmake ..
make

3 运行

运行时如果.trt模型存在则直接加载,若不存会自动先将onnx转换成 trt 模型,并保存在给定的路径,然后运行推理

# 运行时如果.trt模型存在则直接加载,若不存会自动先将onnx转换成 trt 模型,并保存在给定的路径,然后运行推理。
cd build
./yolo_trt

4 测试效果

onnx 测试效果
在这里插入图片描述
tensorRT 测试效果
在这里插入图片描述

tensorRT 时耗在这里插入图片描述

5 替换模型说明

1)按照本实例给的导出onnx方式导出对应的onnx;导出的onnx模型建议simplify后再转trt模型。

2)注意修改后处理相关 postprocess.hpp 中相关的参数(类别、输入分辨率等)。

修改相关的路径

    std::string OnnxFile = "/zhangqian/workspaces1/TensorRT/yolov8_trt_Cplusplus/models/yolov8n_ZQ.onnx";
    std::string SaveTrtFilePath = "/zhangqian/workspaces1/TensorRT/yolov8_trt_Cplusplus/models/yolov8n_ZQ.trt";
    cv::Mat SrcImage = cv::imread("/zhangqian/workspaces1/TensorRT/yolov8_trt_Cplusplus/images/test.jpg");

    int img_width = SrcImage.cols;
    int img_height = SrcImage.rows;

    CNN YOLO(OnnxFile, SaveTrtFilePath, 1, 3, 640, 640, 7);  // 1, 3, 640, 640, 7 前四个为模型输入的NCWH, 7为模型输出叶子节点的个数+1,(本示例中的onnx模型输出有6个叶子节点,再+1=7)
    YOLO.ModelInit();
    YOLO.Inference(SrcImage);

    for (int i = 0; i < YOLO.DetectiontRects_.size(); i += 6)
    {
        int classId = int(YOLO.DetectiontRects_[i + 0]);
        float conf = YOLO.DetectiontRects_[i + 1];
        int xmin = int(YOLO.DetectiontRects_[i + 2] * float(img_width) + 0.5);
        int ymin = int(YOLO.DetectiontRects_[i + 3] * float(img_height) + 0.5);
        int xmax = int(YOLO.DetectiontRects_[i + 4] * float(img_width) + 0.5);
        int ymax = int(YOLO.DetectiontRects_[i + 5] * float(img_height) + 0.5);

        char text1[256];
        sprintf(text1, "%d:%.2f", classId, conf);
        rectangle(SrcImage, cv::Point(xmin, ymin), cv::Point(xmax, ymax), cv::Scalar(255, 0, 0), 2);
        putText(SrcImage, text1, cv::Point(xmin, ymin + 15), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2);
    }

    imwrite("/zhangqian/workspaces1/TensorRT/yolov8_trt_Cplusplus/images/result.jpg", SrcImage);

    printf("== obj: %d \n", int(float(YOLO.DetectiontRects_.size()) / 6.0));

【特别说明】:本示例只是用来测试流程,模型效果并不保证,且代码整理的布局合理性没有过多的考虑。

6 相关链接

yolov8 瑞芯微 RKNN 的 C++部署

yolov8 瑞芯微RKNN和地平线Horizon芯片仿真测试部署-2023年11月15日版本

yolov8 瑞芯微RKNN和地平线Horizon芯片仿真测试部署

  • 20
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值