当前位置:   article > 正文

C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)

C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)

目录

效果

模型信息

mot_ppyoloe_s_36e_ppvehicle.onnx 

vehicle_attribute_model.onnx

项目

代码

下载

其他


C# Onnx PP-Vehicle 车辆分析(包含:车辆检测,识别车型和车辆颜色)

效果

模型信息

mot_ppyoloe_s_36e_ppvehicle.onnx 

Inputs
-------------------------
name:image
tensor:Float[-1, 3, 640, 640]
name:scale_factor
tensor:Float[-1, 2]
---------------------------------------------------------------

Outputs
-------------------------
name:multiclass_nms3_0.tmp_0
tensor:Float[-1, 6]
name:multiclass_nms3_0.tmp_2
tensor:Int32[1]
---------------------------------------------------------------

vehicle_attribute_model.onnx

Inputs
-------------------------
name:x
tensor:Float[-1, 3, 192, 256]
---------------------------------------------------------------

Outputs
-------------------------
name:sigmoid_2.tmp_0
tensor:Float[-1, 19]
---------------------------------------------------------------

项目

VS2022

.net framework 4.8

OpenCvSharp 4.8

Microsoft.ML.OnnxRuntime 1.16.2

代码

  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Text;
  7. namespace Onnx_Demo
  8. {
  9. public partial class frmMain : Form
  10. {
  11. public frmMain()
  12. {
  13. InitializeComponent();
  14. }
  15. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  16. string image_path = "";
  17. DateTime dt1 = DateTime.Now;
  18. DateTime dt2 = DateTime.Now;
  19. Mat image;
  20. PP_YOLOE pp_yoloe;
  21. VehicleAttr vehicleAttr;
  22. StringBuilder sb = new StringBuilder();
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. OpenFileDialog ofd = new OpenFileDialog();
  26. ofd.Filter = fileFilter;
  27. if (ofd.ShowDialog() != DialogResult.OK) return;
  28. pictureBox1.Image = null;
  29. pictureBox2.Image = null;
  30. textBox1.Text = "";
  31. image_path = ofd.FileName;
  32. pictureBox1.Image = new System.Drawing.Bitmap(image_path);
  33. image = new Mat(image_path);
  34. }
  35. private void Form1_Load(object sender, EventArgs e)
  36. {
  37. pp_yoloe = new PP_YOLOE("model/mot_ppyoloe_s_36e_ppvehicle.onnx", 0.6f);
  38. vehicleAttr = new VehicleAttr("model/vehicle_attribute_model.onnx");
  39. image_path = "test_img/1.jpg";
  40. pictureBox1.Image = new Bitmap(image_path);
  41. }
  42. private unsafe void button2_Click(object sender, EventArgs e)
  43. {
  44. if (image_path == "")
  45. {
  46. return;
  47. }
  48. textBox1.Text = "检测中,请稍等……";
  49. pictureBox2.Image = null;
  50. sb.Clear();
  51. System.Windows.Forms.Application.DoEvents();
  52. image = new Mat(image_path);
  53. dt1 = DateTime.Now;
  54. List<BoxInfo> ltBoxInfo = pp_yoloe.Detect(image);
  55. dt2 = DateTime.Now;
  56. Mat result_image = image.Clone();
  57. //pp_yoloe.DrawPred(result_image, ltBoxInfo);
  58. sb.AppendLine("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
  59. sb.AppendLine("------------------------------");
  60. for (int n = 0; n < ltBoxInfo.Count; n++)
  61. {
  62. Rect rect = new Rect();
  63. rect.X = (int)ltBoxInfo[n].xmin;
  64. rect.Y = (int)ltBoxInfo[n].ymin;
  65. rect.Width = (int)(ltBoxInfo[n].xmax - ltBoxInfo[n].xmin);
  66. rect.Height = (int)(ltBoxInfo[n].ymax - ltBoxInfo[n].ymin);
  67. Mat crop_img = new Mat(image, rect);
  68. string color_res_str = "color:";
  69. string type_res_str = "type:";
  70. vehicleAttr.Detect(crop_img, out color_res_str, out type_res_str);
  71. Cv2.Rectangle(result_image, new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin), new OpenCvSharp.Point(ltBoxInfo[n].xmax, ltBoxInfo[n].ymax), new Scalar(0, 0, 255), 2);
  72. Cv2.PutText(result_image
  73. , type_res_str + "," + color_res_str
  74. , new OpenCvSharp.Point(ltBoxInfo[n].xmin, ltBoxInfo[n].ymin - 10)
  75. , HersheyFonts.HersheySimplex
  76. , 1
  77. , new Scalar(0, 255, 0)
  78. , 2);
  79. sb.AppendLine("vehicle:" + ltBoxInfo[n].score.ToString("0.00") + " " + type_res_str + "," + color_res_str);
  80. }
  81. if (pictureBox2.Image != null)
  82. {
  83. pictureBox2.Image.Dispose();
  84. }
  85. pictureBox2.Image = new System.Drawing.Bitmap(result_image.ToMemoryStream());
  86. textBox1.Text = sb.ToString();
  87. }
  88. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  89. {
  90. Common.ShowNormalImg(pictureBox2.Image);
  91. }
  92. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  93. {
  94. Common.ShowNormalImg(pictureBox1.Image);
  95. }
  96. }
  97. }

下载

源码下载

其他

C# PaddleOCR 车牌识别参考 https://lw112190.blog.csdn.net/article/details/131010997

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/205723
推荐阅读
相关标签
  

闽ICP备14008679号