ThyroidClassificationService.cs 15 KB

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