UsHepatoRenalRatioDetect.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AI.Common.Tools;
  4. using UsHepatoRenalRatioDetectLib.OrganSegProcessModule;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using UsHepatoRenalRatioDetectLib.EchoROIAnalyserModule;
  14. using System.ComponentModel;
  15. using UsHepatoRenalRatioDetectLib.UsHRRCalculaterModule;
  16. namespace UsHepatoRenalRatioDetectLib
  17. {
  18. public class UsHepatoRenalRatioDetect : IUsHepatoRenalRatioDetect
  19. {
  20. #region private
  21. private readonly object _netNameLocker = new object();
  22. private IOrganSegProcess _organSegProcess;
  23. private bool _isCropped = false;
  24. private volatile bool _initialized = false;
  25. private volatile bool _enable = false;
  26. private ConcurrentQueue<RawImage> _inputImages = new ConcurrentQueue<RawImage>();
  27. private const int _queueMaxSize = 1;
  28. private readonly ManualResetEvent _waitImageEvent = new ManualResetEvent(true);
  29. private readonly ManualResetEvent _processFinishEvent = new ManualResetEvent(true);
  30. private Thread _imageProcessThread;
  31. private volatile bool _disposing = false;
  32. private IUsHRRAnalyser _echoAnalyser = new UsHRRAnalyser();
  33. private IUsHRRCalculater _echoCalculater = new UsHRRCalculater();
  34. private InferenceCore _inferenceCore = new InferenceCore();
  35. #endregion
  36. #region 实现接口
  37. /// <summary>
  38. /// 通知订阅者,有log要记
  39. /// </summary>
  40. public event EventHandler<LogEventArgs> NotifyLog;
  41. /// <summary>
  42. /// 通知订阅者,有错误发生
  43. /// </summary>
  44. public event EventHandler<ErrorEventArgs> NotifyError;
  45. /// <summary>
  46. /// 通知订阅者,肝肾比计算结果有更新
  47. /// </summary>
  48. public event EventHandler<UsHRRDetectUpdateEvent> NotifyProcessFinish;
  49. /// <summary>
  50. /// 初始化分割模型
  51. /// </summary>
  52. /// <param name="netDir">模型地址</param>
  53. /// <param name="isCropped">是否裁剪</param>
  54. public void Init(string netDir, bool isCropped)
  55. {
  56. if (!_initialized)
  57. {
  58. if (_organSegProcess == null)
  59. {
  60. _organSegProcess = new OrganSegProcess();
  61. _organSegProcess.NotifyLog += OnLogWrite;
  62. int modelCpu = 1;
  63. // 设置inferCore的参数
  64. _inferenceCore.SetConfig(EnumInferCoreConfigKey.CPU_THREADS_NUM, modelCpu.ToString(), EnumDeviceType.CPU);
  65. _organSegProcess.Init(_inferenceCore, EnumDeviceType.CPU, netDir);
  66. }
  67. _isCropped = isCropped;
  68. _initialized = true;
  69. }
  70. }
  71. /// <summary>
  72. /// 加载网络
  73. /// </summary>
  74. public void LoadInferNet()
  75. {
  76. if (!_initialized)
  77. {
  78. NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("HepatoRenalEchoContrastDetect should be initialized before use.")));
  79. }
  80. _organSegProcess.LoadGuideInferNet();
  81. }
  82. /// <summary>
  83. /// 开始处理
  84. /// </summary>
  85. public void StartProcess()
  86. {
  87. _enable = true;
  88. }
  89. /// <summary>
  90. /// 停止处理
  91. /// </summary>
  92. public void StopProcess()
  93. {
  94. _enable = false;
  95. // 是否需要清空待处理队列里的数据?
  96. }
  97. /// <summary>
  98. /// 更新超声图像
  99. /// </summary>
  100. /// <param name="image"></param>
  101. public void UpdateUsImage(RawImage image)
  102. {
  103. if (!_initialized)
  104. {
  105. NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("HepatoRenalEchoContrastDetect should be initialized before use.")));
  106. }
  107. if (_enable)
  108. {
  109. _inputImages.Enqueue(image.Clone());
  110. _waitImageEvent.Set();
  111. if (_imageProcessThread == null || !_imageProcessThread.IsAlive)
  112. {
  113. _imageProcessThread = new Thread(() => DoImageProcess())
  114. {
  115. IsBackground = true,
  116. Name = "ImageProcess"
  117. };
  118. _imageProcessThread.Start();
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 计算两个Roi区域内回声比值
  124. /// </summary>
  125. /// <param name="image"></param>
  126. /// <param name="rcLiver"></param>
  127. /// <param name="rcKidney"></param>
  128. /// <param name="echoContrastValue"></param>
  129. public double GetHepatoRenalRatio(RawImage image, Rect rcLiver, Rect rcKidney)
  130. {
  131. EnumHRRStatus status = EnumHRRStatus.HRR_SUCCESSED;
  132. double hrrRatio = 0;
  133. try
  134. {
  135. status = _echoCalculater.GetHepatoRenalRatio(image, rcLiver, rcKidney, out hrrRatio);
  136. if (status!= EnumHRRStatus.HRR_SUCCESSED)
  137. {
  138. // 通知订阅者,预处理的结果有更新
  139. NotifyProcessFinish?.Invoke(this, new UsHRRDetectUpdateEvent(status,
  140. new HRRDetectResultPerImg(), new Dictionary<EnumProcessTime, int> (), new RawImage()));
  141. }
  142. }
  143. catch (Exception excep)
  144. {
  145. NotifyError?.Invoke(this, new ErrorEventArgs(excep));
  146. }
  147. return hrrRatio;
  148. }
  149. /// <summary>
  150. /// 主动销毁
  151. /// </summary>
  152. public void Dispose()
  153. {
  154. DoDispose();
  155. GC.SuppressFinalize(this);
  156. LogHelper.InfoLog("HepatoRenalEchoContrastDetect.Disposed manually.", string.Empty);
  157. }
  158. /// <summary>
  159. /// 析构
  160. /// </summary>
  161. ~UsHepatoRenalRatioDetect()
  162. {
  163. DoDispose();
  164. LogHelper.InfoLog("HepatoRenalEchoContrastDetect.Disposed by destructor.", string.Empty);
  165. }
  166. #endregion
  167. #region private funcs
  168. /// <summary>
  169. /// 对图像进行处理
  170. /// </summary>
  171. private void DoImageProcess()
  172. {
  173. while (!_disposing)
  174. {
  175. if (_inputImages.Count > 0)
  176. {
  177. _inputImages.TryDequeue(out RawImage image);
  178. // 如果队列里待处理的数据过多,则对该图不做处理,直接跳到下一幅图
  179. if (_inputImages.Count >= _queueMaxSize)
  180. {
  181. image.Dispose();
  182. continue;
  183. }
  184. try
  185. {
  186. // 让dispose的线程等待执行完毕后再销毁
  187. _processFinishEvent.Reset();
  188. var totalStartTime = Environment.TickCount;
  189. var OraganSegStartTime = Environment.TickCount;
  190. // 对当前图像进行推理
  191. var result = _organSegProcess.EvaluateOneImage(image, _isCropped);
  192. var OrganSegTime = Environment.TickCount - OraganSegStartTime;
  193. // 可能会存在检测出其他脏器的情况,先判断是否肝脏和肾脏都被检测出,如有未检测到的情况,后续不作处理
  194. List<DetectedOrgan> detectOrgans = new List<DetectedOrgan>();
  195. for (int ni = 0; ni < result.Count; ni++)
  196. {
  197. var detectedOb = result[ni];
  198. if (detectedOb.Organ == EnumOrgans.Liver || detectedOb.Organ == EnumOrgans.Kidney)
  199. {
  200. detectOrgans.Add(detectedOb);
  201. }
  202. }
  203. SegResultPerOrgan[] segResult = new SegResultPerOrgan[detectOrgans.Count];
  204. for (int ni = 0; ni < detectOrgans.Count; ni++)
  205. {
  206. var detectedOb = detectOrgans[ni];
  207. segResult[ni] = new SegResultPerOrgan(detectedOb.Organ, detectedOb.Confidence, detectedOb.BoundingBox, detectedOb.Contours);
  208. }
  209. Dictionary<EnumOrgans, Rect> rois = new Dictionary<EnumOrgans, Rect>();
  210. RawImage mask = new RawImage();
  211. double ratio = 0;
  212. // 只有当肝肾都被检测到的时候才进行处理
  213. if (detectOrgans.Count == 2)
  214. {
  215. EnumHRRStatus status = new EnumHRRStatus();
  216. _echoAnalyser.Init(image, segResult[0].Contours[0], segResult[1].Contours[0]);
  217. status = _echoAnalyser.ImagePreProcess(/*out RawImage maskTmp*/);
  218. if (status != EnumHRRStatus.HRR_SUCCESSED)
  219. {
  220. // 通知订阅者,预处理的结果有更新
  221. NotifyProcessFinish?.Invoke(this, new UsHRRDetectUpdateEvent(status,
  222. new HRRDetectResultPerImg(), new Dictionary<EnumProcessTime, int>(), new RawImage()));
  223. _echoAnalyser.Dispose();
  224. // 用过后的图像销毁
  225. image.Dispose();
  226. // 执行完毕了,dispose的线程可以开始销毁了
  227. _processFinishEvent.Set();
  228. continue;
  229. }
  230. mask = _echoAnalyser.GetHRRMask();
  231. status = _echoAnalyser.HRRROIDetect(out Rect[] roisDec);
  232. if (status != EnumHRRStatus.HRR_SUCCESSED)
  233. {
  234. // 通知订阅者,预处理的结果有更新
  235. NotifyProcessFinish?.Invoke(this, new UsHRRDetectUpdateEvent(status,
  236. new HRRDetectResultPerImg(), new Dictionary<EnumProcessTime, int>(), new RawImage()));
  237. _echoAnalyser.Dispose();
  238. // 用过后的图像销毁
  239. image.Dispose();
  240. mask.Dispose();
  241. // 执行完毕了,dispose的线程可以开始销毁了
  242. _processFinishEvent.Set();
  243. continue;
  244. }
  245. status = _echoCalculater.GetHepatoRenalRatio(image, roisDec[0], roisDec[1], out ratio);
  246. if (status != EnumHRRStatus.HRR_SUCCESSED)
  247. {
  248. // 通知订阅者,预处理的结果有更新
  249. NotifyProcessFinish?.Invoke(this, new UsHRRDetectUpdateEvent(status,
  250. new HRRDetectResultPerImg(), new Dictionary<EnumProcessTime, int>(), new RawImage()));
  251. _echoAnalyser.Dispose();
  252. // 用过后的图像销毁
  253. image.Dispose();
  254. mask.Dispose();
  255. // 执行完毕了,dispose的线程可以开始销毁了
  256. _processFinishEvent.Set();
  257. continue;
  258. }
  259. _echoAnalyser.Dispose();
  260. rois.Add(EnumOrgans.Liver, roisDec[0]);
  261. rois.Add(EnumOrgans.Kidney, roisDec[1]);
  262. }
  263. // 用过后的图像销毁
  264. image.Dispose();
  265. // 执行完毕了,dispose的线程可以开始销毁了
  266. _processFinishEvent.Set();
  267. var totalProcessTime = Environment.TickCount - totalStartTime;
  268. Dictionary<EnumProcessTime, int> timeElapsed = new Dictionary<EnumProcessTime, int>();
  269. timeElapsed.Add(EnumProcessTime.OrganSegProcessTime, OrganSegTime);
  270. timeElapsed.Add(EnumProcessTime.TotalTime, totalProcessTime);
  271. // 通知订阅者,预处理的结果有更新
  272. NotifyProcessFinish?.Invoke(this, new UsHRRDetectUpdateEvent(EnumHRRStatus.HRR_SUCCESSED, new HRRDetectResultPerImg(segResult, rois, ratio), timeElapsed, mask));
  273. mask.Dispose();
  274. }
  275. catch (Exception excep)
  276. {
  277. _processFinishEvent.Set();
  278. NotifyError?.Invoke(this, new ErrorEventArgs(excep));
  279. }
  280. }
  281. else
  282. {
  283. // 如果已经没有要处理的数据了,就等有新数据输入的时候再继续while循环
  284. _waitImageEvent.Reset();
  285. _waitImageEvent.WaitOne();
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// 销毁
  291. /// </summary>
  292. private void DoDispose()
  293. {
  294. if (!_disposing)
  295. {
  296. _disposing = true;
  297. _waitImageEvent.Set();
  298. _processFinishEvent.WaitOne();
  299. if (_organSegProcess != null)
  300. {
  301. _organSegProcess.Dispose();
  302. _organSegProcess.NotifyLog -= OnLogWrite;
  303. _organSegProcess = null;
  304. }
  305. while (_inputImages.Count > 0)
  306. {
  307. if (_inputImages.TryDequeue(out var input))
  308. {
  309. input?.Dispose();
  310. }
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 有log要记
  316. /// </summary>
  317. /// <param name="sender"></param>
  318. /// <param name="e"></param>
  319. private void OnLogWrite(object sender, LogEventArgs e)
  320. {
  321. NotifyLog?.Invoke(sender, e);
  322. }
  323. #endregion
  324. }
  325. }