HumanSurfaceLiverSegment.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using RUSInferNet;
  4. using RUSInferNet.Vision;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace HumanOrganSegDemo.HumanBodyPartAnalyser.OrganSegment
  12. {
  13. public class HumanSurfaceLiverSegment : IHumanSurfaceOrganSegment
  14. {
  15. #region field
  16. private IInferenceNetwork _inferNet;
  17. private static InferenceConfig _inferenceCore = new InferenceConfig();
  18. private RawImage _rawImg;
  19. #endregion
  20. #region public funcs
  21. /// <summary>
  22. /// 通知订阅者,推理过程中发生了错误
  23. /// </summary>
  24. public event EventHandler<ErrorEventArgs> NotifyError;
  25. /// <summary>
  26. /// 通知订阅者,有log要记
  27. /// </summary>
  28. public event EventHandler<LogEventArgs> NotifyLogWrite;
  29. /// <summary>
  30. /// 实现单幅图肝脏轮廓的分割
  31. /// </summary>
  32. /// <param name="rawImg">rawImg格式图像</param>
  33. /// <returns></returns>
  34. public IDetectedObject[] EvaluateOneImage(RawImage image, Rect roi)
  35. {
  36. try
  37. {
  38. _rawImg = image;
  39. //Rect cropRect = new Rect(0, 0, _rawImg.Width, _rawImg.Height);
  40. IContour[] imgROIs = new IContour[1];
  41. imgROIs[0] = roi.ToContour();
  42. //对一张图进行推理
  43. InferenceNetworkInputImage inferInput = new InferenceNetworkInputImage(_rawImg, imgROIs);
  44. IDetectedObject[] inferResult = _inferNet.Process(inferInput);
  45. var count = inferResult.Length;
  46. for (int ni = 0; ni < count; ni++)
  47. {
  48. inferResult[ni] = (DetectedObject)inferResult[ni];
  49. }
  50. return inferResult;
  51. }
  52. catch (Exception excep)
  53. {
  54. NotifyError?.Invoke(this, new ErrorEventArgs(excep));
  55. return new DetectedObject[] { };
  56. }
  57. }
  58. /// <summary>
  59. /// 销毁
  60. /// </summary>
  61. public void Dispose()
  62. {
  63. DoDispose();
  64. GC.SuppressFinalize(this);
  65. }
  66. /// <summary>
  67. /// 析构函数
  68. /// </summary>
  69. ~HumanSurfaceLiverSegment()
  70. {
  71. DoDispose();
  72. }
  73. #endregion
  74. #region constructor
  75. /// <summary>
  76. /// 构造函数
  77. /// </summary>
  78. /// <param name="numCpu"></param>
  79. /// <param name="netDir"></param>
  80. /// <param name="workName"></param>
  81. /// <exception cref="Exception"></exception>
  82. public HumanSurfaceLiverSegment(int numCpu, string netDir)
  83. {
  84. try
  85. {
  86. _inferNet = new InferNetOnnxHumanSurfaceLiverSeg();
  87. if (!_inferNet.NetworkLoaded)
  88. {
  89. //string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  90. // 设置inferCore的参数
  91. Dictionary<EnumInferenceConfigKey, string> config = new Dictionary<EnumInferenceConfigKey, string>{
  92. { EnumInferenceConfigKey.CPU_THREADS_NUM, numCpu.ToString()}};
  93. _inferenceCore.SetConfig(config, EnumDeviceType.CPU);
  94. _inferNet.LoadNetwork(_inferenceCore, EnumDeviceType.CPU, netDir);
  95. }
  96. }
  97. catch (Exception excep)
  98. {
  99. throw new Exception("Failed at Loading network:" + excep);
  100. }
  101. }
  102. #endregion
  103. #region private funcs
  104. /// <summary>
  105. /// 主动销毁
  106. /// </summary>
  107. private void DoDispose()
  108. {
  109. _inferNet?.Dispose();
  110. _inferNet = null;
  111. }
  112. #endregion
  113. }
  114. }