AutoBLineDiagnosisService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosis.Common.Models;
  5. using AutoBlineCalculationLib;
  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.AutoBLineDiagnosisSDK.Interfaces;
  12. using Vinno.AI.AutoBLineDiagnosisSDK.Models;
  13. using Vinno.AI.AutoBLineDiagnosisService.Tools;
  14. using Vinno.AI.CommonSDK.Enums;
  15. using Vinno.AI.CommonSDK.Models;
  16. using Vinno.AI.CommonSDK.Tools;
  17. using Vinno.AI.Service.Common.Interfaces;
  18. using Vinno.AI.Service.Common.Models;
  19. using Vinno.AI.Service.Common.Tools;
  20. using AutoBLineDiagnosis = AutoBLineDiagnosisSDK.AutoBLineDiagnosis;
  21. namespace Vinno.AI.AutoBLineDiagnosisService
  22. {
  23. public class AutoBLineDiagnosisService : IAutoBLineDiagnosisService, 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 AutoBLineDiagnosis _autoBLineDiagnosis;
  30. private bool _disposed;
  31. public static List<FunctionInfo> AllFunctions { get; }
  32. static AutoBLineDiagnosisService()
  33. {
  34. AllFunctions = AutoBLineDiagnosis.AllFunctions;
  35. }
  36. public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
  37. {
  38. AutoBLineDiagnosis.Init(enableFunction, currentUsedFunction);
  39. }
  40. public AutoBLineDiagnosisService()
  41. {
  42. try
  43. {
  44. _singleImageCache = new ConcurrentStack<byte[]>();
  45. _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForAutoBLineDiagnosisSingleImage);
  46. _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeServer.DataReceived += OnDataReceived;
  48. _singleImagePipeServer.StartAndReceive();
  49. _imageProvider = new ImageProvider(AIEnumType.AutoBLineDiagnosis);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.Error($"AutoBLineDiagnosisService-Constructor error:{ex}");
  54. }
  55. }
  56. public void Initialize(AutoBLineDiagnosisParameter autoBLineParameter, bool hasImageProvider)
  57. {
  58. try
  59. {
  60. CloseDiagnosis();
  61. var cpuNumber = autoBLineParameter.CPUNumber;
  62. var detectMode = (EnumDetectMode)autoBLineParameter.DetectMode;
  63. var detectTps = autoBLineParameter.DetectTps;
  64. var intervalTime = autoBLineParameter.IntervalTime;
  65. var cmPerPixel = autoBLineParameter.CmPerPixel;
  66. _autoBLineDiagnosis = new AutoBLineDiagnosis(Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, cpuNumber, _modelFolder, cmPerPixel, detectMode, detectTps, intervalTime);
  67. _autoBLineDiagnosis.StartEvaluation += OnStartEvaluation;
  68. _autoBLineDiagnosis.FinishEvaluation += OnFinishEvaluation;
  69. _autoBLineDiagnosis.NotifyError += OnNotifyError;
  70. _autoBLineDiagnosis.NotifyLog += OnNotifyLog;
  71. }
  72. catch (Exception ex)
  73. {
  74. Logger.Error($"AutoBLineDiagnosisService-Initialize error:{ex}");
  75. }
  76. }
  77. /// <summary>
  78. /// 切换引擎
  79. /// </summary>
  80. /// <param name="functionInfo"></param>
  81. public void SwitchEngines(FunctionInfo functionInfo)
  82. {
  83. try
  84. {
  85. AutoBLineDiagnosis.SwitchAIEngines(functionInfo);
  86. if (_autoBLineDiagnosis != null)
  87. {
  88. _autoBLineDiagnosis.SwitchAIEngines2(functionInfo);
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. Logger.Error($"AutoBLineDiagnosisService-SwitchEngines error:{ex}");
  94. }
  95. }
  96. public void Start()
  97. {
  98. try
  99. {
  100. if (_autoBLineDiagnosis != null)
  101. {
  102. _autoBLineDiagnosis.Start();
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. Logger.Error($"AutoBLineDiagnosisService-Start error:{ex}");
  108. }
  109. }
  110. public void Stop()
  111. {
  112. try
  113. {
  114. if (_autoBLineDiagnosis != null)
  115. {
  116. _autoBLineDiagnosis.Stop();
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Logger.Error($"AutoBLineDiagnosisService-Stop error:{ex}");
  122. }
  123. }
  124. public void SetDetectTps(int detectTps)
  125. {
  126. try
  127. {
  128. if (_autoBLineDiagnosis != null)
  129. {
  130. _autoBLineDiagnosis.DetectTps = detectTps;
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Logger.Error($"AutoBLineDiagnosisService-SetDetectTps error:{ex}");
  136. }
  137. }
  138. public void SetIntervalTime(int intervalTime)
  139. {
  140. try
  141. {
  142. if (_autoBLineDiagnosis != null)
  143. {
  144. _autoBLineDiagnosis.IntervalTime = intervalTime;
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. Logger.Error($"AutoBLineDiagnosisService-SetIntervalTime error:{ex}");
  150. }
  151. }
  152. public void SetDetectMode(AIEnumDetectMode detectMode)
  153. {
  154. try
  155. {
  156. if (_autoBLineDiagnosis != null)
  157. {
  158. _autoBLineDiagnosis.DetectMode = (EnumDetectMode)detectMode;
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. Logger.Error($"AutoBLineDiagnosisService-SetDetectMode error:{ex}");
  164. }
  165. }
  166. /// <summary>
  167. /// 设置一个像素代表的实际物理距离是多少cm
  168. /// </summary>
  169. /// <param name="cmPerPixel"></param>
  170. public void SetCmPerPixel(float cmPerPixel)
  171. {
  172. try
  173. {
  174. if (_autoBLineDiagnosis != null)
  175. {
  176. _autoBLineDiagnosis.CmPerPixel = cmPerPixel;
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. Logger.Error($"AutoBLineDiagnosisService-SetCmPerPixel error:{ex}");
  182. }
  183. }
  184. /// <summary>
  185. /// 设置探头图像信息
  186. /// </summary>
  187. /// <param name="cmPerPixel"></param>
  188. public void SetProbeImageInfo(TransAIUltrasoundImageRegion transUltrasoundImageRegion)
  189. {
  190. try
  191. {
  192. if (_autoBLineDiagnosis != null)
  193. {
  194. _autoBLineDiagnosis.UltrasoundImageRegion = AICommonServiceConvertHelper.ConvertTransAIUltrasoundImageRegionToIUltrasoundImageRegion(transUltrasoundImageRegion);
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. Logger.Error($"AutoBLineDiagnosisService-SetCmPerPixel error:{ex}");
  200. }
  201. }
  202. /// <summary>
  203. /// 检测单张Byte Image
  204. /// </summary>
  205. /// <param name="cmPerPixel">图像上一个像素实际代表的物理距离是多少cm</param>
  206. /// <returns></returns>
  207. public TransAIBlineScoringResults DetectOneByteImage(float cmPerPixel, TransAIUltrasoundImageRegion transUltrasoundImageRegion)
  208. {
  209. try
  210. {
  211. byte[] byteImage = null;
  212. int retryCount = 0;
  213. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  214. {
  215. Thread.Sleep(10);
  216. retryCount++;
  217. }
  218. if (byteImage == null)
  219. {
  220. Logger.Error($"AutoBLineDiagnosisService-DetectOneByteImage Get Image Cache Fail");
  221. return null;
  222. }
  223. var ultrasoundImageRegion = AICommonServiceConvertHelper.ConvertTransAIUltrasoundImageRegionToIUltrasoundImageRegion(transUltrasoundImageRegion);
  224. var blineScoringResults = _autoBLineDiagnosis.DetectOneImage(byteImage, cmPerPixel, ultrasoundImageRegion);
  225. return AIConvertHelper.ConvertBlineScoringResultsToTransAIBlineScoringResults(blineScoringResults);
  226. }
  227. catch (Exception ex)
  228. {
  229. Logger.Error($"AutoBLineDiagnosisService-DetectOneByteImage error:{ex}");
  230. return null;
  231. }
  232. }
  233. public TransAIBlineScoringResults DetectOneRawImage(int height, int width, AIEnumColorType colorType, float cmPerPixel, TransAIUltrasoundImageRegion transUltrasoundImageRegion)
  234. {
  235. try
  236. {
  237. byte[] byteImage = null;
  238. int retryCount = 0;
  239. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  240. {
  241. Thread.Sleep(10);
  242. retryCount++;
  243. }
  244. if (byteImage == null)
  245. {
  246. Logger.Error($"AutoBLineDiagnosisService-DetectOneRawImage Get Image Cache Fail");
  247. return null;
  248. }
  249. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  250. var ultrasoundImageRegion = AICommonServiceConvertHelper.ConvertTransAIUltrasoundImageRegionToIUltrasoundImageRegion(transUltrasoundImageRegion);
  251. var blineScoringResults = _autoBLineDiagnosis.DetectOneImage(rawImage, cmPerPixel, ultrasoundImageRegion);
  252. return AIConvertHelper.ConvertBlineScoringResultsToTransAIBlineScoringResults(blineScoringResults);
  253. }
  254. catch (Exception ex)
  255. {
  256. Logger.Error($"AutoBLineDiagnosisService-DetectOneRawImage error:{ex}");
  257. return null;
  258. }
  259. }
  260. /// <summary>
  261. /// Send Raw Image Data For Pipe
  262. /// </summary>
  263. /// <param name="height"></param>
  264. /// <param name="width"></param>
  265. /// <param name="colorType"></param>
  266. public void SendRawImageData(int height, int width, AIEnumColorType colorType)
  267. {
  268. try
  269. {
  270. _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
  271. }
  272. catch (Exception ex)
  273. {
  274. Logger.Error($"AutoBLineDiagnosisService-SendRawImageData error:{ex}");
  275. }
  276. }
  277. /// <summary>
  278. /// Send Byte Image Data For Pipe
  279. /// </summary>
  280. public void SendByteImageData()
  281. {
  282. try
  283. {
  284. _imageProvider?.ReceiveByteImageData();
  285. }
  286. catch (Exception ex)
  287. {
  288. Logger.Error($"AutoBLineDiagnosisService-SendByteImageData error:{ex}");
  289. }
  290. }
  291. public void Close()
  292. {
  293. try
  294. {
  295. Logger.Info($"AutoBLineDiagnosisService-Close Invoke");
  296. CloseDiagnosis();
  297. }
  298. catch (Exception ex)
  299. {
  300. Logger.Error($"AutoBLineDiagnosisService-Close error:{ex}");
  301. }
  302. }
  303. private void CloseDiagnosis()
  304. {
  305. if (_autoBLineDiagnosis != null)
  306. {
  307. _autoBLineDiagnosis.StartEvaluation -= OnStartEvaluation;
  308. _autoBLineDiagnosis.FinishEvaluation -= OnFinishEvaluation;
  309. _autoBLineDiagnosis.NotifyError -= OnNotifyError;
  310. _autoBLineDiagnosis.NotifyLog -= OnNotifyLog;
  311. _autoBLineDiagnosis.Close();
  312. _autoBLineDiagnosis = null;
  313. }
  314. }
  315. public void Dispose()
  316. {
  317. try
  318. {
  319. if (!_disposed)
  320. {
  321. Logger.Info($"AutoBLineDiagnosisService-Start Dispose");
  322. CloseDiagnosis();
  323. if (_singleImagePipeServer != null)
  324. {
  325. _singleImagePipeServer.DataReceived -= OnDataReceived;
  326. _singleImagePipeServer.Dispose();
  327. _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
  328. _singleImagePipeServer = null;
  329. }
  330. _imageProvider.Dispose();
  331. _imageProvider = null;
  332. _disposed = true;
  333. Logger.Info($"AutoBLineDiagnosisService Dispose End");
  334. }
  335. }
  336. catch (Exception ex)
  337. {
  338. Logger.Error($"AutoBLineDiagnosisService-Dispose error:{ex}");
  339. }
  340. }
  341. private void OnStartEvaluation(object sender, EventArgs e)
  342. {
  343. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoBLineDiagnosisStartEvaluationRaised, Params = e });
  344. }
  345. private void OnFinishEvaluation(object sender, BlineScoringResults e)
  346. {
  347. var aiBlineScoringResults = AIConvertHelper.ConvertBlineScoringResultsToTransAIBlineScoringResults(e);
  348. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoBLineDiagnosisFinishEvaluationRaised, Params = aiBlineScoringResults });
  349. }
  350. private void OnNotifyLog(object sender, LogEventArgs e)
  351. {
  352. if (e == null)
  353. {
  354. return;
  355. }
  356. switch (e.LogType)
  357. {
  358. case EnumLogType.InfoLog:
  359. Logger.Info($"AutoBLineDiagnosisService OnNotifyLog:{e.Msg}");
  360. break;
  361. case EnumLogType.WarnLog:
  362. Logger.Warning($"AutoBLineDiagnosisService OnNotifyLog:{e.Msg}");
  363. break;
  364. case EnumLogType.ErrorLog:
  365. case EnumLogType.FatalLog:
  366. default:
  367. Logger.Error($"AutoBLineDiagnosisService OnNotifyLog:{e.Msg}");
  368. break;
  369. }
  370. var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
  371. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoBLineDiagnosisNotifyLogRaised, Params = logEventArgs });
  372. }
  373. private void OnNotifyError(object sender, ErrorEventArgs e)
  374. {
  375. Logger.Error($"AutoBLineDiagnosisService OnNotifyError:{e.GetException()}");
  376. var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
  377. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoBLineDiagnosisNotifyLogRaised, Params = logEventArgs });
  378. }
  379. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  380. {
  381. if (e == null)
  382. {
  383. return;
  384. }
  385. switch (e.LogType)
  386. {
  387. case AIEnumLogType.ErrorLog:
  388. case AIEnumLogType.FatalLog:
  389. Logger.Error(e.Msg);
  390. break;
  391. case AIEnumLogType.WarnLog:
  392. Logger.Warning(e.Msg);
  393. break;
  394. case AIEnumLogType.InfoLog:
  395. default:
  396. Logger.Info(e.Msg);
  397. break;
  398. }
  399. }
  400. private void OnDataReceived(object sender, byte[] e)
  401. {
  402. try
  403. {
  404. _singleImageCache.Push(e);
  405. }
  406. catch (Exception ex)
  407. {
  408. Logger.Error($"AutoBLineDiagnosisService-OnDataReceived error:{ex}");
  409. }
  410. }
  411. }
  412. }