前言

前段时间玩了把yolo,结果卡在用winform调用模型实识别的过程.从网络上找了很长一段时间代码,结果总不尽人意.今天隔离结束,回到工位,继续撸代码.

环境

vs2019,Net5,ML.OnnxRuntime,SixLabors.

准备

nuget下载如下包,

下载onnx权重文件

models/FasterRCNN-10.onnx at main · onnx/models · GitHub

代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
//using SixLabors.Fonts;
//using System.Drawing;namespace Microsoft.ML.OnnxRuntime.FasterRcnnSample
{class Program{public static void Main(string[] args){// Read pathsstring modelFilePath = @"D:\Test\2022\FasterRCNN-10.onnx";//string modelFilePath = @"D:\Test\2022\best.onnx";// string imageFilePath = @"D:\Test\2022\A.jpg";string imageFilePath = @"D:\Test\2022\B.png";string outImageFilePath = "outputs.jpg";//System.IO.Directory.CreateDirectory(outImageFilePath);// Read imageusing Image<Rgb24> image = SixLabors.ImageSharp.Image.Load<Rgb24>(imageFilePath);// Resize imagefloat ratio = 800f / Math.Min(image.Width, image.Height);image.Mutate(x => x.Resize((int)(ratio * image.Width), (int)(ratio * image.Height)));// Preprocess imagevar paddedHeight = (int)(Math.Ceiling(image.Height / 32f) * 32f);var paddedWidth = (int)(Math.Ceiling(image.Width / 32f) * 32f);Tensor<float> input = new DenseTensor<float>(new[] { 3, paddedHeight, paddedWidth });var mean = new[] { 102.9801f, 115.9465f, 122.7717f };for (int y = paddedHeight - image.Height; y < image.Height; y++){image.ProcessPixelRows(im =>{var pixelSpan = im.GetRowSpan(y);for (int x = paddedWidth - image.Width; x < image.Width; x++){input[0, y, x] = pixelSpan[x].B - mean[0];input[1, y, x] = pixelSpan[x].G - mean[1];input[2, y, x] = pixelSpan[x].R - mean[2];}});}// Setup inputs and outputsvar inputs = new List<NamedOnnxValue>{NamedOnnxValue.CreateFromTensor("image", input)};// Run inferenceusing var session = new InferenceSession(modelFilePath);using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs);// Postprocess to get predictionsvar resultsArray = results.ToArray();float[] boxes = resultsArray[0].AsEnumerable<float>().ToArray();long[] labels = resultsArray[1].AsEnumerable<long>().ToArray();float[] confidences = resultsArray[2].AsEnumerable<float>().ToArray();var predictions = new List<Prediction>();var minConfidence = 0.7f;for (int i = 0; i < boxes.Length - 4; i += 4){var index = i / 4;if (confidences[index] >= minConfidence){predictions.Add(new Prediction{Box = new Box(boxes[i], boxes[i + 1], boxes[i + 2], boxes[i + 3]),Label = LabelMap.Labels[labels[index]],Confidence = confidences[index]});}}// Put boxes, labels and confidence on image and save for viewingusing var outputImage = File.OpenWrite(outImageFilePath);Font font = SystemFonts.CreateFont("Arial",16);foreach (var p in predictions){image.Mutate(x =>{x.DrawLines(Color.Red, 2f, new PointF[] {new SixLabors.ImageSharp.PointF(p.Box.Xmin, p.Box.Ymin),new SixLabors.ImageSharp.PointF(p.Box.Xmax, p.Box.Ymin),new PointF(p.Box.Xmax, p.Box.Ymin),new PointF(p.Box.Xmax, p.Box.Ymax),new PointF(p.Box.Xmax, p.Box.Ymax),new PointF(p.Box.Xmin, p.Box.Ymax),new PointF(p.Box.Xmin, p.Box.Ymax),new PointF(p.Box.Xmin, p.Box.Ymin)});x.DrawText($"{p.Label}, {p.Confidence:0.00}", font, Color.White, new PointF(p.Box.Xmin, p.Box.Ymin));});}image.SaveAsJpeg(outputImage);}}public class Prediction{public Box Box { set; get; }public string Label { set; get; }public float Confidence { set; get; }}public class Box{public Box(float xMin,float yMin,float xMax,float yMax){Xmin = xMin;Ymin = yMin;Xmax = xMax;Ymax = yMax;}public float Xmin { set; get; }public float Xmax { set; get; }public float Ymin { set; get; }public float Ymax { set; get; }}public static class LabelMap{static LabelMap(){Labels = new string[]{"","person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"};}public static string[] Labels { set; get; }}
}

说明

1.NamedOnnxValue.CreateFromTensor("image", input),这里面的"image"可以从WinMLDashboard软件中看到.

2.image.GetPixelRowSpan() 方法已经被 image.ProcessPixelRows()方法取代.这个东西找了半天.

3.Prediction,Box,LabelMap,是自己补全的.没找到网上的源代码.

效果

随便从网上找的图片.如有侵权,请联系本人.

引用

Mr.Q老师的博客

(20条消息) c# 部署onnx定位模型_Mr.Q的博客-CSDN博客_onnx模型部署

C#调用Onnx模型相关推荐

  1. 关于opencv调用onnx模型的一个错误 Can’t infer a dim denoted by -1 in function ‘cv::dnn::computeShapeByReshapeMas

    关于opencv调用onnx模型的一个错误 Can't infer a dim denoted by -1 in function 'cv::dnn::computeShapeByReshapeMas ...

  2. java调用onnx模型_开源一年多的模型交换格式ONNX,已经一统框架江湖了?

    原标题:开源一年多的模型交换格式ONNX,已经一统框架江湖了? 机器之心原创 作者:思源 近日,微软亚洲研究院和华为举办了 ONNX 合作伙伴研讨会,这是 ONNX 开源社区成立以来首次在中国举办的活 ...

  3. onnx模型部署(一) ONNXRuntime

    通常我们在训练模型时可以使用很多不同的框架,比如有的同学喜欢用 Pytorch,有的同学喜欢使用 TensorFLow,也有的喜欢 MXNet,以及深度学习最开始流行的 Caffe等等,这样不同的训练 ...

  4. 在Jupyter Notebook中调用ML模型服务图像标题生成器

    说明:写本文的目的主要是验证如何在Jupyter Notebook中通过API调用机器学习模型服务. 1.环境说明 CentOS7(部署在VMware Workstation Pro中的虚拟机) 需要 ...

  5. pytorch模型(.pt)转onnx模型(.onnx)的方法详解(1)

    1. pytorch模型转换到onnx模型 2.运行onnx模型 3.比对onnx模型和pytorch模型的输出结果 我这里重点是第一点和第二点,第三部分  比较容易 首先你要安装 依赖库:onnx ...

  6. C++:onnxruntime调用FasterRCNN模型

    背景: 最近由于项目原因,需要用C++做一些目标检测的任务,就捣鼓一下YOLOv5,发现部署确实很方便,将YOLOv5模型转为onnx模型后,可以用OpenCV的dnn.readNetFromONNX ...

  7. 模型量化(3):ONNX 模型的静态量化和动态量化

    转自AI Studio,原文链接:模型量化(3):ONNX 模型的静态量化和动态量化 - 飞桨AI Studio 1. 引入 前面介绍了模型量化的基本原理 也介绍了如何使用 PaddleSlim 对 ...

  8. ONNX 模型的静态量化和动态量化

    文章目录 1 量化介绍 1.1 量化概述 1.2 量化方式 1.3 量化类型 1.4 量化格式 2. 量化实践 2.1 安装依赖 2.2 模型准备 2.3 动态量化 2.4 静态量化 3. 对比测试 ...

  9. 【yolov5】pytorch模型导出为onnx模型

    博主想拿官网的yolov5训练好pt模型,然后转换成rknn模型,然后在瑞芯微开发板上调用模型检测.但是官网的版本对npu不友好,所以采用改进结构的版本: 将Focus层改成Conv层 将Swish激 ...

  10. 使用Relay部署编译ONNX模型

    使用Relay部署编译ONNX模型 本文介绍如何使用Relay部署ONNX模型的入门. 首先,必须安装ONNX软件包. 一个快速的解决方案是安装protobuf编译器,然后 pip install o ...

最新文章

  1. GCLGP | 图卷积高斯过程
  2. Eclipse.自动提示--编写HTML/CSS/JS/JSP代码时自动提示的解决办法
  3. 5界面怎么做窗帘拉动的效果_别让土味窗帘毁了你的家
  4. Sh“.NET研究”arePoint开发笔记-SharePoint2010添加ASP.NET应用程序
  5. 整数域上的多项式辗转相除
  6. (iPhone)怎样从photo album中获取所有图片 “****TWO*****” ---》 获取所有图片从Photo Album?...
  7. ASIHTTPRequest 详解, http 请求终结者
  8. Spark运行命令示例
  9. jquery的语法结构包括哪几部分?_牙釉质的组织学结构具体包括哪几部分
  10. css 毛玻璃_CSS实现雨滴动画效果
  11. 电子工业出版社博文视点在上海第六届UPA中国用户可用性大会上
  12. C/C++ 大小端转换
  13. 巴特沃斯、切比雪夫、贝塞尔滤波器详解:(区别,特点,电路图)
  14. QT设置背景图片的3种方式 区别——设置样式表styleSheet
  15. (9)Android之路====Android系统OTA更新
  16. 除法器的实现(恢复余数、不恢复余数、级数展开、Newton-Raphson)
  17. mysqldump实战-问题2
  18. 最近搞的AGV调度控制中心
  19. 冬令营2015 酱油记
  20. Some linux hits

热门文章

  1. Unity Kinect体感跑酷互动游戏方案
  2. LCD3D打印机和DLP3D打印技术的区别详解
  3. 能上QQ,却不能上网的解决办法
  4. 衡量计算机储存容量的常用计量单位是,衡量存储器的单位是什么
  5. 苹果4.3该如何避免?机审人审怎么过?
  6. 云服务器转租赁协议,云服务器转租赁协议
  7. 舆情监测系统功能及作用
  8. 电场强度 高斯定理 习题
  9. 入侵一个网站的服务器拿数据,入侵一个网站的服务器拿数据库
  10. 计算机如何把应用储存进u盘,怎样把word中的内容保存进u盘 怎样把word文档放到u盘里?...