OrganIdentification.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.OrganIdentificationSDK.Interfaces;
  9. using Vinno.AI.OrganIdentificationSDK.Models;
  10. namespace Vinno.AI.OrganIdentificationSDK
  11. {
  12. public class OrganIdentification : IOrganIdentification
  13. {
  14. private readonly IOrganIdentificationService _organIndentifcationService;
  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<AIDiagResultPerImg> FinishEvaluationNotification;
  38. public OrganIdentification()
  39. {
  40. _organIndentifcationService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IOrganIdentificationService>();
  41. if (AIManager.Instance.AINotificationManager != null)
  42. {
  43. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  44. }
  45. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForOrganIdentificationSingleImage);
  46. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeClient.Start();
  48. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForOrganIdentificationRawImageProvider);
  49. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  50. _rawImageProviderPipeClient.Start();
  51. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForOrganIdentificationByteImageProvider);
  52. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  53. _byteImageProviderPipeClient.Start();
  54. }
  55. /// <summary>
  56. /// 初始化AI Diagnosis
  57. /// </summary>
  58. /// <param name="aiDiagnosisParameter">AI Diagnosis Parameter</param>
  59. /// <param name="imageProvider">Image Provider</param>
  60. public void Initialize(OrganIdentificationParameter organIndentificationParameter, IAIImageProvider imageProvider = null)
  61. {
  62. try
  63. {
  64. if (!_initialized)
  65. {
  66. _imageProvider = imageProvider;
  67. _organIndentifcationService.Initialize(organIndentificationParameter, _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. _imageProvider.ByteImageProvided += OnByteImageProvided;
  84. _imageProvider.RawImageProvided += OnRawImageProvided;
  85. _imageProvider.Start();
  86. _organIndentifcationService.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. _organIndentifcationService.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.OrganIndentificationStartEvaluationRaised:
  111. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  112. break;
  113. case AIEnumNotificationType.OrganIndentificationFinishEvaluationRaised:
  114. var transAIDiagResultPerImg = JsonSerializer.Deserialize<TransAIDiagResultPerImg>(e.Params.ToString());
  115. var aiDiagResultPerImg = AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(transAIDiagResultPerImg);
  116. FinishEvaluationNotification?.Invoke(this, aiDiagResultPerImg);
  117. break;
  118. case AIEnumNotificationType.OrganIndentificationNotifyLogRaised:
  119. var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
  120. AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
  121. break;
  122. }
  123. }
  124. /// <summary>
  125. /// 设置图片是否已裁剪
  126. /// </summary>
  127. /// <param name="isCropped"></param>
  128. public void SetIsCropped(bool isCropped)
  129. {
  130. try
  131. {
  132. _organIndentifcationService.SetIsCropped(isCropped);
  133. }
  134. catch (Exception ex)
  135. {
  136. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  137. }
  138. }
  139. /// <summary>
  140. /// 设置每秒图片吞吐量
  141. /// </summary>
  142. /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
  143. public void SetDetectTps(int detectTps)
  144. {
  145. if (detectTps <= 0)
  146. {
  147. throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
  148. }
  149. try
  150. {
  151. _organIndentifcationService.SetDetectTps(detectTps);
  152. }
  153. catch (Exception ex)
  154. {
  155. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  156. }
  157. }
  158. /// <summary>
  159. /// 设置间隔时间
  160. /// </summary>
  161. /// <param name="intervalTime"></param>
  162. public void SetIntervalTime(int intervalTime)
  163. {
  164. try
  165. {
  166. _organIndentifcationService.SetIntervalTime(intervalTime);
  167. }
  168. catch (Exception ex)
  169. {
  170. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  171. }
  172. }
  173. /// <summary>
  174. /// 设置检测模式
  175. /// </summary>
  176. /// <param name="detectMode"></param>
  177. public void SetDetectMode(AIEnumDetectMode detectMode)
  178. {
  179. try
  180. {
  181. _organIndentifcationService.SetDetectMode(detectMode);
  182. }
  183. catch (Exception ex)
  184. {
  185. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  186. }
  187. }
  188. /// <summary>
  189. /// 检测单张Byte Image
  190. /// </summary>
  191. /// <param name="byteImage"></param>
  192. /// <returns></returns>
  193. public AIDiagResultPerImg DetectOneImage(byte[] byteImage)
  194. {
  195. if (byteImage == null)
  196. {
  197. throw new ArgumentNullException(nameof(byteImage));
  198. }
  199. try
  200. {
  201. _singleImagePipeClient.SendBytes(byteImage);
  202. var result = _organIndentifcationService.DetectOneByteImage();
  203. return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
  204. }
  205. catch (Exception ex)
  206. {
  207. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  208. return null;
  209. }
  210. }
  211. /// <summary>
  212. /// 检测单张Raw Image
  213. /// </summary>
  214. /// <param name="rawImage"></param>
  215. /// <returns></returns>
  216. public AIDiagResultPerImg DetectOneImage(AIRawImage rawImage)
  217. {
  218. if (rawImage == null)
  219. {
  220. throw new ArgumentNullException(nameof(rawImage));
  221. }
  222. try
  223. {
  224. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  225. var result = _organIndentifcationService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType);
  226. return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
  227. }
  228. catch (Exception ex)
  229. {
  230. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  231. return null;
  232. }
  233. }
  234. private void OnByteImageProvided(object sender, byte[] byteImage)
  235. {
  236. try
  237. {
  238. if (byteImage == null)
  239. {
  240. return;
  241. }
  242. _byteImageProviderPipeClient?.SendBytes(byteImage);
  243. _organIndentifcationService.SendByteImageData();
  244. }
  245. catch (Exception ex)
  246. {
  247. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  248. }
  249. }
  250. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  251. {
  252. try
  253. {
  254. if (rawImage == null)
  255. {
  256. return;
  257. }
  258. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  259. _organIndentifcationService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  260. }
  261. catch (Exception ex)
  262. {
  263. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  264. }
  265. }
  266. /// <summary>
  267. /// 清除脏器识别的缓存
  268. /// </summary>
  269. public void ClearOrganIdentificationCache()
  270. {
  271. try
  272. {
  273. _organIndentifcationService.ClearOrganIdentificationCache();
  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. /// <summary>
  281. /// Close AI Diagnosis
  282. /// </summary>
  283. public void Close()
  284. {
  285. try
  286. {
  287. if (_disposed)
  288. {
  289. return;
  290. }
  291. _initialized = false;
  292. if (_imageProvider != null)
  293. {
  294. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  295. _imageProvider.RawImageProvided -= OnRawImageProvided;
  296. _imageProvider.Stop();
  297. _imageProvider = null;
  298. }
  299. _organIndentifcationService.Close();
  300. }
  301. catch (Exception ex)
  302. {
  303. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  304. }
  305. }
  306. public void Dispose()
  307. {
  308. try
  309. {
  310. if (!_disposed)
  311. {
  312. _initialized = false;
  313. if (AIManager.Instance.AINotificationManager != null)
  314. {
  315. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  316. }
  317. if (_imageProvider != null)
  318. {
  319. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  320. _imageProvider.RawImageProvided -= OnRawImageProvided;
  321. _imageProvider.Stop();
  322. _imageProvider = null;
  323. }
  324. if (_singleImagePipeClient != null)
  325. {
  326. _singleImagePipeClient.Dispose();
  327. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  328. _singleImagePipeClient = null;
  329. }
  330. if (_rawImageProviderPipeClient != null)
  331. {
  332. _rawImageProviderPipeClient.Dispose();
  333. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  334. _rawImageProviderPipeClient = null;
  335. }
  336. if (_byteImageProviderPipeClient != null)
  337. {
  338. _byteImageProviderPipeClient.Dispose();
  339. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  340. _byteImageProviderPipeClient = null;
  341. }
  342. _disposed = true;
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  348. }
  349. }
  350. }
  351. }