AutoEFDiagnosis.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text.Json;
  6. using Vinno.AI.AutoEFDiagnosisSDK.Interfaces;
  7. using Vinno.AI.AutoEFDiagnosisSDK.Models;
  8. using Vinno.AI.AutoEFDiagnosisSDK.Tools;
  9. using Vinno.AI.CommonSDK.Enums;
  10. using Vinno.AI.CommonSDK.Interfaces;
  11. using Vinno.AI.CommonSDK.Models;
  12. using Vinno.AI.CommonSDK.Tools;
  13. namespace Vinno.AI.AutoEFDiagnosisSDK
  14. {
  15. public class AutoEFDiagnosis : IAutoEFDiagnosis
  16. {
  17. private readonly IAutoEFDiagnosisService _autoEFDiagnosisService;
  18. private Dictionary<string, Dictionary<double, AILVVolumeCalcResult>> _resultDic = new Dictionary<string, Dictionary<double, AILVVolumeCalcResult>>();
  19. private IAIImageProvider _imageProvider;
  20. private bool _initialized;
  21. private bool _disposed;
  22. /// <summary>
  23. /// Used For Detect One Image
  24. /// </summary>
  25. private PipeClient _singleImagePipeClient;
  26. /// <summary>
  27. /// Used For Provide Raw Image
  28. /// </summary>
  29. private PipeClient _rawImageProviderPipeClient;
  30. /// <summary>
  31. /// Used For Provide Byte Image
  32. /// </summary>
  33. private PipeClient _byteImageProviderPipeClient;
  34. /// <summary>
  35. /// Raised when the image evaluation is started.
  36. /// </summary>
  37. public event EventHandler StartEvaluationNotification;
  38. /// <summary>
  39. /// Raised when the image evaluation is finished.
  40. /// </summary>
  41. public event EventHandler<AICardiacCurveInfos> FinishEvaluationNotification;
  42. public AutoEFDiagnosis()
  43. {
  44. _autoEFDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAutoEFDiagnosisService>();
  45. if (AIManager.Instance.AINotificationManager != null)
  46. {
  47. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  48. }
  49. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoEFDiagnosisSingleImage);
  50. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  51. _singleImagePipeClient.Start();
  52. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoEFDiagnosisRawImageProvider);
  53. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  54. _rawImageProviderPipeClient.Start();
  55. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoEFDiagnosisByteImageProvider);
  56. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  57. _byteImageProviderPipeClient.Start();
  58. }
  59. /// <summary>
  60. /// 初始化AutoEF Diagnosis
  61. /// </summary>
  62. /// <param name="autoEFDiagnosisParameter">AI Diagnosis Parameter</param>
  63. public void Initialize(AutoEFDiagnosisParameter autoEFDiagnosisParameter, IAIImageProvider imageProvider = null)
  64. {
  65. try
  66. {
  67. if (!_initialized)
  68. {
  69. _imageProvider = imageProvider;
  70. _autoEFDiagnosisService.Initialize(autoEFDiagnosisParameter, _imageProvider != null);
  71. _initialized = true;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  77. }
  78. }
  79. /// <summary>
  80. /// Start Image Provider
  81. /// </summary>
  82. public void Start()
  83. {
  84. if (_imageProvider != null)
  85. {
  86. _autoEFDiagnosisService.Start();
  87. _imageProvider.ByteImageProvided += OnByteImageProvided;
  88. _imageProvider.RawImageProvided += OnRawImageProvided;
  89. _imageProvider.Start();
  90. }
  91. }
  92. /// <summary>
  93. /// Stop Image Provider
  94. /// </summary>
  95. public void Stop()
  96. {
  97. if (_imageProvider != null)
  98. {
  99. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  100. _imageProvider.RawImageProvided -= OnRawImageProvided;
  101. _imageProvider.Stop();
  102. }
  103. _autoEFDiagnosisService.Stop();
  104. }
  105. private void OnNotificationReceived(object sender, AINotificationArgs e)
  106. {
  107. switch (e.NotificationType)
  108. {
  109. case AIEnumNotificationType.AutoEFDiagnosisStartEvaluationRaised:
  110. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  111. break;
  112. case AIEnumNotificationType.AutoEFDiagnosisFinishEvaluationRaised:
  113. var transAICardiacCurveInfos = JsonSerializer.Deserialize<TransAICardiacCurveInfos>(e.Params?.ToString());
  114. var result = GetFinalResult(transAICardiacCurveInfos);
  115. FinishEvaluationNotification?.Invoke(this, result);
  116. break;
  117. case AIEnumNotificationType.AutoEFDiagnosisNotifyLogRaised:
  118. var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
  119. AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
  120. break;
  121. }
  122. }
  123. #region 兽用
  124. /// <summary>
  125. /// 启用AutoEF(仅兽用)
  126. /// </summary>
  127. /// <param name="totalFrameCount">整个视频帧数</param>
  128. /// <param name="cmPerPixel">探头中像素与实际单位转换的参数</param>
  129. public void StartAutotEFCalculation(long totalFrameCount, double cmPerPixel)
  130. {
  131. try
  132. {
  133. _autoEFDiagnosisService.StartAutotEFCalculationForVet(totalFrameCount, cmPerPixel);
  134. }
  135. catch (Exception ex)
  136. {
  137. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  138. }
  139. }
  140. /// <summary>
  141. /// 推入一张裸图(仅兽用)
  142. /// </summary>
  143. /// <param name="rawImage"></param>
  144. /// <param name="frameTime"></param>
  145. /// <returns></returns>
  146. public void DetectOneImageForAutoEFCalculation(AIRawImage rawImage, double frameTime)
  147. {
  148. if (rawImage == null)
  149. {
  150. throw new ArgumentNullException(nameof(rawImage));
  151. }
  152. try
  153. {
  154. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  155. _autoEFDiagnosisService.DetectOneRawImageForAutoEFCalculationForVet(rawImage.Height, rawImage.Width, rawImage.ColorType, frameTime);
  156. }
  157. catch (Exception ex)
  158. {
  159. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  160. }
  161. }
  162. /// <summary>
  163. /// 推入一张图(仅兽用)
  164. /// </summary>
  165. /// <param name="byteImage"></param>
  166. /// <param name="frameTime"></param>
  167. /// <returns></returns>
  168. public void DetectOneImageForAutoEFCalculation(byte[] byteImage, double frameTime)
  169. {
  170. if (byteImage == null)
  171. {
  172. throw new ArgumentNullException(nameof(byteImage));
  173. }
  174. try
  175. {
  176. _singleImagePipeClient.SendBytes(byteImage);
  177. _autoEFDiagnosisService.DetectOneByteImageForAutoEFCalculationForVet(frameTime);
  178. }
  179. catch (Exception ex)
  180. {
  181. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  182. }
  183. }
  184. /// <summary>
  185. /// 停止AutoEF并返回结果,若IsReturnEveryImgResult为True,则返回所有帧的AutoEF结果,若IsReturnEveryImgResult为False,则只返回ES,ED两张图的结果(仅兽用)
  186. /// </summary>
  187. /// <returns></returns>
  188. public Dictionary<double, AIEDESDetectResult> StopAutoEFCalculation()
  189. {
  190. try
  191. {
  192. return _autoEFDiagnosisService.StopAutoEFCalculationForVet();
  193. }
  194. catch (Exception ex)
  195. {
  196. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  197. return null;
  198. }
  199. }
  200. #endregion 兽用
  201. #region 人用
  202. /// <summary>
  203. /// 启用AutoEF计算(仅支持人用)
  204. /// </summary>
  205. /// <param name="totalTime">表示要显示的AutoEF曲线的总时长</param>
  206. /// <param name="cmPerPixel">探头中像素与实际单位转换的参数</param>
  207. /// <param name="intervalTime">推理的间隔时间</param>
  208. public string StartAutotEFCalculationForHuman(double totalTime, float cmPerPixel, double intervalTime)
  209. {
  210. try
  211. {
  212. return _autoEFDiagnosisService.StartAutotEFCalculation(totalTime, cmPerPixel, intervalTime);
  213. }
  214. catch (Exception ex)
  215. {
  216. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  217. }
  218. return null;
  219. }
  220. /// <summary>
  221. /// 检测单张图像(仅支持人用)
  222. /// </summary>
  223. /// <param name="calculationId"></param>
  224. /// <param name="rawImage"></param>
  225. /// <param name="frameTime"></param>
  226. /// <returns></returns>
  227. public AICardiacCurveInfos DetectOneImageForHuman(string calculationId, AIRawImage rawImage, double frameTime)
  228. {
  229. if (rawImage == null)
  230. {
  231. throw new ArgumentNullException(nameof(rawImage));
  232. }
  233. try
  234. {
  235. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  236. var result = _autoEFDiagnosisService.DetectOneRawImage(calculationId, rawImage.Height, rawImage.Width, rawImage.ColorType, frameTime);
  237. return GetFinalResult(result);
  238. }
  239. catch (Exception ex)
  240. {
  241. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  242. return null;
  243. }
  244. }
  245. private AICardiacCurveInfos GetFinalResult(TransAICardiacCurveInfos result)
  246. {
  247. if (result == null)
  248. {
  249. return null;
  250. }
  251. Dictionary<double, AILVVolumeCalcResult> dic;
  252. if (_resultDic.ContainsKey(result.CalcultationId))
  253. {
  254. dic = _resultDic[result.CalcultationId];
  255. }
  256. else
  257. {
  258. if (_resultDic.Count > 5)
  259. {
  260. _resultDic.Remove(_resultDic.Keys.FirstOrDefault());
  261. }
  262. dic = new Dictionary<double, AILVVolumeCalcResult>();
  263. }
  264. var aiCardiacCurveInfos = AIConvertHelper.ConvertTransAICardiacCurveInfosToAIAardiacCurveInfos(result, dic);
  265. if (aiCardiacCurveInfos != null)
  266. {
  267. _resultDic[result.CalcultationId] = aiCardiacCurveInfos.ResultPerImage;
  268. }
  269. return aiCardiacCurveInfos;
  270. }
  271. /// <summary>
  272. /// 检测单张图像(仅支持人用)
  273. /// </summary>
  274. /// <param name="byteImage"></param>
  275. /// <param name="byteImage"></param>
  276. /// <param name="frameTime"></param>
  277. /// <returns></returns>
  278. public AICardiacCurveInfos DetectOneImageForHuman(string calculationId, byte[] byteImage, double frameTime)
  279. {
  280. if (byteImage == null)
  281. {
  282. throw new ArgumentNullException(nameof(byteImage));
  283. }
  284. try
  285. {
  286. _singleImagePipeClient.SendBytes(byteImage);
  287. var result = _autoEFDiagnosisService.DetectOneByteImage(calculationId, frameTime);
  288. return GetFinalResult(result);
  289. }
  290. catch (Exception ex)
  291. {
  292. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  293. return null;
  294. }
  295. }
  296. #endregion 人用
  297. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  298. {
  299. AIManager.Instance.AILogManager?.WriteLogInfo(e);
  300. }
  301. /// <summary>
  302. /// 设置每秒图片吞吐量
  303. /// </summary>
  304. /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
  305. public void SetDetectTps(int detectTps)
  306. {
  307. if (detectTps <= 0)
  308. {
  309. throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
  310. }
  311. try
  312. {
  313. _autoEFDiagnosisService.SetDetectTps(detectTps);
  314. }
  315. catch (Exception ex)
  316. {
  317. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  318. }
  319. }
  320. /// <summary>
  321. /// 设置间隔时间
  322. /// </summary>
  323. /// <param name="intervalTime"></param>
  324. public void SetIntervalTime(int intervalTime)
  325. {
  326. try
  327. {
  328. _autoEFDiagnosisService.SetIntervalTime(intervalTime);
  329. }
  330. catch (Exception ex)
  331. {
  332. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  333. }
  334. }
  335. /// <summary>
  336. /// 设置一个像素代表的实际物理距离是多少cm
  337. /// </summary>
  338. /// <param name="cmPerPixel"></param>
  339. public void SetCmPerPixel(double cmPerPixel)
  340. {
  341. try
  342. {
  343. _autoEFDiagnosisService.SetCmPerPixel(cmPerPixel);
  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. /// <summary>
  351. /// 设置检测模式
  352. /// </summary>
  353. /// <param name="detectMode"></param>
  354. public void SetDetectMode(AIEnumDetectMode detectMode)
  355. {
  356. try
  357. {
  358. _autoEFDiagnosisService.SetDetectMode(detectMode);
  359. }
  360. catch (Exception ex)
  361. {
  362. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  363. }
  364. }
  365. private void OnByteImageProvided(object sender, byte[] byteImage)
  366. {
  367. try
  368. {
  369. if (byteImage == null)
  370. {
  371. return;
  372. }
  373. _byteImageProviderPipeClient?.SendBytes(byteImage);
  374. _autoEFDiagnosisService.SendByteImageData();
  375. }
  376. catch (Exception ex)
  377. {
  378. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  379. }
  380. }
  381. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  382. {
  383. try
  384. {
  385. if (rawImage == null)
  386. {
  387. return;
  388. }
  389. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  390. _autoEFDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  391. }
  392. catch (Exception ex)
  393. {
  394. new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}");
  395. }
  396. }
  397. /// <summary>
  398. /// Close Myocardial Diagnosis
  399. /// </summary>
  400. public void Close()
  401. {
  402. try
  403. {
  404. if (_disposed)
  405. {
  406. return;
  407. }
  408. _initialized = false;
  409. if (_imageProvider != null)
  410. {
  411. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  412. _imageProvider.RawImageProvided -= OnRawImageProvided;
  413. _imageProvider.Stop();
  414. _imageProvider = null;
  415. }
  416. _autoEFDiagnosisService.Close();
  417. }
  418. catch (Exception ex)
  419. {
  420. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  421. }
  422. }
  423. public void Dispose()
  424. {
  425. try
  426. {
  427. if (!_disposed)
  428. {
  429. _initialized = false;
  430. if (AIManager.Instance.AINotificationManager != null)
  431. {
  432. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  433. }
  434. if (_imageProvider != null)
  435. {
  436. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  437. _imageProvider.RawImageProvided -= OnRawImageProvided;
  438. _imageProvider.Stop();
  439. _imageProvider = null;
  440. }
  441. if (_singleImagePipeClient != null)
  442. {
  443. _singleImagePipeClient.Dispose();
  444. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  445. _singleImagePipeClient = null;
  446. }
  447. if (_rawImageProviderPipeClient != null)
  448. {
  449. _rawImageProviderPipeClient.Dispose();
  450. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  451. _rawImageProviderPipeClient = null;
  452. }
  453. if (_byteImageProviderPipeClient != null)
  454. {
  455. _byteImageProviderPipeClient.Dispose();
  456. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  457. _byteImageProviderPipeClient = null;
  458. }
  459. _disposed = true;
  460. }
  461. }
  462. catch (Exception ex)
  463. {
  464. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  465. }
  466. }
  467. }
  468. }