[C++][cmake]基于C++在windows上部署yolo26的目标检测onnx模型

yolo26已经正式发布了,第一时间手搓C++代码实现YOLO26部署,首先看yolov11网络结构,发现输出shape是1x84x8400

再看看YOLO26网络结构:

可见yolo11和yolo26输出是不一样的是不能共用代码。

模型使用官方yolo26n.pt转换成的onnx,转换命令

yolo export model=yolo26n.pt format=onnx opset=12

如果你是自己训练的模型可以替换即可,但是需要yolo26框架才行

测试环境:

windows10 x64

vs2019

cmake==3.30.1

onnxruntime-gpu==1.20.1

opencv==4.12.0

使用步骤:

首先cmake生成exe文件,然后将onnxruntime.dll和onnxruntime_providers_shared.dll放到exe一起,不然会提示报错0x0000007b,这是因为系统目录也有个onnxruntime.dll引发冲突,并把car.mp4也放到exe一起。运行直接输入 yolo26.exe C:\Users\Administrator\Desktop\yolo26-onnx-cplus\models\yolo26n.onnx 注意onnx路径要是你真实路径我的onnx路径是我桌面上地址

调用代码:

复制代码
#include "YOlo26Manager.h"
#include <iostream>
#include <opencv2/opencv.hpp>


void demo_image()
{
    std::string model_path = "yolo26.onnx";
    Yolo26Manager detector(model_path);
    cv::Mat image = cv::imread("car.jpg");
    std::vector<Detection> detections = detector.Inference(image);
    cv::Mat resultImg = detector.DrawImage(image, detections);
    cv::imshow("yolo26", resultImg);
    cv::waitKey(0);
}

// int main(int argc, char const *argv[])
// {
//     demo_image();
//     return 0;
// }



int main(int argc, char const *argv[])
{
    std::string model_path = argv[1];
    cv::namedWindow("yolo26", cv::WINDOW_AUTOSIZE);
    Yolo26Manager detector(model_path,false);
    cv::VideoCapture cap("car.mp4");//这个地方也可以修改成视频路径或者摄像头索引
    if (!cap.isOpened())
    {
        std::cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    cv::Mat frame;
    std::cout << "Start detect" << std::endl << "Press any key to terminate" << std::endl;

    for (;;)
    {
        cap.read(frame);
        if (frame.empty())
        {
            std::cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        auto timer = cv::getTickCount();
        std::vector<Detection> detections = detector.Inference(frame);
        double fps = cv::getTickFrequency() / ((double)cv::getTickCount() - timer);
        cv::putText(frame, "FPS: " + std::to_string(fps), cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0), 2, 8);
        cv::Mat resultImg = detector.DrawImage(frame, detections);
        cv::imshow("yolo26", resultImg);
        if (cv::waitKey(5) >= 0)
            break;
    }

    return 0;
}

最后测试效果:

源码地址:https://download.csdn.net/download/FL1623863129/92565316

相关推荐
同勉共进3 小时前
记一例 vibe coding + gcc bug 导致的线程池死锁问题
c++·线程池·gcc·死锁·vibe coding
我叫洋洋3 小时前
C ++ [ hello world ]
c语言·c++·算法
CCPC不拿奖不改名4 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
王维同学6 小时前
[自学][Windows C++]RunOnceEx 注册表分组键的安全遍历
c++·windows·安全·开源
Scott9999HH7 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
脱胎换骨-军哥8 小时前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust
AI棒棒牛9 小时前
第11讲 | 目标检测任务:边界框、IoU、类别与置信度
人工智能·yolo·目标检测·yolo26
旖-旎9 小时前
LeetCode 279:完全平方数(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
胖大和尚11 小时前
C++ 多线程编程的实现方式
c++·thread
imatt11 小时前
Qt Creator 在Windows下编译信息乱码
windows·qt