HepatoRenalRatioDiagnosisService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosis.Common.Models;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Threading;
  10. using UsHepatoRenalRatioDetectLib;
  11. using Vinno.AI.CommonSDK.Enums;
  12. using Vinno.AI.CommonSDK.Models;
  13. using Vinno.AI.CommonSDK.Tools;
  14. using Vinno.AI.HepatoRenalRatioDiagnosisSDK.Enums;
  15. using Vinno.AI.HepatoRenalRatioDiagnosisSDK.Interfaces;
  16. using Vinno.AI.HepatoRenalRatioDiagnosisSDK.Models;
  17. using Vinno.AI.HepatoRenalRatioDiagnosisService.Tools;
  18. using Vinno.AI.Service.Common.Interfaces;
  19. using Vinno.AI.Service.Common.Models;
  20. using Vinno.AI.Service.Common.Tools;
  21. using HepatoRenalRatioDiagnosis = HepatoRenalRatioDiagnosisSDK.HepatoRenalRatioDiagnosis;
  22. namespace Vinno.AI.HepatoRenalRatioDiagnosisService
  23. {
  24. public class HepatoRenalRatioDiagnosisService : IHepatoRenalRatioDiagnosisService, IDisposable, IEngine
  25. {
  26. private readonly string _modelFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  27. private readonly ConcurrentStack<byte[]> _singleImageCache;
  28. private PipeServer _singleImagePipeServer;
  29. private ImageProvider _imageProvider;
  30. private HepatoRenalRatioDiagnosis _hepatoRenalRatioDiagnosis;
  31. private bool _disposed;
  32. public static List<FunctionInfo> AllFunctions { get; }
  33. static HepatoRenalRatioDiagnosisService()
  34. {
  35. AllFunctions = HepatoRenalRatioDiagnosis.AllFunctions;
  36. }
  37. public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
  38. {
  39. HepatoRenalRatioDiagnosis.Init(enableFunction, currentUsedFunction);
  40. }
  41. public HepatoRenalRatioDiagnosisService()
  42. {
  43. try
  44. {
  45. _singleImageCache = new ConcurrentStack<byte[]>();
  46. _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForHepatoRenalRatioDiagnosisSingleImage);
  47. _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
  48. _singleImagePipeServer.DataReceived += OnDataReceived;
  49. _singleImagePipeServer.StartAndReceive();
  50. _imageProvider = new ImageProvider(AIEnumType.HepatoRenalRatioDiagnosis);
  51. }
  52. catch (Exception ex)
  53. {
  54. Logger.Error($"HepatoRenalRatioDiagnosisService-Constructor error:{ex}");
  55. }
  56. }
  57. public void Initialize(HepatoRenalRatioDiagnosisParameter hepatoRenalRatioDiagnosisParameter, bool hasImageProvider)
  58. {
  59. try
  60. {
  61. CloseDiagnosis();
  62. var detectMode = (EnumDetectMode)hepatoRenalRatioDiagnosisParameter.DetectMode;
  63. var detectTps = hepatoRenalRatioDiagnosisParameter.DetectTps;
  64. var intervalTime = hepatoRenalRatioDiagnosisParameter.IntervalTime;
  65. var isCropped = hepatoRenalRatioDiagnosisParameter.IsCropped;
  66. _hepatoRenalRatioDiagnosis = new HepatoRenalRatioDiagnosis();
  67. _hepatoRenalRatioDiagnosis.StartEvaluation += OnStartEvaluation;
  68. _hepatoRenalRatioDiagnosis.FinishEvaluation += OnFinishEvaluation;
  69. _hepatoRenalRatioDiagnosis.NotifyError += OnNotifyError;
  70. _hepatoRenalRatioDiagnosis.NotifyLog += OnNotifyLog;
  71. _hepatoRenalRatioDiagnosis.Initialize(Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, detectMode, isCropped, detectTps, intervalTime, _modelFolder);
  72. }
  73. catch (Exception ex)
  74. {
  75. Logger.Error($"HepatoRenalRatioDiagnosisService-Initialize error:{ex}");
  76. }
  77. }
  78. /// <summary>
  79. /// 切换引擎
  80. /// </summary>
  81. /// <param name="functionInfo"></param>
  82. public void SwitchEngines(FunctionInfo functionInfo)
  83. {
  84. try
  85. {
  86. HepatoRenalRatioDiagnosis.SwitchAIEngines(functionInfo);
  87. if (_hepatoRenalRatioDiagnosis != null)
  88. {
  89. _hepatoRenalRatioDiagnosis.SwitchAIEngines2(functionInfo);
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. Logger.Error($"HepatoRenalRatioDiagnosisService-SwitchEngines error:{ex}");
  95. }
  96. }
  97. public void Start()
  98. {
  99. try
  100. {
  101. if (_hepatoRenalRatioDiagnosis != null)
  102. {
  103. _hepatoRenalRatioDiagnosis.Start();
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. Logger.Error($"HepatoRenalRatioDiagnosisService-Start error:{ex}");
  109. }
  110. }
  111. public void Stop()
  112. {
  113. try
  114. {
  115. if (_hepatoRenalRatioDiagnosis != null)
  116. {
  117. _hepatoRenalRatioDiagnosis.Stop();
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. Logger.Error($"HepatoRenalRatioDiagnosisService-Stop error:{ex}");
  123. }
  124. }
  125. public void SetDetectTps(int detectTps)
  126. {
  127. try
  128. {
  129. if (_hepatoRenalRatioDiagnosis != null)
  130. {
  131. _hepatoRenalRatioDiagnosis.DetectTps = detectTps;
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Logger.Error($"HepatoRenalRatioDiagnosisService-SetDetectTps error:{ex}");
  137. }
  138. }
  139. public void SetIntervalTime(int intervalTime)
  140. {
  141. try
  142. {
  143. if (_hepatoRenalRatioDiagnosis != null)
  144. {
  145. _hepatoRenalRatioDiagnosis.IntervalTime = intervalTime;
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. Logger.Error($"HepatoRenalRatioDiagnosisService-SetIntervalTime error:{ex}");
  151. }
  152. }
  153. public void SetDetectMode(AIEnumDetectMode detectMode)
  154. {
  155. try
  156. {
  157. if (_hepatoRenalRatioDiagnosis != null)
  158. {
  159. _hepatoRenalRatioDiagnosis.DetectMode = (EnumDetectMode)detectMode;
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. Logger.Error($"HepatoRenalRatioDiagnosisService-SetDetectMode error:{ex}");
  165. }
  166. }
  167. public void SetIsCropped(bool isCropped)
  168. {
  169. try
  170. {
  171. if (_hepatoRenalRatioDiagnosis != null)
  172. {
  173. _hepatoRenalRatioDiagnosis.IsCropped = isCropped;
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. Logger.Error($"HepatoRenalRatioDiagnosisService-SetDetectMode error:{ex}");
  179. }
  180. }
  181. /// <summary>
  182. /// 检测单张Byte Image
  183. /// </summary>
  184. /// <returns></returns>
  185. public AIHRRDetectResultPerImg DetectOneByteImage()
  186. {
  187. try
  188. {
  189. byte[] byteImage = null;
  190. int retryCount = 0;
  191. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  192. {
  193. Thread.Sleep(10);
  194. retryCount++;
  195. }
  196. if (byteImage == null)
  197. {
  198. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneByteImage Get Image Cache Fail");
  199. return null;
  200. }
  201. var result = _hepatoRenalRatioDiagnosis.DetectOneImage(byteImage);
  202. var aiHRRDetectResultPerImg = AIConvertHelper.ConvertHRRDetectResultPerImgToAIHRRDetectResultPerImg(result);
  203. return aiHRRDetectResultPerImg;
  204. }
  205. catch (Exception ex)
  206. {
  207. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneByteImage error:{ex}");
  208. return null;
  209. }
  210. }
  211. /// <summary>
  212. /// 检测单张Byte Image,其中IsCropped参数仅本次有效
  213. /// </summary>
  214. /// <returns></returns>
  215. public AIHRRDetectResultPerImg DetectOneByteImageWithTempSetting(bool isCropped)
  216. {
  217. try
  218. {
  219. byte[] byteImage = null;
  220. int retryCount = 0;
  221. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  222. {
  223. Thread.Sleep(10);
  224. retryCount++;
  225. }
  226. if (byteImage == null)
  227. {
  228. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneByteImage Get Image Cache Fail");
  229. return null;
  230. }
  231. var result = _hepatoRenalRatioDiagnosis.DetectOneImage(byteImage, isCropped);
  232. var aiHRRDetectResultPerImg = AIConvertHelper.ConvertHRRDetectResultPerImgToAIHRRDetectResultPerImg(result);
  233. return aiHRRDetectResultPerImg;
  234. }
  235. catch (Exception ex)
  236. {
  237. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneByteImage error:{ex}");
  238. return null;
  239. }
  240. }
  241. public AIHRRDetectResultPerImg DetectOneRawImage(int height, int width, AIEnumColorType colorType)
  242. {
  243. try
  244. {
  245. byte[] byteImage = null;
  246. int retryCount = 0;
  247. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  248. {
  249. Thread.Sleep(10);
  250. retryCount++;
  251. }
  252. if (byteImage == null)
  253. {
  254. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneRawImage Get Image Cache Fail");
  255. return null;
  256. }
  257. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  258. var result = _hepatoRenalRatioDiagnosis.DetectOneImage(rawImage);
  259. var aiHRRDetectResultPerImg = AIConvertHelper.ConvertHRRDetectResultPerImgToAIHRRDetectResultPerImg(result);
  260. return aiHRRDetectResultPerImg;
  261. }
  262. catch (Exception ex)
  263. {
  264. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneRawImage error:{ex}");
  265. return null;
  266. }
  267. }
  268. public AIHRRDetectResultPerImg DetectOneRawImageWithTempSetting(int height, int width, AIEnumColorType colorType, bool isCropped)
  269. {
  270. try
  271. {
  272. byte[] byteImage = null;
  273. int retryCount = 0;
  274. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  275. {
  276. Thread.Sleep(10);
  277. retryCount++;
  278. }
  279. if (byteImage == null)
  280. {
  281. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneRawImage Get Image Cache Fail");
  282. return null;
  283. }
  284. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  285. var result = _hepatoRenalRatioDiagnosis.DetectOneImage(rawImage, isCropped);
  286. var aiHRRDetectResultPerImg = AIConvertHelper.ConvertHRRDetectResultPerImgToAIHRRDetectResultPerImg(result);
  287. return aiHRRDetectResultPerImg;
  288. }
  289. catch (Exception ex)
  290. {
  291. Logger.Error($"HepatoRenalRatioDiagnosisService-DetectOneRawImage error:{ex}");
  292. return null;
  293. }
  294. }
  295. public AIHepatoRenalRatioInfo GetHepatoRenalRatioByByteImage(AIRect rcLiver, AIRect rcKidney)
  296. {
  297. try
  298. {
  299. byte[] byteImage = null;
  300. int retryCount = 0;
  301. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  302. {
  303. Thread.Sleep(10);
  304. retryCount++;
  305. }
  306. if (byteImage == null)
  307. {
  308. Logger.Error($"HepatoRenalRatioDiagnosisService-GetHepatoRenalRatioByByteImage Get Image Cache Fail");
  309. return null;
  310. }
  311. var liverRect = AICommonServiceConvertHelper.ConvertAIRectToRect(rcLiver);
  312. var kidneyRect = AICommonServiceConvertHelper.ConvertAIRectToRect(rcKidney);
  313. var result = _hepatoRenalRatioDiagnosis.GetHepatoRenalRatio(byteImage, liverRect, kidneyRect, out var meanGrayLiverROI, out var meanGrayKidneyROI, out var hrrRatio);
  314. return new AIHepatoRenalRatioInfo(meanGrayLiverROI, meanGrayKidneyROI, hrrRatio, (AIEnumHRRStatus)result);
  315. }
  316. catch (Exception ex)
  317. {
  318. Logger.Error($"HepatoRenalRatioDiagnosisService-GetHepatoRenalRatioByRawImage error:{ex}");
  319. return null;
  320. }
  321. }
  322. public AIHepatoRenalRatioInfo GetHepatoRenalRatioByRawImage(int height, int width, AIEnumColorType colorType, AIRect rcLiver, AIRect rcKidney)
  323. {
  324. try
  325. {
  326. byte[] byteImage = null;
  327. int retryCount = 0;
  328. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  329. {
  330. Thread.Sleep(10);
  331. retryCount++;
  332. }
  333. if (byteImage == null)
  334. {
  335. Logger.Error($"HepatoRenalRatioDiagnosisService-GetHepatoRenalRatioByRawImage Get Image Cache Fail");
  336. return null;
  337. }
  338. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  339. var liverRect = AICommonServiceConvertHelper.ConvertAIRectToRect(rcLiver);
  340. var kidneyRect = AICommonServiceConvertHelper.ConvertAIRectToRect(rcKidney);
  341. var result = _hepatoRenalRatioDiagnosis.GetHepatoRenalRatio(rawImage, liverRect, kidneyRect, out var meanGrayLiverROI, out var meanGrayKidneyROI, out var hrrRatio);
  342. return new AIHepatoRenalRatioInfo(meanGrayLiverROI, meanGrayKidneyROI, hrrRatio, (AIEnumHRRStatus)result);
  343. }
  344. catch (Exception ex)
  345. {
  346. Logger.Error($"HepatoRenalRatioDiagnosisService-GetHepatoRenalRatioByRawImage error:{ex}");
  347. return null;
  348. }
  349. }
  350. /// <summary>
  351. /// Send Raw Image Data For Pipe
  352. /// </summary>
  353. /// <param name="height"></param>
  354. /// <param name="width"></param>
  355. /// <param name="colorType"></param>
  356. public void SendRawImageData(int height, int width, AIEnumColorType colorType)
  357. {
  358. try
  359. {
  360. _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
  361. }
  362. catch (Exception ex)
  363. {
  364. Logger.Error($"HepatoRenalRatioDiagnosisService-SendRawImageData error:{ex}");
  365. }
  366. }
  367. /// <summary>
  368. /// Send Byte Image Data For Pipe
  369. /// </summary>
  370. public void SendByteImageData()
  371. {
  372. try
  373. {
  374. _imageProvider?.ReceiveByteImageData();
  375. }
  376. catch (Exception ex)
  377. {
  378. Logger.Error($"HepatoRenalRatioDiagnosisService-SendByteImageData error:{ex}");
  379. }
  380. }
  381. public void Close()
  382. {
  383. try
  384. {
  385. Logger.Info($"HepatoRenalRatioDiagnosisService-Close Invoke");
  386. CloseDiagnosis();
  387. }
  388. catch (Exception ex)
  389. {
  390. Logger.Error($"HepatoRenalRatioDiagnosisService-Close error:{ex}");
  391. }
  392. }
  393. private void CloseDiagnosis()
  394. {
  395. if (_hepatoRenalRatioDiagnosis != null)
  396. {
  397. _hepatoRenalRatioDiagnosis.StartEvaluation -= OnStartEvaluation;
  398. _hepatoRenalRatioDiagnosis.FinishEvaluation -= OnFinishEvaluation;
  399. _hepatoRenalRatioDiagnosis.NotifyError -= OnNotifyError;
  400. _hepatoRenalRatioDiagnosis.NotifyLog -= OnNotifyLog;
  401. _hepatoRenalRatioDiagnosis.Close();
  402. _hepatoRenalRatioDiagnosis = null;
  403. }
  404. }
  405. public void Dispose()
  406. {
  407. try
  408. {
  409. if (!_disposed)
  410. {
  411. Logger.Info($"HepatoRenalRatioDiagnosisService-Start Dispose");
  412. CloseDiagnosis();
  413. if (_singleImagePipeServer != null)
  414. {
  415. _singleImagePipeServer.DataReceived -= OnDataReceived;
  416. _singleImagePipeServer.Dispose();
  417. _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
  418. _singleImagePipeServer = null;
  419. }
  420. _imageProvider.Dispose();
  421. _imageProvider = null;
  422. _disposed = true;
  423. Logger.Info($"HepatoRenalRatioDiagnosisService Dispose End");
  424. }
  425. }
  426. catch (Exception ex)
  427. {
  428. Logger.Error($"HepatoRenalRatioDiagnosisService-Dispose error:{ex}");
  429. }
  430. }
  431. private void OnStartEvaluation(object sender, EventArgs e)
  432. {
  433. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.HepatoRenalRatioDiagnosisStartEvaluationRaised, Params = e });
  434. }
  435. private void OnFinishEvaluation(object sender, HRRDetectResultPerImg e)
  436. {
  437. var aiHRRDetectResultPerImg = AIConvertHelper.ConvertHRRDetectResultPerImgToAIHRRDetectResultPerImg(e);
  438. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.HepatoRenalRatioDiagnosisFinishEvaluationRaised, Params = aiHRRDetectResultPerImg });
  439. }
  440. private void OnNotifyLog(object sender, LogEventArgs e)
  441. {
  442. if (e == null)
  443. {
  444. return;
  445. }
  446. switch (e.LogType)
  447. {
  448. case EnumLogType.InfoLog:
  449. Logger.Info($"HepatoRenalRatioDiagnosisService OnNotifyLog:{e.Msg}");
  450. break;
  451. case EnumLogType.WarnLog:
  452. Logger.Warning($"HepatoRenalRatioDiagnosisService OnNotifyLog:{e.Msg}");
  453. break;
  454. case EnumLogType.ErrorLog:
  455. case EnumLogType.FatalLog:
  456. default:
  457. Logger.Error($"HepatoRenalRatioDiagnosisService OnNotifyLog:{e.Msg}");
  458. break;
  459. }
  460. var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
  461. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.HepatoRenalRatioDiagnosisNotifyLogRaised, Params = logEventArgs });
  462. }
  463. private void OnNotifyError(object sender, ErrorEventArgs e)
  464. {
  465. Logger.Error($"HepatoRenalRatioDiagnosisService OnNotifyError:{e.GetException()}");
  466. var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
  467. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.HepatoRenalRatioDiagnosisNotifyLogRaised, Params = logEventArgs });
  468. }
  469. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  470. {
  471. if (e == null)
  472. {
  473. return;
  474. }
  475. switch (e.LogType)
  476. {
  477. case AIEnumLogType.ErrorLog:
  478. case AIEnumLogType.FatalLog:
  479. Logger.Error(e.Msg);
  480. break;
  481. case AIEnumLogType.WarnLog:
  482. Logger.Warning(e.Msg);
  483. break;
  484. case AIEnumLogType.InfoLog:
  485. default:
  486. Logger.Info(e.Msg);
  487. break;
  488. }
  489. }
  490. private void OnDataReceived(object sender, byte[] e)
  491. {
  492. try
  493. {
  494. _singleImageCache.Push(e);
  495. }
  496. catch (Exception ex)
  497. {
  498. Logger.Error($"HepatoRenalRatioDiagnosisService-OnDataReceived error:{ex}");
  499. }
  500. }
  501. }
  502. }