VetHeartDiagnosis.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text.Json;
  5. using Vinno.AI.CommonSDK.Enums;
  6. using Vinno.AI.CommonSDK.Interfaces;
  7. using Vinno.AI.CommonSDK.Models;
  8. using Vinno.AI.CommonSDK.Models.Data;
  9. using Vinno.AI.CommonSDK.Tools;
  10. using Vinno.AI.CustomizeDiagnosisSDK.Interfaces;
  11. using Vinno.AI.VetHeartDiagnosisSDK.Enums;
  12. using Vinno.AI.VetHeartDiagnosisSDK.Interfaces;
  13. using Vinno.AI.VetHeartDiagnosisSDK.Models;
  14. using Vinno.AI.VetHeartDiagnosisSDK.Tools;
  15. namespace Vinno.AI.VetHeartDiagnosisSDK
  16. {
  17. public class VetHeartDiagnosis : IVetHeartDiagnosis
  18. {
  19. private readonly IVetHeartDiagnosisService _vetHeartDiagnosisService;
  20. private IAIImageProviderV2 _imageProvider;
  21. private Dictionary<AIEnumVetHeartEngineType, IAIVetHeartEngine> _aiEngineDict = new Dictionary<AIEnumVetHeartEngineType, IAIVetHeartEngine>();
  22. private bool _initialized;
  23. private bool _disposed;
  24. /// <summary>
  25. /// Used For Detect One Image
  26. /// </summary>
  27. private PipeClient _singleImagePipeClient;
  28. /// <summary>
  29. /// Used For Provide Image
  30. /// </summary>
  31. private PipeClient _imageProviderPipeClient;
  32. /// <summary>
  33. /// Raised when the image evaluation is started.
  34. /// </summary>
  35. public event EventHandler StartEvaluationNotification;
  36. /// <summary>
  37. /// Raised when the image evaluation is finished.
  38. /// </summary>
  39. public event EventHandler<IReadOnlyDictionary<AIImageDataId, IReadOnlyList<IAIResultData>>> FinishEvaluationNotification;
  40. public VetHeartDiagnosis()
  41. {
  42. _vetHeartDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IVetHeartDiagnosisService>();
  43. if (AIManager.Instance.AINotificationManager != null)
  44. {
  45. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  46. }
  47. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForVetHeartDiagnosisSingleImage);
  48. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  49. _singleImagePipeClient.Start();
  50. _imageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForVetHeartDiagnosisRawImageProvider);
  51. _imageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  52. _imageProviderPipeClient.Start();
  53. }
  54. /// <summary>
  55. /// 初始化VetHeartMLine Diagnosis
  56. /// </summary>
  57. /// <param name="vetHeartMLineDiagnosisParameter">VetHeartMLine Diagnosis Parameter</param>
  58. /// <param name="imageProvider">Image Provider</param>
  59. public void Initialize(VetHeartDiagnosisParameter vetHeartMLineDiagnosisParameter, IAIImageProviderV2 imageProvider = null)
  60. {
  61. try
  62. {
  63. if (!_initialized)
  64. {
  65. _imageProvider = imageProvider;
  66. _vetHeartDiagnosisService.Initialize(vetHeartMLineDiagnosisParameter, _imageProvider != null);
  67. foreach (var aiEngine in _aiEngineDict.Values)
  68. {
  69. aiEngine?.Dispose();
  70. }
  71. _aiEngineDict.Clear();
  72. foreach (var value in Enum.GetValues(typeof(AIEnumVetHeartEngineType)))
  73. {
  74. var type = (AIEnumVetHeartEngineType)value;
  75. var aiEngine = new AIVetHeartEngine(_vetHeartDiagnosisService, type, _singleImagePipeClient);
  76. _aiEngineDict[type] = aiEngine;
  77. }
  78. _initialized = true;
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  84. }
  85. }
  86. public IAIVetHeartEngine GetAIEngine(AIEnumVetHeartEngineType engineType)
  87. {
  88. try
  89. {
  90. if (_aiEngineDict.ContainsKey(engineType))
  91. {
  92. var aiEngine = _aiEngineDict[engineType];
  93. if (_imageProvider != null)
  94. {
  95. _vetHeartDiagnosisService.SetCurrentEngine(engineType);
  96. }
  97. return aiEngine;
  98. }
  99. return null;
  100. }
  101. catch (Exception ex)
  102. {
  103. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  104. return null;
  105. }
  106. }
  107. /// <summary>
  108. /// Start Image Provider
  109. /// </summary>
  110. public void Start()
  111. {
  112. if (_imageProvider != null)
  113. {
  114. _vetHeartDiagnosisService.Start();
  115. _imageProvider.ImageProvided += OnImageProvided;
  116. _imageProvider.Start();
  117. }
  118. }
  119. /// <summary>
  120. /// Stop Image Provider
  121. /// </summary>
  122. public void Stop()
  123. {
  124. if (_imageProvider != null)
  125. {
  126. _imageProvider.ImageProvided -= OnImageProvided;
  127. _imageProvider.Stop();
  128. }
  129. _vetHeartDiagnosisService.Stop();
  130. }
  131. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  132. {
  133. AIManager.Instance.AILogManager?.WriteLogInfo(e);
  134. }
  135. private void OnNotificationReceived(object sender, AINotificationArgs e)
  136. {
  137. switch (e.NotificationType)
  138. {
  139. case AIEnumNotificationType.VetHeartMLineDiagnosisStartEvaluationRaised:
  140. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  141. break;
  142. case AIEnumNotificationType.VetHeartMLineDiagnosisFinishEvaluationRaised:
  143. var transResultList = JsonSerializer.Deserialize<List<TransAIResultInfo>>(e.Params?.ToString());
  144. var result = AIConvertHelper.ConvertTransResultListToAIResultDict(transResultList);
  145. FinishEvaluationNotification?.Invoke(this, result);
  146. break;
  147. case AIEnumNotificationType.VetHeartMLineDiagnosisNotifyLogRaised:
  148. var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
  149. AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
  150. break;
  151. }
  152. }
  153. /// <summary>
  154. /// 设置每秒图片吞吐量
  155. /// </summary>
  156. /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
  157. public void SetDetectTps(int detectTps)
  158. {
  159. if (detectTps <= 0)
  160. {
  161. throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
  162. }
  163. try
  164. {
  165. _vetHeartDiagnosisService.SetDetectTps(detectTps);
  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="intervalTime"></param>
  176. public void SetIntervalTime(int intervalTime)
  177. {
  178. try
  179. {
  180. _vetHeartDiagnosisService.SetIntervalTime(intervalTime);
  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. /// 设置检测模式
  189. /// </summary>
  190. /// <param name="detectMode"></param>
  191. public void SetDetectMode(AIEnumDetectMode detectMode)
  192. {
  193. try
  194. {
  195. _vetHeartDiagnosisService.SetDetectMode(detectMode);
  196. }
  197. catch (Exception ex)
  198. {
  199. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  200. }
  201. }
  202. private void OnImageProvided(object sender, AIImageInputData inputData)
  203. {
  204. try
  205. {
  206. if (inputData == null)
  207. {
  208. return;
  209. }
  210. if (inputData.Image == null)
  211. {
  212. throw new ArgumentNullException(nameof(inputData.Image));
  213. }
  214. if (inputData.ImageDataId == null)
  215. {
  216. throw new ArgumentNullException(nameof(inputData.ImageDataId));
  217. }
  218. if (inputData.UltrasoundImageRegion != null || inputData.UltrasoundImageRegion is AIConvexArrayUltrasoundImageRegion || inputData.UltrasoundImageRegion is AILinearArrayUltrasoundImageRegion)
  219. {
  220. throw new ArgumentOutOfRangeException(nameof(inputData.UltrasoundImageRegion));
  221. }
  222. _imageProviderPipeClient?.SendBytes(inputData.Image.DataBuffer);
  223. var ultrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(inputData.UltrasoundImageRegion);
  224. _vetHeartDiagnosisService.SendImageData(inputData.ImageDataId, inputData.ImageExtraInfo, ultrasoundImageRegion, inputData.Image.Height, inputData.Image.Width, inputData.Image.ColorType);
  225. }
  226. catch (Exception ex)
  227. {
  228. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  229. }
  230. }
  231. /// <summary>
  232. /// Close VetHeartMLine Diagnosis
  233. /// </summary>
  234. public void Close()
  235. {
  236. try
  237. {
  238. if (_disposed)
  239. {
  240. return;
  241. }
  242. _initialized = false;
  243. if (_imageProvider != null)
  244. {
  245. _imageProvider.ImageProvided -= OnImageProvided;
  246. _imageProvider.Stop();
  247. _imageProvider = null;
  248. }
  249. _vetHeartDiagnosisService.Close();
  250. foreach (var aiEngine in _aiEngineDict.Values)
  251. {
  252. aiEngine?.Dispose();
  253. }
  254. _aiEngineDict.Clear();
  255. }
  256. catch (Exception ex)
  257. {
  258. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  259. }
  260. }
  261. public void Dispose()
  262. {
  263. try
  264. {
  265. if (!_disposed)
  266. {
  267. _initialized = false;
  268. if (AIManager.Instance.AINotificationManager != null)
  269. {
  270. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  271. }
  272. if (_imageProvider != null)
  273. {
  274. _imageProvider.ImageProvided -= OnImageProvided;
  275. _imageProvider.Stop();
  276. _imageProvider = null;
  277. }
  278. if (_singleImagePipeClient != null)
  279. {
  280. _singleImagePipeClient.Dispose();
  281. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  282. _singleImagePipeClient = null;
  283. }
  284. if (_imageProviderPipeClient != null)
  285. {
  286. _imageProviderPipeClient.Dispose();
  287. _imageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  288. _imageProviderPipeClient = null;
  289. }
  290. foreach (var aiEngine in _aiEngineDict.Values)
  291. {
  292. aiEngine?.Dispose();
  293. }
  294. _aiEngineDict.Clear();
  295. _disposed = true;
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  301. }
  302. }
  303. }
  304. }