ThyroidClassification.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. using Vinno.AI.CommonSDK.Enums;
  5. using Vinno.AI.CommonSDK.Interfaces;
  6. using Vinno.AI.CommonSDK.Models;
  7. using Vinno.AI.CommonSDK.Tools;
  8. using Vinno.AI.ThyroidClassificationSDK.Interfaces;
  9. using Vinno.AI.ThyroidClassificationSDK.Models;
  10. namespace Vinno.AI.ThyroidClassificationSDK
  11. {
  12. public class ThyroidClassification : IThyroidClassification
  13. {
  14. private readonly IThyroidClassificationService _thyroidClassificationService;
  15. private IAIImageProvider _imageProvider;
  16. private bool _initialized;
  17. private bool _disposed;
  18. /// <summary>
  19. /// Used For Detect One Image
  20. /// </summary>
  21. private PipeClient _singleImagePipeClient;
  22. /// <summary>
  23. /// Used For Provide Raw Image
  24. /// </summary>
  25. private PipeClient _rawImageProviderPipeClient;
  26. /// <summary>
  27. /// Used For Provide Byte Image
  28. /// </summary>
  29. private PipeClient _byteImageProviderPipeClient;
  30. /// <summary>
  31. /// Raised when the image evaluation is started.
  32. /// </summary>
  33. public event EventHandler StartEvaluationNotification;
  34. /// <summary>
  35. /// Raised when the image evaluation is finished.
  36. /// </summary>
  37. public event EventHandler<AIThyroidSectionResult> FinishEvaluationNotification;
  38. public ThyroidClassification()
  39. {
  40. _thyroidClassificationService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IThyroidClassificationService>();
  41. if (AIManager.Instance.AINotificationManager != null)
  42. {
  43. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  44. }
  45. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForThyroidClassificationSingleImage);
  46. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeClient.Start();
  48. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForThyroidClassificationRawImageProvider);
  49. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  50. _rawImageProviderPipeClient.Start();
  51. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForThyroidClassificationByteImageProvider);
  52. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  53. _byteImageProviderPipeClient.Start();
  54. }
  55. /// <summary>
  56. /// Start Image Provider
  57. /// </summary>
  58. public void Start()
  59. {
  60. if (_imageProvider != null)
  61. {
  62. _thyroidClassificationService.Start();
  63. _imageProvider.ByteImageProvided += OnByteImageProvided;
  64. _imageProvider.RawImageProvided += OnRawImageProvided;
  65. _imageProvider.Start();
  66. }
  67. }
  68. /// <summary>
  69. /// Stop Image Provider
  70. /// </summary>
  71. public void Stop()
  72. {
  73. if (_imageProvider != null)
  74. {
  75. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  76. _imageProvider.RawImageProvided -= OnRawImageProvided;
  77. _imageProvider.Stop();
  78. }
  79. _thyroidClassificationService.Stop();
  80. }
  81. /// <summary>
  82. /// 检测单张Raw Image
  83. /// </summary>
  84. /// <param name="rawImage">图像资源</param>
  85. /// <returns></returns>
  86. public AIThyroidSectionResult DetectOneImage(AIRawImage rawImage)
  87. {
  88. if (rawImage == null)
  89. {
  90. throw new ArgumentNullException(nameof(rawImage));
  91. }
  92. try
  93. {
  94. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  95. return _thyroidClassificationService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType);
  96. }
  97. catch (Exception ex)
  98. {
  99. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  100. return null;
  101. }
  102. }
  103. /// <summary>
  104. /// 检测单张Byte Image
  105. /// </summary>
  106. /// <param name="byteImage">图像资源</param>
  107. /// <returns></returns>
  108. public AIThyroidSectionResult DetectOneImage(byte[] byteImage)
  109. {
  110. if (byteImage == null)
  111. {
  112. throw new ArgumentNullException(nameof(byteImage));
  113. }
  114. try
  115. {
  116. _singleImagePipeClient.SendBytes(byteImage);
  117. return _thyroidClassificationService.DetectOneByteImage();
  118. }
  119. catch (Exception ex)
  120. {
  121. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  122. return null;
  123. }
  124. }
  125. /// <summary>
  126. /// 设置每秒图片吞吐量
  127. /// </summary>
  128. /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
  129. public void SetDetectTps(int detectTps)
  130. {
  131. if (detectTps <= 0)
  132. {
  133. throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
  134. }
  135. try
  136. {
  137. _thyroidClassificationService.SetDetectTps(detectTps);
  138. }
  139. catch (Exception ex)
  140. {
  141. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  142. }
  143. }
  144. /// <summary>
  145. /// 设置间隔时间
  146. /// </summary>
  147. /// <param name="intervalTime"></param>
  148. public void SetIntervalTime(int intervalTime)
  149. {
  150. try
  151. {
  152. _thyroidClassificationService.SetIntervalTime(intervalTime);
  153. }
  154. catch (Exception ex)
  155. {
  156. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  157. }
  158. }
  159. /// <summary>
  160. /// 设置检测模式
  161. /// </summary>
  162. /// <param name="detectMode"></param>
  163. public void SetDetectMode(AIEnumDetectMode detectMode)
  164. {
  165. try
  166. {
  167. _thyroidClassificationService.SetDetectMode(detectMode);
  168. }
  169. catch (Exception ex)
  170. {
  171. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  172. }
  173. }
  174. /// <summary>
  175. /// 设置检测结果是否包含轮廓信息
  176. /// </summary>
  177. /// <param name="isIncludeContour"></param>
  178. public void SetIsIncludeContour(bool isIncludeContour)
  179. {
  180. try
  181. {
  182. _thyroidClassificationService.SetIsIncludeContour(isIncludeContour);
  183. }
  184. catch (Exception ex)
  185. {
  186. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  187. }
  188. }
  189. /// <summary>
  190. /// Close ThyroidClassifiationService
  191. /// </summary>
  192. public void Close()
  193. {
  194. try
  195. {
  196. if (_disposed)
  197. {
  198. return;
  199. }
  200. _initialized = false;
  201. if (_imageProvider != null)
  202. {
  203. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  204. _imageProvider.RawImageProvided -= OnRawImageProvided;
  205. _imageProvider.Stop();
  206. _imageProvider = null;
  207. }
  208. _thyroidClassificationService.Close();
  209. }
  210. catch (Exception ex)
  211. {
  212. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  213. }
  214. }
  215. public void Dispose()
  216. {
  217. try
  218. {
  219. if (!_disposed)
  220. {
  221. _initialized = false;
  222. if (AIManager.Instance.AINotificationManager != null)
  223. {
  224. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  225. }
  226. if (_imageProvider != null)
  227. {
  228. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  229. _imageProvider.RawImageProvided -= OnRawImageProvided;
  230. _imageProvider.Stop();
  231. _imageProvider = null;
  232. }
  233. if (_singleImagePipeClient != null)
  234. {
  235. _singleImagePipeClient.Dispose();
  236. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  237. _singleImagePipeClient = null;
  238. }
  239. if (_rawImageProviderPipeClient != null)
  240. {
  241. _rawImageProviderPipeClient.Dispose();
  242. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  243. _rawImageProviderPipeClient = null;
  244. }
  245. if (_byteImageProviderPipeClient != null)
  246. {
  247. _byteImageProviderPipeClient.Dispose();
  248. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  249. _byteImageProviderPipeClient = null;
  250. }
  251. _disposed = true;
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  257. }
  258. }
  259. /// <summary>
  260. /// 初始化ThyroidClassification
  261. /// </summary>
  262. /// <param name="thyroidClassificationParameter">ThyroidClassificationParameter</param>
  263. /// <param name="imageProvider">Image Provider</param>
  264. public void Initialize(ThyroidClassificationParameter thyroidClassificationParameter, IAIImageProvider imageProvider = null)
  265. {
  266. try
  267. {
  268. if (!_initialized)
  269. {
  270. _imageProvider = imageProvider;
  271. _thyroidClassificationService.Initialize(thyroidClassificationParameter, _imageProvider != null);
  272. _initialized = true;
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  278. }
  279. }
  280. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  281. {
  282. AIManager.Instance.AILogManager?.WriteLogInfo(e);
  283. }
  284. private void OnNotificationReceived(object sender, AINotificationArgs e)
  285. {
  286. switch (e.NotificationType)
  287. {
  288. case AIEnumNotificationType.ThyroidClassifiationStartEvaluationRaised:
  289. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  290. break;
  291. case AIEnumNotificationType.ThyroidClassifiationFinishEvaluationRaised:
  292. var aiThyroidSectionResult = JsonSerializer.Deserialize<AIThyroidSectionResult>(e.Params?.ToString());
  293. FinishEvaluationNotification?.Invoke(this, aiThyroidSectionResult);
  294. break;
  295. case AIEnumNotificationType.ThyroidClassifiationNotifyLogRaised:
  296. var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
  297. AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
  298. break;
  299. }
  300. }
  301. private void OnByteImageProvided(object sender, byte[] byteImage)
  302. {
  303. try
  304. {
  305. if (byteImage == null)
  306. {
  307. return;
  308. }
  309. _byteImageProviderPipeClient?.SendBytes(byteImage);
  310. _thyroidClassificationService.SendByteImageData();
  311. }
  312. catch (Exception ex)
  313. {
  314. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  315. }
  316. }
  317. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  318. {
  319. try
  320. {
  321. if (rawImage == null)
  322. {
  323. return;
  324. }
  325. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  326. _thyroidClassificationService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  327. }
  328. catch (Exception ex)
  329. {
  330. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  331. }
  332. }
  333. }
  334. }