YoloV6Detector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Microsoft.ML.OnnxRuntime;
  2. using Microsoft.ML.OnnxRuntime.Tensors;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. namespace YoloTest
  10. {
  11. internal class YoloV6Detector : IDisposable
  12. {
  13. private InferenceSession _session;
  14. public string[] _classNames; // 添加此字段
  15. public YoloV6Detector(string modelPath, string[] classNames = null, bool useGpu = true)
  16. {
  17. var options = new SessionOptions();
  18. Console.WriteLine("选择GPU");
  19. int dNUM = Convert.ToInt32(Console.ReadLine());
  20. if (useGpu)
  21. {
  22. try
  23. {
  24. options.AppendExecutionProvider_DML(dNUM);
  25. Console.WriteLine("DML推理运行");
  26. }
  27. catch(Exception e)
  28. {
  29. Console.WriteLine($"{e.Message}");
  30. options.AppendExecutionProvider_CPU(0);
  31. Console.WriteLine("CPU推理运行");
  32. }
  33. }
  34. else
  35. {
  36. options.AppendExecutionProvider_CPU(0);
  37. Console.WriteLine("CPU推理运行");
  38. }
  39. _session = new InferenceSession(modelPath, options);
  40. _classNames = classNames ?? new string[]
  41. {
  42. "OK", "NG"
  43. };
  44. }
  45. public YoloV6Detector(string modelPath, string[] classNames = null)
  46. {
  47. var options = new SessionOptions();
  48. _session = new InferenceSession(modelPath, options);
  49. // 初始化类别名称
  50. _classNames = classNames ?? new string[]
  51. {
  52. "OK", "NG"
  53. // 根据你的模型实际类别修改
  54. };
  55. }
  56. Stopwatch OnceRunTime = new Stopwatch();
  57. public List<Detection> Detect(Image inputImage)
  58. {
  59. var inputTensor = PreprocessImage(inputImage);
  60. var inputs = new List<NamedOnnxValue>
  61. {
  62. NamedOnnxValue.CreateFromTensor("images", inputTensor)
  63. };
  64. OnceRunTime.Restart();
  65. using (var results = _session.Run(inputs))
  66. {
  67. OnceRunTime.Stop();
  68. Console.WriteLine("单次推理耗时{0}", OnceRunTime.ElapsedMilliseconds);
  69. return Postprocess(results, inputImage.Width, inputImage.Height);
  70. }
  71. }
  72. private Tensor<float> PreprocessImage(Image image)
  73. {
  74. const int inputWidth = 640;
  75. const int inputHeight = 640;
  76. const int channels = 3;
  77. using (var resizedImage = new Bitmap(image, inputWidth, inputHeight))
  78. {
  79. var tensorData = new float[1 * channels * inputHeight * inputWidth];
  80. // 使用 LockBits 直接访问内存,比 GetPixel 快 10-50 倍
  81. var bitmapData = resizedImage.LockBits(
  82. new Rectangle(0, 0, inputWidth, inputHeight),
  83. ImageLockMode.ReadOnly,
  84. PixelFormat.Format24bppRgb);
  85. try
  86. {
  87. unsafe
  88. {
  89. byte* ptr = (byte*)bitmapData.Scan0;
  90. int stride = bitmapData.Stride;
  91. for (int y = 0; y < inputHeight; y++)
  92. {
  93. for (int x = 0; x < inputWidth; x++)
  94. {
  95. byte* pixel = ptr + y * stride + x * 3;
  96. int idx = y * inputWidth + x;
  97. tensorData[idx] = pixel[2] / 255.0f; // R
  98. tensorData[inputHeight * inputWidth + idx] = pixel[1] / 255.0f; // G
  99. tensorData[2 * inputHeight * inputWidth + idx] = pixel[0] / 255.0f; // B
  100. }
  101. }
  102. }
  103. }
  104. finally
  105. {
  106. resizedImage.UnlockBits(bitmapData);
  107. }
  108. return new DenseTensor<float>(tensorData, new[] { 1, channels, inputHeight, inputWidth });
  109. }
  110. }
  111. //private Tensor<float> PreprocessImage(Image image)
  112. //{
  113. // const int inputWidth = 640;
  114. // const int inputHeight = 640;
  115. // const int channels = 3;
  116. // using (var resizedImage = new Bitmap(image, inputWidth, inputHeight))
  117. // {
  118. // var tensorData = new float[1 * channels * inputHeight * inputWidth];
  119. // for (int y = 0; y < inputHeight; y++)
  120. // {
  121. // for (int x = 0; x < inputWidth; x++)
  122. // {
  123. // var pixel = resizedImage.GetPixel(x, y);
  124. // tensorData[y * inputWidth + x] = pixel.R / 255.0f;
  125. // tensorData[inputHeight * inputWidth + y * inputWidth + x] = pixel.G / 255.0f;
  126. // tensorData[2 * inputHeight * inputWidth + y * inputWidth + x] = pixel.B / 255.0f;
  127. // }
  128. // }
  129. // return new DenseTensor<float>(tensorData, new[] { 1, channels, inputHeight, inputWidth });
  130. // }
  131. //}
  132. private List<Detection> Postprocess(IEnumerable<NamedOnnxValue> outputs, int imgWidth, int imgHeight)
  133. {
  134. var detections = new List<Detection>();
  135. // 1. 获取模型输出
  136. var output = outputs.FirstOrDefault();
  137. if (output == null) return detections;
  138. var outputTensor = output.AsTensor<float>();
  139. var outputShape = outputTensor.Dimensions;
  140. // YOLOv6 输出格式:[batch, num_anchors, 85] 或 [batch, num_anchors, 4 + 1 + num_classes]
  141. int numClasses = _classNames.Length;
  142. int numAnchors = outputShape[1];
  143. int numValuesPerAnchor = outputShape[2]; // 通常为 4 + 1 + numClasses
  144. // 2. 解析输出张量
  145. var rawDetections = new List<RawDetection>();
  146. const float confidenceThreshold = 0.4f;
  147. const float nmsThreshold = 0.2f;
  148. for (int i = 0; i < numAnchors; i++)
  149. {
  150. // 获取目标置信度 (第 5 个值,索引从 4 开始)
  151. float objectness = outputTensor[0, i, 4];
  152. if (objectness < confidenceThreshold) continue;
  153. // 获取各类别概率并计算最大置信度
  154. float maxClassProb = 0;
  155. int maxClassId = 0;
  156. for (int c = 0; c < numClasses; c++)
  157. {
  158. float classProb = outputTensor[0, i, 5 + c];
  159. if (classProb > maxClassProb)
  160. {
  161. maxClassProb = classProb;
  162. maxClassId = c;
  163. }
  164. }
  165. float confidence = objectness * maxClassProb;
  166. if (confidence < confidenceThreshold) continue;
  167. // 获取边界框坐标 (cx, cy, w, h)
  168. float cx = outputTensor[0, i, 0];
  169. float cy = outputTensor[0, i, 1];
  170. float w = outputTensor[0, i, 2];
  171. float h = outputTensor[0, i, 3];
  172. // 转换为左上角坐标
  173. float x = cx - w / 2;
  174. float y = cy - h / 2;
  175. rawDetections.Add(new RawDetection
  176. {
  177. X = x,
  178. Y = y,
  179. Width = w,
  180. Height = h,
  181. ClassId = maxClassId,
  182. Confidence = confidence
  183. });
  184. }
  185. // 3. 执行非极大值抑制 (NMS)
  186. var nmsDetections = ApplyNMS(rawDetections, nmsThreshold);
  187. // 4. 将坐标从模型尺寸转换回原始图像尺寸
  188. float scaleX = (float)imgWidth / 640;
  189. float scaleY = (float)imgHeight / 640;
  190. foreach (var det in nmsDetections)
  191. {
  192. detections.Add(new Detection(
  193. det.X * scaleX,
  194. det.Y * scaleY,
  195. det.Width * scaleX,
  196. det.Height * scaleY,
  197. det.ClassId,
  198. det.Confidence
  199. ));
  200. }
  201. return detections;
  202. }
  203. // 辅助类:原始检测结果
  204. private class RawDetection
  205. {
  206. public float X { get; set; }
  207. public float Y { get; set; }
  208. public float Width { get; set; }
  209. public float Height { get; set; }
  210. public int ClassId { get; set; }
  211. public float Confidence { get; set; }
  212. }
  213. // 非极大值抑制 (NMS)
  214. private List<RawDetection> ApplyNMS(List<RawDetection> detections, float iouThreshold)
  215. {
  216. if (detections.Count == 0) return detections;
  217. // 按置信度降序排序
  218. var sorted = detections.OrderByDescending(d => d.Confidence).ToList();
  219. var results = new List<RawDetection>();
  220. while (sorted.Count > 0)
  221. {
  222. var best = sorted[0];
  223. results.Add(best);
  224. sorted.RemoveAt(0);
  225. // 移除与当前最佳检测框 IoU 过高的框
  226. sorted = sorted.Where(d =>
  227. {
  228. if (d.ClassId != best.ClassId) return true;
  229. return CalculateIoU(best, d) < iouThreshold;
  230. }).ToList();
  231. }
  232. return results;
  233. }
  234. // 计算 IoU (交并比)
  235. private float CalculateIoU(RawDetection a, RawDetection b)
  236. {
  237. // 计算交集
  238. float x1 = Math.Max(a.X, b.X);
  239. float y1 = Math.Max(a.Y, b.Y);
  240. float x2 = Math.Min(a.X + a.Width, b.X + b.Width);
  241. float y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);
  242. float intersection = Math.Max(0, x2 - x1) * Math.Max(0, y2 - y1);
  243. // 计算并集
  244. float areaA = a.Width * a.Height;
  245. float areaB = b.Width * b.Height;
  246. float union = areaA + areaB - intersection;
  247. return union > 0 ? intersection / union : 0;
  248. }
  249. public void Dispose()
  250. {
  251. _session?.Dispose();
  252. }
  253. }
  254. // Detection 类定义
  255. public class Detection
  256. {
  257. public float X { get; set; }
  258. public float Y { get; set; }
  259. public float Width { get; set; }
  260. public float Height { get; set; }
  261. public int ClassId { get; set; }
  262. public float Confidence { get; set; }
  263. public Detection(float x, float y, float width, float height, int classId, float confidence)
  264. {
  265. X = x;
  266. Y = y;
  267. Width = width;
  268. Height = height;
  269. ClassId = classId;
  270. Confidence = confidence;
  271. }
  272. }
  273. }