MyocardialDiagnosisService.cs 20 KB

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