CarotidClassification.cs 14 KB

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