CarotidClassificationService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosis.Common.Models;
  5. using CarotidArterySectionClassificationLib;
  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.CarotidClassificationSDK.Interfaces;
  12. using Vinno.AI.CarotidClassificationSDK.Models;
  13. using Vinno.AI.CarotidClassificationService.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 CarotidClassification = CarotidClassificationSDK.CarotidClassification;
  21. namespace Vinno.AI.CarotidClassificationService
  22. {
  23. public class CarotidClassificationService : ICarotidClassificationService, IDisposable, IEngine
  24. {
  25. private readonly ConcurrentStack<byte[]> _singleImageCache;
  26. private readonly string _modelFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  27. private PipeServer _singleImagePipeServer;
  28. private ImageProvider _imageProvider;
  29. private CarotidClassification _carotidClassification;
  30. private bool _disposed;
  31. public static List<FunctionInfo> AllFunctions { get; }
  32. static CarotidClassificationService()
  33. {
  34. AllFunctions = CarotidClassification.AllFunctions;
  35. }
  36. public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
  37. {
  38. CarotidClassification.Init(enableFunction, currentUsedFunction);
  39. }
  40. public CarotidClassificationService()
  41. {
  42. try
  43. {
  44. _singleImageCache = new ConcurrentStack<byte[]>();
  45. _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForCarotidClassificationSingleImage);
  46. _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeServer.DataReceived += OnDataReceived;
  48. _singleImagePipeServer.StartAndReceive();
  49. _imageProvider = new ImageProvider(AIEnumType.CarotidClassification);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.Error($"CarotidClassificationService-Constructor error:{ex}");
  54. }
  55. }
  56. public void Initialize(CarotidClassificationParameter carotidClassificationParameter, bool hasImageProvider)
  57. {
  58. try
  59. {
  60. CloseDiagnosis();
  61. var cpuNumber = carotidClassificationParameter.CPUNumber;
  62. var detectMode = (EnumDetectMode)carotidClassificationParameter.DetectMode;
  63. var detectTps = carotidClassificationParameter.DetectTps;
  64. var intervalTime = carotidClassificationParameter.IntervalTime;
  65. var isIncludeContour = carotidClassificationParameter.IsIncludeContour;
  66. _carotidClassification = new CarotidClassification(Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, cpuNumber, _modelFolder, detectMode, detectTps, intervalTime, isIncludeContour);
  67. _carotidClassification.StartEvaluation += OnStartEvaluation;
  68. _carotidClassification.FinishEvaluation += OnFinishEvaluation;
  69. _carotidClassification.NotifyError += OnNotifyError;
  70. _carotidClassification.NotifyLog += OnNotifyLog;
  71. }
  72. catch (Exception ex)
  73. {
  74. Logger.Error($"CarotidClassificationService-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. CarotidClassification.SwitchAIEngines(functionInfo);
  86. if (_carotidClassification != null)
  87. {
  88. _carotidClassification.SwitchAIEngines2(functionInfo);
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. Logger.Error($"CarotidClassificationService-SwitchEngines error:{ex}");
  94. }
  95. }
  96. public void Start()
  97. {
  98. try
  99. {
  100. if (_carotidClassification != null)
  101. {
  102. _carotidClassification.Start();
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. Logger.Error($"CarotidClassificationService-Start error:{ex}");
  108. }
  109. }
  110. public void Stop()
  111. {
  112. try
  113. {
  114. if (_carotidClassification != null)
  115. {
  116. _carotidClassification.Stop();
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Logger.Error($"CarotidClassificationService-Stop error:{ex}");
  122. }
  123. }
  124. public void SetDetectTps(int detectTps)
  125. {
  126. try
  127. {
  128. if (_carotidClassification != null)
  129. {
  130. _carotidClassification.DetectTps = detectTps;
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Logger.Error($"CarotidClassificationService-SetDetectTps error:{ex}");
  136. }
  137. }
  138. public void SetIntervalTime(int intervalTime)
  139. {
  140. try
  141. {
  142. if (_carotidClassification != null)
  143. {
  144. _carotidClassification.IntervalTime = intervalTime;
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. Logger.Error($"CarotidClassificationService-SetIntervalTime error:{ex}");
  150. }
  151. }
  152. public void SetDetectMode(AIEnumDetectMode detectMode)
  153. {
  154. try
  155. {
  156. if (_carotidClassification != null)
  157. {
  158. _carotidClassification.DetectMode = (EnumDetectMode)detectMode;
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. Logger.Error($"CarotidClassificationService-SetDetectMode error:{ex}");
  164. }
  165. }
  166. public void SetIsIncludeContour(bool isIncludeContour)
  167. {
  168. try
  169. {
  170. if (_carotidClassification != null)
  171. {
  172. _carotidClassification.IsIncludeContour = isIncludeContour;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. Logger.Error($"CarotidClassificationService-SetIsIncludeContour error:{ex}");
  178. }
  179. }
  180. /// <summary>
  181. /// 检测单张Byte Image
  182. /// </summary>
  183. /// <returns></returns>
  184. public AICarotidArterySectionClassificationResults DetectOneByteImage()
  185. {
  186. try
  187. {
  188. byte[] byteImage = null;
  189. int retryCount = 0;
  190. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  191. {
  192. Thread.Sleep(10);
  193. retryCount++;
  194. }
  195. if (byteImage == null)
  196. {
  197. Logger.Error($"CarotidClassificationService-DetectOneByteImage Get Image Cache Fail");
  198. return null;
  199. }
  200. var carotidArterySectionClassificationResults = _carotidClassification.DetectOneImage(byteImage);
  201. return AIConvertHelper.ConvertCarotidArterySectionClassificationResultsToAICarotidArterySectionClassificationResults(carotidArterySectionClassificationResults);
  202. }
  203. catch (Exception ex)
  204. {
  205. Logger.Error($"CarotidClassificationService-DetectOneByteImage error:{ex}");
  206. return null;
  207. }
  208. }
  209. public AICarotidArterySectionClassificationResults DetectOneRawImage(int height, int width, AIEnumColorType colorType)
  210. {
  211. try
  212. {
  213. byte[] byteImage = null;
  214. int retryCount = 0;
  215. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  216. {
  217. Thread.Sleep(10);
  218. retryCount++;
  219. }
  220. if (byteImage == null)
  221. {
  222. Logger.Error($"CarotidClassificationService-DetectOneRawImage Get Image Cache Fail");
  223. return null;
  224. }
  225. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  226. var carotidArterySectionClassificationResults = _carotidClassification.DetectOneImage(rawImage);
  227. return AIConvertHelper.ConvertCarotidArterySectionClassificationResultsToAICarotidArterySectionClassificationResults(carotidArterySectionClassificationResults);
  228. }
  229. catch (Exception ex)
  230. {
  231. Logger.Error($"CarotidClassificationService-DetectOneRawImage error:{ex}");
  232. return null;
  233. }
  234. }
  235. /// <summary>
  236. /// Send Raw Image Data For Pipe
  237. /// </summary>
  238. /// <param name="height"></param>
  239. /// <param name="width"></param>
  240. /// <param name="colorType"></param>
  241. public void SendRawImageData(int height, int width, AIEnumColorType colorType)
  242. {
  243. try
  244. {
  245. _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
  246. }
  247. catch (Exception ex)
  248. {
  249. Logger.Error($"CarotidClassificationService-SendRawImageData error:{ex}");
  250. }
  251. }
  252. /// <summary>
  253. /// Send Byte Image Data For Pipe
  254. /// </summary>
  255. public void SendByteImageData()
  256. {
  257. try
  258. {
  259. _imageProvider?.ReceiveByteImageData();
  260. }
  261. catch (Exception ex)
  262. {
  263. Logger.Error($"CarotidClassificationService-SendByteImageData error:{ex}");
  264. }
  265. }
  266. public void Close()
  267. {
  268. try
  269. {
  270. Logger.Info($"CarotidClassificationService-Close Invoke");
  271. CloseDiagnosis();
  272. }
  273. catch (Exception ex)
  274. {
  275. Logger.Error($"CarotidClassificationService-Close error:{ex}");
  276. }
  277. }
  278. private void CloseDiagnosis()
  279. {
  280. if (_carotidClassification != null)
  281. {
  282. _carotidClassification.StartEvaluation -= OnStartEvaluation;
  283. _carotidClassification.FinishEvaluation -= OnFinishEvaluation;
  284. _carotidClassification.NotifyError -= OnNotifyError;
  285. _carotidClassification.NotifyLog -= OnNotifyLog;
  286. _carotidClassification.Close();
  287. _carotidClassification = null;
  288. }
  289. }
  290. public void Dispose()
  291. {
  292. try
  293. {
  294. if (!_disposed)
  295. {
  296. Logger.Info($"CarotidClassificationService-Start Dispose");
  297. CloseDiagnosis();
  298. if (_singleImagePipeServer != null)
  299. {
  300. _singleImagePipeServer.DataReceived -= OnDataReceived;
  301. _singleImagePipeServer.Dispose();
  302. _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
  303. _singleImagePipeServer = null;
  304. }
  305. _imageProvider.Dispose();
  306. _imageProvider = null;
  307. _disposed = true;
  308. Logger.Info($"CarotidClassificationService Dispose End");
  309. }
  310. }
  311. catch (Exception ex)
  312. {
  313. Logger.Error($"CarotidClassificationService-Dispose error:{ex}");
  314. }
  315. }
  316. private void OnStartEvaluation(object sender, EventArgs e)
  317. {
  318. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.CarotidClassificationStartEvaluationRaised, Params = e });
  319. }
  320. private void OnFinishEvaluation(object sender, CarotidArterySectionClassificationResults e)
  321. {
  322. var aiCarotidArterySectionClassificationResults = AIConvertHelper.ConvertCarotidArterySectionClassificationResultsToAICarotidArterySectionClassificationResults(e);
  323. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.CarotidClassificationFinishEvaluationRaised, Params = aiCarotidArterySectionClassificationResults });
  324. }
  325. private void OnNotifyLog(object sender, LogEventArgs e)
  326. {
  327. if (e == null)
  328. {
  329. return;
  330. }
  331. switch (e.LogType)
  332. {
  333. case EnumLogType.InfoLog:
  334. Logger.Info($"CarotidClassificationService OnNotifyLog:{e.Msg}");
  335. break;
  336. case EnumLogType.WarnLog:
  337. Logger.Warning($"CarotidClassificationService OnNotifyLog:{e.Msg}");
  338. break;
  339. case EnumLogType.ErrorLog:
  340. case EnumLogType.FatalLog:
  341. default:
  342. Logger.Error($"CarotidClassificationService OnNotifyLog:{e.Msg}");
  343. break;
  344. }
  345. var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
  346. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.CarotidClassificationNotifyLogRaised, Params = logEventArgs });
  347. }
  348. private void OnNotifyError(object sender, ErrorEventArgs e)
  349. {
  350. Logger.Error($"CarotidClassificationService OnNotifyError:{e.GetException()}");
  351. var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
  352. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.CarotidClassificationNotifyLogRaised, Params = logEventArgs });
  353. }
  354. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  355. {
  356. if (e == null)
  357. {
  358. return;
  359. }
  360. switch (e.LogType)
  361. {
  362. case AIEnumLogType.ErrorLog:
  363. case AIEnumLogType.FatalLog:
  364. Logger.Error(e.Msg);
  365. break;
  366. case AIEnumLogType.WarnLog:
  367. Logger.Warning(e.Msg);
  368. break;
  369. case AIEnumLogType.InfoLog:
  370. default:
  371. Logger.Info(e.Msg);
  372. break;
  373. }
  374. }
  375. private void OnDataReceived(object sender, byte[] e)
  376. {
  377. try
  378. {
  379. _singleImageCache.Push(e);
  380. }
  381. catch (Exception ex)
  382. {
  383. Logger.Error($"CarotidClassificationService-OnDataReceived error:{ex}");
  384. }
  385. }
  386. }
  387. }