C# OnnxRuntime yolo12

目录

效果

模型

项目

代码

下载

效果

模型
Model Properties

date:2025-04-08T16:19:17.772588
description:Ultralytics YOLOv12m model
author:Ultralytics
version:8.3.102
task:detect
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[640, 640]
names:{0: ‘person’, 1: ‘bicycle’, 2: ‘car’, 3: ‘motorcycle’, 4: ‘airplane’, 5: ‘bus’, 6: ‘train’, 7: ‘truck’, 8: ‘boat’, 9: ‘traffic light’, 10: ‘fire hydrant’, 11: ‘stop sign’, 12: ‘parking meter’, 13: ‘bench’, 14: ‘bird’, 15: ‘cat’, 16: ‘dog’, 17: ‘horse’, 18: ‘sheep’, 19: ‘cow’, 20: ‘elephant’, 21: ‘bear’, 22: ‘zebra’, 23: ‘giraffe’, 24: ‘backpack’, 25: ‘umbrella’, 26: ‘handbag’, 27: ‘tie’, 28: ‘suitcase’, 29: ‘frisbee’, 30: ‘skis’, 31: ‘snowboard’, 32: ‘sports ball’, 33: ‘kite’, 34: ‘baseball bat’, 35: ‘baseball glove’, 36: ‘skateboard’, 37: ‘surfboard’, 38: ‘tennis racket’, 39: ‘bottle’, 40: ‘wine glass’, 41: ‘cup’, 42: ‘fork’, 43: ‘knife’, 44: ‘spoon’, 45: ‘bowl’, 46: ‘banana’, 47: ‘apple’, 48: ‘sandwich’, 49: ‘orange’, 50: ‘broccoli’, 51: ‘carrot’, 52: ‘hot dog’, 53: ‘pizza’, 54: ‘donut’, 55: ‘cake’, 56: ‘chair’, 57: ‘couch’, 58: ‘potted plant’, 59: ‘bed’, 60: ‘dining table’, 61: ‘toilet’, 62: ‘tv’, 63: ‘laptop’, 64: ‘mouse’, 65: ‘remote’, 66: ‘keyboard’, 67: ‘cell phone’, 68: ‘microwave’, 69: ‘oven’, 70: ‘toaster’, 71: ‘sink’, 72: ‘refrigerator’, 73: ‘book’, 74: ‘clock’, 75: ‘vase’, 76: ‘scissors’, 77: ‘teddy bear’, 78: ‘hair drier’, 79: ‘toothbrush’}
args:{‘batch’: 1, ‘half’: False, ‘dynamic’: False, ‘simplify’: True, ‘opset’: 12, ‘nms’: False}

Inputs

name:images
tensor:Float[1, 3, 640, 640]

Outputs

name:output0
tensor:Float[1, 84, 8400]

项目

代码
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Onnx_Yolov8_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    string image_path = ""; 
    string model_path;
    string classer_path;
    public string[] class_names;
    public int class_num;

    DateTime dt1 = DateTime.Now;
    DateTime dt2 = DateTime.Now;

    int input_height;
    int input_width;
    float ratio_height;
    float ratio_width;

    InferenceSession onnx_session;

    int box_num;
    float conf_threshold;
    float nms_threshold;

    /// <summary>
    /// 选择图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = fileFilter;
        if (ofd.ShowDialog() != DialogResult.OK) return;

        pictureBox1.Image = null;

        image_path = ofd.FileName;
        pictureBox1.Image = new Bitmap(image_path);

        textBox1.Text = "";
        pictureBox2.Image = null;
    }

    /// <summary>
    /// 推理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        if (image_path == "")
        {
            return;
        }

        button2.Enabled = false;
        pictureBox2.Image = null;
        textBox1.Text = "";
        Application.DoEvents();

        Mat image = new Mat(image_path);
        
        //图片缩放
        int height = image.Rows;
        int width = image.Cols;
        Mat temp_image = image.Clone();
        if (height > input_height || width > input_width)
        {
            float scale = Math.Min((float)input_height / height, (float)input_width / width);
            OpenCvSharp.Size new_size = new OpenCvSharp.Size((int)(width * scale), (int)(height * scale));
            Cv2.Resize(image, temp_image, new_size);
        }
        ratio_height = (float)height / temp_image.Rows;
        ratio_width = (float)width / temp_image.Cols;
        Mat input_img = new Mat();
        Cv2.CopyMakeBorder(temp_image, input_img, 0, input_height - temp_image.Rows, 0, input_width - temp_image.Cols, BorderTypes.Constant, 0);

        //Cv2.ImShow("input_img", input_img);

        //输入Tensor
        Tensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
        for (int y = 0; y < input_img.Height; y++)
        {
            for (int x = 0; x < input_img.Width; x++)
            {
                input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f;
                input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f;
                input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f;
            }
        }

        List<NamedOnnxValue> input_container = new List<NamedOnnxValue>
        {
            NamedOnnxValue.CreateFromTensor("images", input_tensor)
        };

        //推理
        dt1 = DateTime.Now;
        var ort_outputs = onnx_session.Run(input_container).ToArray();
        dt2 = DateTime.Now;

        float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num);

        float[] confidenceInfo = new float[class_num];
        float[] rectData = new float[4];

        List<DetectionResult> detResults = new List<DetectionResult>();

        for (int i = 0; i < box_num; i++)
        {
            Array.Copy(data, i * (class_num + 4), rectData, 0, 4);
            Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num);

            float score = confidenceInfo.Max(); // 获取最大值

            int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置

            int _centerX = (int)(rectData[0] * ratio_width);
            int _centerY = (int)(rectData[1] * ratio_height);
            int _width = (int)(rectData[2] * ratio_width);
            int _height = (int)(rectData[3] * ratio_height);

            detResults.Add(new DetectionResult(
               maxIndex,
               class_names[maxIndex],
               new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height),
               score));
        }

        //NMS
        CvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices);
        detResults = detResults.Where((x, index) => indices.Contains(index)).ToList();

        //绘制结果
        Mat result_image = image.Clone();
        foreach (DetectionResult r in detResults)
        {
            Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
            Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2);
        }

        pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

        button2.Enabled = true;
    }

    /// <summary>
    ///窗体加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Load(object sender, EventArgs e)
    {
        model_path = "model/yolo12m.onnx";

        //创建输出会话,用于输出模型读取信息
        SessionOptions options = new SessionOptions();
        options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
        options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

        // 创建推理模型类,读取模型文件
        onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

        input_height = 640;
        input_width = 640;

        box_num = 8400;
        conf_threshold = 0.25f;
        nms_threshold = 0.5f;

        classer_path = "model/lable.txt";
        class_names = File.ReadAllLines(classer_path, Encoding.UTF8);
        class_num = class_names.Length;

        image_path = "test_img/1.jpg";
        pictureBox1.Image = new Bitmap(image_path);


    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
        if (pictureBox2.Image == null)
        {
            return;
        }
        Bitmap output = new Bitmap(pictureBox2.Image);
        SaveFileDialog sdf = new SaveFileDialog();
        sdf.Title = "保存";
        sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
        if (sdf.ShowDialog() == DialogResult.OK)
        {
            switch (sdf.FilterIndex)
            {
                case 1:
                    {
                        output.Save(sdf.FileName, ImageFormat.Jpeg);
                        break;
                    }
                case 2:
                    {
                        output.Save(sdf.FileName, ImageFormat.Png);
                        break;
                    }
                case 3:
                    {
                        output.Save(sdf.FileName, ImageFormat.Bmp);
                        break;
                    }
                case 4:
                    {
                        output.Save(sdf.FileName, ImageFormat.Emf);
                        break;
                    }
                case 5:
                    {
                        output.Save(sdf.FileName, ImageFormat.Exif);
                        break;
                    }
                case 6:
                    {
                        output.Save(sdf.FileName, ImageFormat.Gif);
                        break;
                    }
                case 7:
                    {
                        output.Save(sdf.FileName, ImageFormat.Icon);
                        break;
                    }

                case 8:
                    {
                        output.Save(sdf.FileName, ImageFormat.Tiff);
                        break;
                    }
                case 9:
                    {
                        output.Save(sdf.FileName, ImageFormat.Wmf);
                        break;
                    }
            }
            MessageBox.Show("保存成功,位置:" + sdf.FileName);
        }
    }

    private void pictureBox1_DoubleClick(object sender, EventArgs e)
    {
        ShowNormalImg(pictureBox1.Image);
    }

    private void pictureBox2_DoubleClick(object sender, EventArgs e)
    {
        ShowNormalImg(pictureBox2.Image);
    }

    public  void ShowNormalImg(Image img)
    {
        if (img == null) return;

        frmShow frm = new frmShow();

        frm.Width = Screen.PrimaryScreen.Bounds.Width;
        frm.Height = Screen.PrimaryScreen.Bounds.Height;

        if (frm.Width > img.Width)
        {
            frm.Width = img.Width;
        }

        if (frm.Height > img.Height)
        {
            frm.Height = img.Height;
        }

        bool b = frm.richTextBox1.ReadOnly;
        Clipboard.SetDataObject(img, true);
        frm.richTextBox1.ReadOnly = false;
        frm.richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
        frm.richTextBox1.ReadOnly = b;

        frm.ShowDialog();

    }

    public unsafe float[] Transpose(float[] tensorData, int rows, int cols)
    {
        float[] transposedTensorData = new float[tensorData.Length];

        fixed (float* pTensorData = tensorData)
        {
            fixed (float* pTransposedData = transposedTensorData)
            {
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < cols; j++)
                    {
                        int index = i * cols + j;
                        int transposedIndex = j * rows + i;
                        pTransposedData[transposedIndex] = pTensorData[index];
                    }
                }
            }
        }
        return transposedTensorData;
    }
}

public class DetectionResult
{
    public DetectionResult(int ClassId, string Class, Rect Rect, float Confidence)
    {
        this.ClassId = ClassId;
        this.Confidence = Confidence;
        this.Rect = Rect;
        this.Class = Class;
    }

    public string Class { get; set; }

    public int ClassId { get; set; }

    public float Confidence { get; set; }

    public Rect Rect { get; set; }

}

}

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Onnx_Yolov8_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    string image_path = ""; 
    string model_path;
    string classer_path;
    public string[] class_names;
    public int class_num;

    DateTime dt1 = DateTime.Now;
    DateTime dt2 = DateTime.Now;

    int input_height;
    int input_width;
    float ratio_height;
    float ratio_width;

    InferenceSession onnx_session;

    int box_num;
    float conf_threshold;
    float nms_threshold;

    /// <summary>
    /// 选择图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = fileFilter;
        if (ofd.ShowDialog() != DialogResult.OK) return;

        pictureBox1.Image = null;

        image_path = ofd.FileName;
        pictureBox1.Image = new Bitmap(image_path);

        textBox1.Text = "";
        pictureBox2.Image = null;
    }

    /// <summary>
    /// 推理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        if (image_path == "")
        {
            return;
        }

        button2.Enabled = false;
        pictureBox2.Image = null;
        textBox1.Text = "";
        Application.DoEvents();

        Mat image = new Mat(image_path);
        
        //图片缩放
        int height = image.Rows;
        int width = image.Cols;
        Mat temp_image = image.Clone();
        if (height > input_height || width > input_width)
        {
            float scale = Math.Min((float)input_height / height, (float)input_width / width);
            OpenCvSharp.Size new_size = new OpenCvSharp.Size((int)(width * scale), (int)(height * scale));
            Cv2.Resize(image, temp_image, new_size);
        }
        ratio_height = (float)height / temp_image.Rows;
        ratio_width = (float)width / temp_image.Cols;
        Mat input_img = new Mat();
        Cv2.CopyMakeBorder(temp_image, input_img, 0, input_height - temp_image.Rows, 0, input_width - temp_image.Cols, BorderTypes.Constant, 0);

        //Cv2.ImShow("input_img", input_img);

        //输入Tensor
        Tensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
        for (int y = 0; y < input_img.Height; y++)
        {
            for (int x = 0; x < input_img.Width; x++)
            {
                input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f;
                input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f;
                input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f;
            }
        }

        List<NamedOnnxValue> input_container = new List<NamedOnnxValue>
        {
            NamedOnnxValue.CreateFromTensor("images", input_tensor)
        };

        //推理
        dt1 = DateTime.Now;
        var ort_outputs = onnx_session.Run(input_container).ToArray();
        dt2 = DateTime.Now;

        float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num);

        float[] confidenceInfo = new float[class_num];
        float[] rectData = new float[4];

        List<DetectionResult> detResults = new List<DetectionResult>();

        for (int i = 0; i < box_num; i++)
        {
            Array.Copy(data, i * (class_num + 4), rectData, 0, 4);
            Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num);

            float score = confidenceInfo.Max(); // 获取最大值

            int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置

            int _centerX = (int)(rectData[0] * ratio_width);
            int _centerY = (int)(rectData[1] * ratio_height);
            int _width = (int)(rectData[2] * ratio_width);
            int _height = (int)(rectData[3] * ratio_height);

            detResults.Add(new DetectionResult(
               maxIndex,
               class_names[maxIndex],
               new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height),
               score));
        }

        //NMS
        CvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices);
        detResults = detResults.Where((x, index) => indices.Contains(index)).ToList();

        //绘制结果
        Mat result_image = image.Clone();
        foreach (DetectionResult r in detResults)
        {
            Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
            Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2);
        }

        pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

        button2.Enabled = true;
    }

    /// <summary>
    ///窗体加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Load(object sender, EventArgs e)
    {
        model_path = "model/yolo12m.onnx";

        //创建输出会话,用于输出模型读取信息
        SessionOptions options = new SessionOptions();
        options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
        options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

        // 创建推理模型类,读取模型文件
        onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

        input_height = 640;
        input_width = 640;

        box_num = 8400;
        conf_threshold = 0.25f;
        nms_threshold = 0.5f;

        classer_path = "model/lable.txt";
        class_names = File.ReadAllLines(classer_path, Encoding.UTF8);
        class_num = class_names.Length;

        image_path = "test_img/1.jpg";
        pictureBox1.Image = new Bitmap(image_path);


    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
        if (pictureBox2.Image == null)
        {
            return;
        }
        Bitmap output = new Bitmap(pictureBox2.Image);
        SaveFileDialog sdf = new SaveFileDialog();
        sdf.Title = "保存";
        sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
        if (sdf.ShowDialog() == DialogResult.OK)
        {
            switch (sdf.FilterIndex)
            {
                case 1:
                    {
                        output.Save(sdf.FileName, ImageFormat.Jpeg);
                        break;
                    }
                case 2:
                    {
                        output.Save(sdf.FileName, ImageFormat.Png);
                        break;
                    }
                case 3:
                    {
                        output.Save(sdf.FileName, ImageFormat.Bmp);
                        break;
                    }
                case 4:
                    {
                        output.Save(sdf.FileName, ImageFormat.Emf);
                        break;
                    }
                case 5:
                    {
                        output.Save(sdf.FileName, ImageFormat.Exif);
                        break;
                    }
                case 6:
                    {
                        output.Save(sdf.FileName, ImageFormat.Gif);
                        break;
                    }
                case 7:
                    {
                        output.Save(sdf.FileName, ImageFormat.Icon);
                        break;
                    }

                case 8:
                    {
                        output.Save(sdf.FileName, ImageFormat.Tiff);
                        break;
                    }
                case 9:
                    {
                        output.Save(sdf.FileName, ImageFormat.Wmf);
                        break;
                    }
            }
            MessageBox.Show("保存成功,位置:" + sdf.FileName);
        }
    }

    private void pictureBox1_DoubleClick(object sender, EventArgs e)
    {
        ShowNormalImg(pictureBox1.Image);
    }

    private void pictureBox2_DoubleClick(object sender, EventArgs e)
    {
        ShowNormalImg(pictureBox2.Image);
    }

    public  void ShowNormalImg(Image img)
    {
        if (img == null) return;

        frmShow frm = new frmShow();

        frm.Width = Screen.PrimaryScreen.Bounds.Width;
        frm.Height = Screen.PrimaryScreen.Bounds.Height;

        if (frm.Width > img.Width)
        {
            frm.Width = img.Width;
        }

        if (frm.Height > img.Height)
        {
            frm.Height = img.Height;
        }

        bool b = frm.richTextBox1.ReadOnly;
        Clipboard.SetDataObject(img, true);
        frm.richTextBox1.ReadOnly = false;
        frm.richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Bitmap));
        frm.richTextBox1.ReadOnly = b;

        frm.ShowDialog();

    }

    public unsafe float[] Transpose(float[] tensorData, int rows, int cols)
    {
        float[] transposedTensorData = new float[tensorData.Length];

        fixed (float* pTensorData = tensorData)
        {
            fixed (float* pTransposedData = transposedTensorData)
            {
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < cols; j++)
                    {
                        int index = i * cols + j;
                        int transposedIndex = j * rows + i;
                        pTransposedData[transposedIndex] = pTensorData[index];
                    }
                }
            }
        }
        return transposedTensorData;
    }
}

public class DetectionResult
{
    public DetectionResult(int ClassId, string Class, Rect Rect, float Confidence)
    {
        this.ClassId = ClassId;
        this.Confidence = Confidence;
        this.Rect = Rect;
        this.Class = Class;
    }

    public string Class { get; set; }

    public int ClassId { get; set; }

    public float Confidence { get; set; }

    public Rect Rect { get; set; }

}

————————————————

                        版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.youkuaiyun.com/lw112190/article/details/147072013

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值