1
0

LiverLesionDetectImg.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.ML.OnnxRuntime;
  4. using Microsoft.ML.OnnxRuntime.Tensors;
  5. using System.Runtime.InteropServices;
  6. using System.IO;
  7. namespace YOLODetectProcessLib
  8. {
  9. public class LiverLesionDetectImg
  10. {
  11. private IInferenceNetwork _inferNet;
  12. /// <summary>
  13. /// 通知订阅者,推理过程中发生了错误
  14. /// </summary>
  15. public event EventHandler<ErrorEventArgs> NotifyError;
  16. public LiverLesionDetectImg(int numCPU, string netDir)
  17. {
  18. try
  19. {
  20. _inferNet = new InferNetOnnxDetectLiverLesion();
  21. _inferNet.LoadNetwork(numCPU, netDir);
  22. }
  23. catch (Exception excep)
  24. {
  25. throw new Exception("Failed at Loading network:" + excep);
  26. }
  27. }
  28. /// <summary>
  29. /// 实现单幅图的乳腺病灶检测
  30. /// </summary>
  31. /// <param name="rawImg">rawImg格式图像</param>
  32. /// <param name="isCropped">是否已经裁切</param>
  33. /// <returns></returns>
  34. public List<DetectedBreastLesionResult> ProcessLiverLesionImg(RawImage rawImg, bool isCropped)
  35. {
  36. try
  37. {
  38. YOLODetectProcessLib.Rect cropRect = new YOLODetectProcessLib.Rect(0, 0, rawImg.Width, rawImg.Height); ;
  39. if (!isCropped)
  40. {
  41. if (!UsImageRegionSegUtils.CropWithCvCore(rawImg, out cropRect))
  42. {
  43. NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Failed at UsImageRegionSegUtils.CropWithCvCore")));
  44. }
  45. }
  46. //对一张图进行推理
  47. InferenceNetworkInputImage inferInput = new InferenceNetworkInputImage(rawImg, cropRect);
  48. var inferResult = _inferNet.Process(new InferenceNetworkInputImage[] { inferInput });
  49. if (inferResult == null)
  50. {
  51. NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Unexpected result size for the breast object detect model of one image.")));
  52. }
  53. if (inferResult.Length != 1)
  54. {
  55. NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Unexpected result size for the breast object detect model of one image.")));
  56. }
  57. if (inferResult[0].Length == 0)
  58. {
  59. return new List<DetectedBreastLesionResult>();
  60. }
  61. else
  62. {
  63. List<DetectedBreastLesionResult> results = new List<DetectedBreastLesionResult>();
  64. for (int ni = 0; ni < inferResult[0].Length; ni++)
  65. {
  66. results.Add(new DetectedBreastLesionResult(inferResult[0][ni].Label, inferResult[0][ni].Confidence, inferResult[0][ni].BoundingBox));
  67. }
  68. return results;
  69. }
  70. }
  71. catch (Exception excep)
  72. {
  73. NotifyError?.Invoke(this, new ErrorEventArgs(excep));
  74. return new List<DetectedBreastLesionResult>();
  75. }
  76. }
  77. /// <summary>
  78. /// 销毁
  79. /// </summary>
  80. public void Dispose()
  81. {
  82. DoDispose();
  83. GC.SuppressFinalize(this);
  84. }
  85. /// <summary>
  86. /// 析构函数
  87. /// </summary>
  88. ~LiverLesionDetectImg()
  89. {
  90. DoDispose();
  91. }
  92. private void DoDispose()
  93. {
  94. _inferNet?.Dispose();
  95. _inferNet = null;
  96. }
  97. }
  98. }