0


C# OpenCvSharp DNN FreeYOLO 密集行人检测

C# OpenCvSharp DNN FreeYOLO 密集行人检测

效果

模型信息

Inputs

name:input
tensor:Float[1, 3, 192, 320]


Outputs

name:output
tensor:Float[1, 1260, 6]


项目

代码

  1. using OpenCvSharp;
  2. using OpenCvSharp.Dnn;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace OpenCvSharp_DNN_Demo
  9. {
  10. public partial class frmMain : Form
  11. {
  12. public frmMain()
  13. {
  14. InitializeComponent();
  15. }
  16. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  17. string image_path = "";
  18. DateTime dt1 = DateTime.Now;
  19. DateTime dt2 = DateTime.Now;
  20. float confThreshold;
  21. float nmsThreshold;
  22. int num_stride = 3;
  23. float[] strides = new float[3] { 8.0f, 16.0f, 32.0f };
  24. string modelpath;
  25. int inpHeight;
  26. int inpWidth;
  27. List<string> class_names;
  28. int num_class;
  29. Net opencv_net;
  30. Mat BN_image;
  31. Mat image;
  32. Mat result_image;
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. OpenFileDialog ofd = new OpenFileDialog();
  36. ofd.Filter = fileFilter;
  37. if (ofd.ShowDialog() != DialogResult.OK) return;
  38. pictureBox1.Image = null;
  39. pictureBox2.Image = null;
  40. textBox1.Text = "";
  41. image_path = ofd.FileName;
  42. pictureBox1.Image = new Bitmap(image_path);
  43. image = new Mat(image_path);
  44. }
  45. private void Form1_Load(object sender, EventArgs e)
  46. {
  47. confThreshold = 0.6f;
  48. nmsThreshold = 0.5f;
  49. modelpath = "model/yolo_free_huge_crowdhuman_192x320.onnx";
  50. inpHeight = 192;
  51. inpWidth = 320;
  52. opencv_net = CvDnn.ReadNetFromOnnx(modelpath);
  53. class_names = new List<string>();
  54. class_names.Add("person");
  55. num_class = 1;
  56. image_path = "test_img/1.jpg";
  57. pictureBox1.Image = new Bitmap(image_path);
  58. }
  59. private unsafe void button2_Click(object sender, EventArgs e)
  60. {
  61. if (image_path == "")
  62. {
  63. return;
  64. }
  65. textBox1.Text = "检测中,请稍等……";
  66. pictureBox2.Image = null;
  67. Application.DoEvents();
  68. image = new Mat(image_path);
  69. float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);
  70. int neww = (int)(image.Cols * ratio);
  71. int newh = (int)(image.Rows * ratio);
  72. Mat dstimg = new Mat();
  73. Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));
  74. Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);
  75. BN_image = CvDnn.BlobFromImage(dstimg);
  76. //配置图片输入数据
  77. opencv_net.SetInput(BN_image);
  78. //模型推理,读取推理结果
  79. Mat[] outs = new Mat[1] { new Mat() };
  80. string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();
  81. dt1 = DateTime.Now;
  82. opencv_net.Forward(outs, outBlobNames);
  83. dt2 = DateTime.Now;
  84. int num_proposal = outs[0].Size(1);
  85. int nout = outs[0].Size(2);
  86. float* pdata = (float*)outs[0].Data;
  87. List<float> confidences = new List<float>();
  88. List<Rect> boxes = new List<Rect>();
  89. List<int> classIds = new List<int>();
  90. for (int n = 0; n < num_stride; n++)
  91. {
  92. int num_grid_x = (int)Math.Ceiling(inpWidth / strides[n]);
  93. int num_grid_y = (int)Math.Ceiling(inpHeight / strides[n]);
  94. for (int i = 0; i < num_grid_y; i++)
  95. {
  96. for (int j = 0; j < num_grid_x; j++)
  97. {
  98. float box_score = pdata[4];
  99. int max_ind = 0;
  100. float max_class_socre = 0;
  101. for (int k = 0; k < num_class; k++)
  102. {
  103. if (pdata[k + 5] > max_class_socre)
  104. {
  105. max_class_socre = pdata[k + 5];
  106. max_ind = k;
  107. }
  108. }
  109. max_class_socre = max_class_socre* box_score;
  110. max_class_socre = (float)Math.Sqrt(max_class_socre);
  111. if (max_class_socre > confThreshold)
  112. {
  113. float cx = (0.5f + j + pdata[0]) * strides[n]; //cx
  114. float cy = (0.5f + i + pdata[1]) * strides[n]; //cy
  115. float w = (float)(Math.Exp(pdata[2]) * strides[n]); //w
  116. float h = (float)(Math.Exp(pdata[3]) * strides[n]); //h
  117. float xmin = (float)((cx - 0.5 * w) / ratio);
  118. float ymin = (float)((cy - 0.5 * h) / ratio);
  119. float xmax = (float)((cx + 0.5 * w) / ratio);
  120. float ymax = (float)((cy + 0.5 * h) / ratio);
  121. int left = (int)((cx - 0.5 * w) / ratio);
  122. int top = (int)((cy - 0.5 * h) / ratio);
  123. int width = (int)(w / ratio);
  124. int height = (int)(h / ratio);
  125. confidences.Add(max_class_socre);
  126. boxes.Add(new Rect(left, top, width, height));
  127. classIds.Add(max_ind);
  128. }
  129. pdata += nout;
  130. }
  131. }
  132. }
  133. int[] indices;
  134. CvDnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, out indices);
  135. result_image = image.Clone();
  136. for (int ii = 0; ii < indices.Length; ++ii)
  137. {
  138. int idx = indices[ii];
  139. Rect box = boxes[idx];
  140. Cv2.Rectangle(result_image, new OpenCvSharp.Point(box.X, box.Y), new OpenCvSharp.Point(box.X + box.Width, box.Y + box.Height), new Scalar(0, 0, 255), 2);
  141. string label = class_names[classIds[idx]] + ":" + confidences[idx].ToString("0.00");
  142. Cv2.PutText(result_image, label, new OpenCvSharp.Point(box.X, box.Y - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2);
  143. }
  144. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  145. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  146. }
  147. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  148. {
  149. Common.ShowNormalImg(pictureBox2.Image);
  150. }
  151. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  152. {
  153. Common.ShowNormalImg(pictureBox1.Image);
  154. }
  155. }
  156. }

下载

可执行程序exe下载

源码下载

标签: c# dnn 开发语言

本文转载自: https://blog.csdn.net/weixin_46771779/article/details/136558700
版权归原作者 乱蜂朝王 所有, 如有侵权,请联系我们删除。

“C# OpenCvSharp DNN FreeYOLO 密集行人检测”的评论:

还没有评论