• C# Onnx yolov8 竹签计数、一次性筷子计数


    目录

    效果

    模型信息

    项目

    代码

    数据集

    下载


    C# Onnx yolov8 竹签计数、一次性筷子计数

    效果

    模型信息

    Model Properties
    -------------------------
    date:2024-01-03T08:55:22.768617
    author:Ultralytics
    task:detect
    license:AGPL-3.0 https://ultralytics.com/license
    version:8.0.172
    stride:32
    batch:1
    imgsz:[640, 640]
    names:{0: 'label'}
    ---------------------------------------------------------------

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

    Outputs
    -------------------------
    name:output0
    tensor:Float[1, 5, 8400]
    ---------------------------------------------------------------

    项目

    代码

    1. using Microsoft.ML.OnnxRuntime;
    2. using Microsoft.ML.OnnxRuntime.Tensors;
    3. using OpenCvSharp;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.Drawing;
    7. using System.Drawing.Imaging;
    8. using System.Linq;
    9. using System.Windows.Forms;
    10. namespace Onnx_Yolov8_Demo
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    19. string image_path = "";
    20. string startupPath;
    21. string classer_path;
    22. DateTime dt1 = DateTime.Now;
    23. DateTime dt2 = DateTime.Now;
    24. string model_path;
    25. Mat image;
    26. DetectionResult result_pro;
    27. Mat result_image;
    28. Result result;
    29. SessionOptions options;
    30. InferenceSession onnx_session;
    31. Tensor<float> input_tensor;
    32. List<NamedOnnxValue> input_container;
    33. IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
    34. DisposableNamedOnnxValue[] results_onnxvalue;
    35. Tensor<float> result_tensors;
    36. private void button1_Click(object sender, EventArgs e)
    37. {
    38. OpenFileDialog ofd = new OpenFileDialog();
    39. ofd.Filter = fileFilter;
    40. if (ofd.ShowDialog() != DialogResult.OK) return;
    41. pictureBox1.Image = null;
    42. image_path = ofd.FileName;
    43. pictureBox1.Image = new Bitmap(image_path);
    44. textBox1.Text = "";
    45. image = new Mat(image_path);
    46. pictureBox2.Image = null;
    47. }
    48. private void button2_Click(object sender, EventArgs e)
    49. {
    50. if (image_path == "")
    51. {
    52. return;
    53. }
    54. button2.Enabled = false;
    55. pictureBox2.Image = null;
    56. textBox1.Text = "";
    57. pictureBox2.Image = null;
    58. Application.DoEvents();
    59. //图片缩放
    60. image = new Mat(image_path);
    61. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
    62. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
    63. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
    64. image.CopyTo(new Mat(max_image, roi));
    65. float[] result_array = new float[8400 * 84];
    66. float[] factors = new float[2];
    67. factors[0] = factors[1] = (float)(max_image_length / 640.0);
    68. // 将图片转为RGB通道
    69. Mat image_rgb = new Mat();
    70. Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
    71. Mat resize_image = new Mat();
    72. Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(640, 640));
    73. // 输入Tensor
    74. for (int y = 0; y < resize_image.Height; y++)
    75. {
    76. for (int x = 0; x < resize_image.Width; x++)
    77. {
    78. input_tensor[0, 0, y, x] = resize_image.At<Vec3b>(y, x)[0] / 255f;
    79. input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1] / 255f;
    80. input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2] / 255f;
    81. }
    82. }
    83. //input_tensor 放入一个输入参数的容器,并指定名称
    84. input_container.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));
    85. dt1 = DateTime.Now;
    86. //运行 Inference 并获取结果
    87. result_infer = onnx_session.Run(input_container);
    88. dt2 = DateTime.Now;
    89. // 将输出结果转为DisposableNamedOnnxValue数组
    90. results_onnxvalue = result_infer.ToArray();
    91. // 读取第一个节点输出并转为Tensor数据
    92. result_tensors = results_onnxvalue[0].AsTensor<float>();
    93. result_array = result_tensors.ToArray();
    94. resize_image.Dispose();
    95. image_rgb.Dispose();
    96. result_pro = new DetectionResult(classer_path, factors);
    97. result = result_pro.process_result(result_array);
    98. //result_image = result_pro.draw_result(result, image.Clone());
    99. result_image = result_pro.draw_result2(result, image.Clone());
    100. if (!result_image.Empty())
    101. {
    102. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    103. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\r\n";
    104. textBox1.Text += "Count:" + result.length;
    105. }
    106. else
    107. {
    108. textBox1.Text = "无信息";
    109. }
    110. button2.Enabled = true;
    111. }
    112. private void Form1_Load(object sender, EventArgs e)
    113. {
    114. startupPath = System.Windows.Forms.Application.StartupPath;
    115. model_path = "model/best.onnx";
    116. classer_path = "model/lable.txt";
    117. // 创建输出会话,用于输出模型读取信息
    118. options = new SessionOptions();
    119. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
    120. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
    121. // 创建推理模型类,读取本地模型文件
    122. onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
    123. // 输入Tensor
    124. input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
    125. // 创建输入容器
    126. input_container = new List<NamedOnnxValue>();
    127. image_path = "test_img/0.jpg";
    128. pictureBox1.Image = new Bitmap(image_path);
    129. image = new Mat(image_path);
    130. }
    131. private void pictureBox1_DoubleClick(object sender, EventArgs e)
    132. {
    133. Common.ShowNormalImg(pictureBox1.Image);
    134. }
    135. private void pictureBox2_DoubleClick(object sender, EventArgs e)
    136. {
    137. Common.ShowNormalImg(pictureBox2.Image);
    138. }
    139. SaveFileDialog sdf = new SaveFileDialog();
    140. private void button3_Click(object sender, EventArgs e)
    141. {
    142. if (pictureBox2.Image == null)
    143. {
    144. return;
    145. }
    146. Bitmap output = new Bitmap(pictureBox2.Image);
    147. sdf.Title = "保存";
    148. 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";
    149. if (sdf.ShowDialog() == DialogResult.OK)
    150. {
    151. switch (sdf.FilterIndex)
    152. {
    153. case 1:
    154. {
    155. output.Save(sdf.FileName, ImageFormat.Jpeg);
    156. break;
    157. }
    158. case 2:
    159. {
    160. output.Save(sdf.FileName, ImageFormat.Png);
    161. break;
    162. }
    163. case 3:
    164. {
    165. output.Save(sdf.FileName, ImageFormat.Bmp);
    166. break;
    167. }
    168. case 4:
    169. {
    170. output.Save(sdf.FileName, ImageFormat.Emf);
    171. break;
    172. }
    173. case 5:
    174. {
    175. output.Save(sdf.FileName, ImageFormat.Exif);
    176. break;
    177. }
    178. case 6:
    179. {
    180. output.Save(sdf.FileName, ImageFormat.Gif);
    181. break;
    182. }
    183. case 7:
    184. {
    185. output.Save(sdf.FileName, ImageFormat.Icon);
    186. break;
    187. }
    188. case 8:
    189. {
    190. output.Save(sdf.FileName, ImageFormat.Tiff);
    191. break;
    192. }
    193. case 9:
    194. {
    195. output.Save(sdf.FileName, ImageFormat.Wmf);
    196. break;
    197. }
    198. }
    199. MessageBox.Show("保存成功,位置:" + sdf.FileName);
    200. }
    201. }
    202. }
    203. }

    数据集

    下载

     数据集(带标注)下载

     源码下载

  • 相关阅读:
    重金属行业供应链协同系统:驱动金属产业高质量发展,赋能数字化供应链建设
    CI/CD:安装配置Gitlab Runner
    cad怎么转成pdf文件?3个方法教你实现cad转pdf
    Aimi.fm:AI的生成音乐平台
    云原生 | Docker - [常用命令]
    数仓建设中,数据治理如何切入
    研发效能的逻辑:填补软工鸿沟
    智慧公厕:将科技融入日常生活的创新之举
    生产报工管理系统可以帮助企业解决哪些问题?
    2.6W字系统总结,带你实现 Linux 自由!
  • 原文地址:https://blog.csdn.net/weixin_46771779/article/details/136588249