C# OpenVinoSharp部署Yolo26模型进行推理

使用开源没多久的Yolo26模型进行推理测试,无需NMS操作

效果如下:

模型信息:

输出格式说明→ [1,300,6] 6: x1 y1 x2 y2 score classeId(左上角 右下角 置信度 类别ID)

代码如下:

using OpenCvSharp;

using OpenCvSharp.Extensions;

using OpenVinoSharp;

using OpenVinoSharp.Extensions.process;

using System;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.Linq;

using System.Windows.Forms;

namespace OPENVINOSHARP_NEW

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

Core core;

CompiledModel compiledModel;

InferRequest inferRequest;

//分配随机颜色

private static readonly Scalar[] Colors = Enumerable.Repeat(false, 80).Select(x => Scalar.RandomColor()).ToArray();

//coco names

private static readonly string[] Labels = File.ReadAllLines("D:\\ultralytics-main\\classes.txt");

private void button1_Click(object sender, EventArgs e)

{

core = new Core();

compiledModel = core.compile_model("C:\\yolo26s.onnx", "CPU");

inferRequest = compiledModel.create_infer_request();

}

private void button2_Click(object sender, EventArgs e)

{

Mat image = new Mat("D:\\ultralytics-main\\bus.jpg");

Mat rgbMat = image.CvtColor(ColorConversionCodes.BGR2RGB);

rgbMat = OpenVinoSharp.Extensions.process.Resize.letterbox_img(rgbMat, 640, out var scales);

Mat normalizedMat = new Mat();

rgbMat.ConvertTo(normalizedMat, MatType.CV_32FC3, 1.0 / 255.0f); ;

float[] data = Permute.run(normalizedMat);

Tensor inputTensor = inferRequest.get_input_tensor();

inputTensor.set_data(data);

inferRequest.infer();

var outputTensor = inferRequest.get_output_tensor();

var output = outputTensor.get_data<float>((int)outputTensor.get_size()); //[1,300,6] 6: x y w h score classe

var proposals = (int)outputTensor.get_shape()[1];

var nums = (int)outputTensor.get_shape()[2];

List<DetResult> results = new List<DetResult>();

for (int i = 0; i < proposals; i++)

{

float score = output[i * nums + 4];

if (score > 0.5)

{

int x1 = (int)(output[i * nums + 0] * scales);

int y1 = (int)(output[i * nums + 1] * scales);

int x2 = (int)(output[i * nums + 2] * scales);

int y2 = (int)(output[i * nums + 3] * scales);

Rect rect = new Rect(x1, y1, x2-x1, y2-y1);

int classId = (int)output[i * nums + 5];

DetResult detResult = new DetResult

{

box = rect,

score = score,

label = classId

};

results.Add(detResult);

}

}

// 绘制边界框

foreach (DetResult detResult in results)

{

Cv2.Rectangle(image, detResult.box, Colors[detResult.label], 2);

// 添加标签和置信度

string label = $"Class: {Labels[detResult.label]} - {detResult.score:F2}";

Cv2.PutText(image, label,new Point(detResult.box.X, detResult.box.Y - 10),HersheyFonts.HersheySimplex, 0.5, new Scalar(255, 255, 0), 1);

}

pictureBox1.Image = image.ToBitmap();

rgbMat.Dispose();

normalizedMat.Dispose();

image.Dispose();

}

}

public class DetResult

{

public Rect box;

public float score;

public int label;

}

}

相关推荐
badhope3 小时前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园4 小时前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈5 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl5 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628885 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
IT猿手5 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
AI科技星5 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
sunwenjian8866 小时前
Java进阶——IO 流
java·开发语言·python
波特率1152006 小时前
const关键字与函数的重载
开发语言·c++·函数重载
FL16238631296 小时前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#