学习C#调用Microsoft.ML.OnnxRuntime+OpenCvSharp+YOLO26进行目标检测的基本用法

  使用ultralytics库将YOLO26模型文件yolo26m.pt转换为onnx格式的模型文件,然后调用Microsoft.ML.OnnxRuntime获取模型信息,如下图所示:

  yolo26m.onnx模型的输入形状与yolo5相同,但输出形状变成了1,300,6,第一维还是代表一次处理的图片数量,第二维为预测框的总数量,也即每张图片的预测结果数量,第三维为每个预测框的特征向量长度,由于YOLO26采用端到端无NMS推理,直接输出最终检测结果,因此第三维的长度急剧减少,主要包括x1, y1, x2, y2, confidence, class_id等6个值,前4个值为预测框的左上角和右下角坐标,confidence为置信度分数,class_id为COCO类别的索引值。
  基于之前调用Microsoft.ML.OnnxRuntime+OpenCvSharp+YOLO5的源码,调整模型预测结果的后处理函数及坐标变换函数逻辑如下:

csharp 复制代码
public (int x, int y, int w, int h) ScaleCoordinatesOfYolo26(
    float xl, float yl, float xr, float yr,
    int originalWidth, int originalHeight,
    int inputSize = 640)
{
    // 计算缩放比例
    float scale = Math.Min((float)inputSize / originalWidth, (float)inputSize / originalHeight);

    // 计算填充尺寸
    int padX = (int)((inputSize - originalWidth * scale) / 2);
    int padY = (int)((inputSize - originalHeight * scale) / 2);

    // 映射回原始图像坐标            
    int scaledX = (int)((xl - padX) / scale);
    int scaledY = (int)((yl - padY) / scale);
    int scaledW = (int)((xr-xl) / scale);
    int scaledH = (int)((yr-yl) / scale);

    return (scaledX, scaledY, scaledW, scaledH);
}

public List<DetectionResult> ProcessDetectionsOfYolo26(
   DenseTensor<float> output,
   int originalWidth,
   int originalHeight,
   float confidenceThreshold = 0.8f)
{
    var detections = new List<DetectionResult>();

    // 解析输出张量 (1, 300, 6) - 假设80个类别
    int numDetections = output.Dimensions[1];

    for (int i = 0; i < numDetections; i++)
    {
        // 提取对象置信度
        float objectConfidence = output[0,i,4];

        // 获取最高概率类别
        int classId = Convert.ToInt32(output[0, i, 5]);

        if (objectConfidence > confidenceThreshold)
        {
            // 提取边界框坐标
            float pXl = output[0, i, 0];
            float pYl = output[0, i, 1];
            float pXr = output[0, i, 2];
            float pYr = output[0, i, 3];

            // 映射回原始图像坐标
            var (x, y, w, h) = ScaleCoordinatesOfYolo26(
                pXl, pYl, pXr, pYr,
                originalWidth, originalHeight);

            // 创建边界框
            Rect boundingBox = new Rect(x, y, w, h);

            detections.Add(new DetectionResult
            {
                ClassId = classId,
                Label = CocoLabels[classId],
                Confidence = objectConfidence,
                BoundingBox = boundingBox,
                OriginalCoordinates = (x, y, w, h)
            });
        }
    }

    return detections;
}

  最后是程序运行效果,如下图所示:

参考文献:

1https://docs.ultralytics.com/zh/models/yolo26/

相关推荐
stsdddd5 天前
YOLO系列目标检测数据集大全【第二十九期】
yolo·目标检测·目标跟踪
AI棒棒牛5 天前
第 03 讲《监督学习:数据、标签、Loss与训练循环》
人工智能·学习·yolo·目标检测·yolo26
stsdddd5 天前
YOLO系列目标检测数据集大全【第三十期】
yolo·目标检测·目标跟踪
YOLO数据集集合5 天前
无人机航拍地质灾害智能识别 山体滑坡实例分割数据集落地实战 | 泥石流监测 道路险情封堵 深度学习模型训练方案10296期
人工智能·深度学习·yolo·目标检测·无人机
音沐mu.5 天前
【73】墙壁建筑缺陷数据集(有v5/v8模型)/YOLO墙壁建筑缺陷检测
yolo·目标检测·目标检测数据集·墙壁建筑缺陷数据集·墙壁建筑缺陷检测
YOLO数据集集合5 天前
无人机风电设备智能巡检 风机叶片缺陷目标检测数据集实战 | 表面腐蚀漏油识别 工业视觉质检 深度学习模型训练落地10337期
人工智能·深度学习·目标检测·计算机视觉·无人机
装不满的克莱因瓶5 天前
【工业领域】掌握非极大值抑制(NMS)目标检测后处理方法——从重复框消除到工程落地核心技术
人工智能·python·深度学习·目标检测·机器学习·计算机视觉·目标跟踪
装不满的克莱因瓶5 天前
【工业领域】了解目标检测评估指标——从mAP到IoU的完整评价体系解析
人工智能·pytorch·python·深度学习·目标检测·计算机视觉·目标跟踪
动物园猫5 天前
直升机停机坪目标检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·目标检测
再一次等风来6 天前
YOLO26 实测记录:从模型下载、预测验证到 ONNX Runtime 推理部署
yolo·计算机视觉·onnx·yolo26