当前位置: 首页 > news >正文

[深度学习]基于C++和onnxruntime部署yolov12的onnx模型

基于C++和ONNX Runtime部署YOLOv12的ONNX模型,可以遵循以下步骤:

  1. 准备环境:首先,确保已经下载后指定版本opencv和onnruntime的C++库。

  2. 模型转换: 安装好yolov12环境并将YOLOv12模型转换为ONNX格式。这通常涉及使用深度学习框架(如PyTorch或TensorFlow)加载原始模型,并导出为ONNX格式。转换指令

# End-to-End ONNX
yolo export model=yolov12{n/s/m/b/l/x}.pt format=onnx opset=13 simplify
  1. C++环境配置:在CMakeLists.txt项目中正确引用了opencv和ONNX Runtime的头文件,并链接到相应的库。这允许在C++代码中使用ONNX Runtime的功能。

  2. 加载模型:使用ONNX Runtime的API加载转换后的YOLOv12 ONNX模型。

  3. 执行推理:通过ONNX Runtime的推理引擎,将图像数据输入到模型中,并执行目标检测任务。

  4. 处理结果:解析模型输出的结果,这通常涉及将输出的张量数据转换为可理解的检测结果,如边界框坐标和类别标签。

通过这些步骤,可以在C++环境中利用ONNX Runtime高效地部署YOLOv12模型,实现实时的目标检测功能。

【测试环境】

windows10 x64
vs2019
cmake==3.30.1
onnxruntime==1.16.3
opencv==4.9.0
【使用步骤】
首先cmake生成exe文件,然后将onnxruntime.dll和onnxruntime_providers_shared.dll放到exe一起,不然会提示报错0xc000007b,这是因为系统目录也有个onnxruntime.dll引发冲突,并把car.mp4也放到exe一起。运行直接输入
yolov12.exe 注意onnx路径要是你真实路径我的onnx路径是我桌面上地址

【代码调用】

注意onnxruntime使用的cpu版本库,如需使用GPU还需要修改代码才行

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <string>
#include "YOLO12.hpp"

int main() {
    // Paths to the model, labels, test image, and save directory
    const std::string labelsPath = "../models/coco.names";
    const std::string imagePath = "../data/dog.jpg";           // Image path
    const std::string savePath = "../data/dog_detections.jpg";   // Save directory

    // Model path for YOLOv12
    const std::string modelPath = "../models/yolov12n.onnx";   // YOLOv12

    // Initialize the YOLO detector with the chosen model and labels
    bool isGPU = true; // Set to false for CPU processing
    YOLO12Detector detector(modelPath, labelsPath, isGPU);

    // Load an image
    cv::Mat image = cv::imread(imagePath);
    if (image.empty()) {
        std::cerr << "Error: Could not open or find the image!\n";
        return -1;
    }

    // Detect objects in the image and measure execution time
    auto start = std::chrono::high_resolution_clock::now();
    std::vector<Detection> results = detector.detect(image);
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::high_resolution_clock::now() - start);

    std::cout << "Detection completed in: " << duration.count() << " ms" << std::endl;

    // Draw bounding boxes on the image
    detector.drawBoundingBox(image, results); // Simple bounding box drawing
    // detector.drawBoundingBoxMask(image, results); // Uncomment for mask drawing

    // Save the processed image to the specified directory
    if (cv::imwrite(savePath, image)) {
        std::cout << "Processed image saved successfully at: " << savePath << std::endl;
    } else {
        std::cerr << "Error: Could not save the processed image to: " << savePath << std::endl;
    }

    // Display the image
    cv::imshow("Detections", image);
    cv::waitKey(0); // Wait for a key press to close the window

    return 0;
}

部署演示可以参考视频:bilibili.com/video/BV1iYPsewEDg

http://www.dtcms.com/a/38486.html

相关文章:

  • Git add --- error: Filename too long
  • 代码随想录DAY35|01背包问题、416. 分割等和子集
  • DeepSeek开源周Day1:重磅发布FlashMLA,重新定义AI推理效率天花板
  • AI顿悟之旅 - 1 - DeepSeek的训练方法为什么相比GPT-o1大幅度减少算力资源?
  • Windows与Ubuntu安装MySQL
  • Sqlserver安全篇之_隐藏实例功能和禁用SQL Server Browser服务
  • 银河麒麟高级服务器操作系统通用rsync禁止匿名访问操作指南
  • 【STL专题】优先级队列priority_queue的使用和模拟实现,巧妙利用仿函数解决优先级
  • wifi5和wifi6,WiFi 2.4G、5G,五类网线和六类网线,4G和5G的区别
  • 本地部署轻量级web开发框架Flask并实现无公网ip远程访问开发界面
  • windows中kafka集群部署示例
  • PHP 连接 Memcached 服务
  • Redis持久化机制与数据恢复
  • PHP入门基础学习三(PHP基本语法)
  • 【音视频】音视频录制、播放原理
  • 网络安全学习-WEB安全常见漏洞
  • 1.介绍一下TCP/IP模型和OSI模型的区别【中高频】
  • ubuntu22.04 如何扩根目录空间,当空间不够时
  • C++---了解STL
  • Github 2025-02-25 Python开源项目日报 Top10
  • Metal 学习笔记三:渲染管线
  • Chromedriver与Chrome版本映射表
  • 机器视觉--相机曝光
  • HITCON2017SSRFME-学习复盘
  • 【大语言模型】【整合版】DeepSeek 模型提示词学习笔记(散装的可以看我之前的学习笔记,这里只是归纳与总结了一下思路,内容和之前发的差不多)
  • Day8 蓝桥杯acw讲解
  • Rust 驱动的 Python 工具革命:Ruff 和 uv 与传统工具的对比分
  • nodejs:express + js-mdict 作为后端,vue 3 + vite 作为前端,在线查询英汉词典
  • 【由技及道】SpringBoot启动即退出的量子纠缠诊断实录【人工智障AI2077的开发问题日志】
  • JavaScript函数-函数案例