AmnioticFluidDiagnosis.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. using Vinno.AI.AmnioticFluidDiagnosisSDK.Interfaces;
  5. using Vinno.AI.AmnioticFluidDiagnosisSDK.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.AmnioticFluidDiagnosisSDK
  11. {
  12. public class AmnioticFluidDiagnosis : IAmnioticFluidDiagnosis
  13. {
  14. private readonly IAmnioticFluidDiagnosisService _amnioticFluidDiagnosisService;
  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<AIAmnioticFluidMeasurementResults> FinishEvaluationNotification;
  38. public AmnioticFluidDiagnosis()
  39. {
  40. _amnioticFluidDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAmnioticFluidDiagnosisService>();
  41. if (AIManager.Instance.AINotificationManager != null)
  42. {
  43. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  44. }
  45. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisSingleImage);
  46. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeClient.Start();
  48. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisRawImageProvider);
  49. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  50. _rawImageProviderPipeClient.Start();
  51. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisByteImageProvider);
  52. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  53. _byteImageProviderPipeClient.Start();
  54. }
  55. /// <summary>
  56. /// 初始化AmnioticFluid Diagnosis
  57. /// </summary>
  58. /// <param name="amnioticFluidDiagnosisParameter">AmnioticFluid Diagnosis Parameter</param>
  59. /// <param name="imageProvider">Image Provider</param>
  60. public void Initialize(AmnioticFluidDiagnosisParameter amnioticFluidDiagnosisParameter, IAIImageProvider imageProvider = null)
  61. {
  62. try
  63. {
  64. if (!_initialized)
  65. {
  66. _imageProvider = imageProvider;
  67. _amnioticFluidDiagnosisService.Initialize(amnioticFluidDiagnosisParameter, _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. _amnioticFluidDiagnosisService.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. _amnioticFluidDiagnosisService.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.AmnioticFluidDiagnosisStartEvaluationRaised:
  111. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  112. break;
  113. case AIEnumNotificationType.AmnioticFluidDiagnosisFinishEvaluationRaised:
  114. var aiAmnioticFluidMeasurementResults = JsonSerializer.Deserialize<AIAmnioticFluidMeasurementResults>(e.Params?.ToString());
  115. FinishEvaluationNotification?.Invoke(this, aiAmnioticFluidMeasurementResults);
  116. break;
  117. case AIEnumNotificationType.AmnioticFluidDiagnosisNotifyLogRaised:
  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. _amnioticFluidDiagnosisService.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. _amnioticFluidDiagnosisService.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. /// 设置一个像素代表的实际物理距离是多少cm
  159. /// </summary>
  160. /// <param name="cmPerPixel"></param>
  161. public void SetCmPerPixel(float cmPerPixel)
  162. {
  163. try
  164. {
  165. _amnioticFluidDiagnosisService.SetCmPerPixel(cmPerPixel);
  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="detectMode"></param>
  176. public void SetDetectMode(AIEnumDetectMode detectMode)
  177. {
  178. try
  179. {
  180. _amnioticFluidDiagnosisService.SetDetectMode(detectMode);
  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. /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
  192. /// <returns></returns>
  193. public AIAmnioticFluidMeasurementResults DetectOneImage(AIRawImage rawImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
  194. {
  195. if (rawImage == null)
  196. {
  197. throw new ArgumentNullException(nameof(rawImage));
  198. }
  199. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  200. {
  201. throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
  202. }
  203. try
  204. {
  205. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  206. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  207. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  208. return _amnioticFluidDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel, transUltrasoundImageRegion);
  209. }
  210. catch (Exception ex)
  211. {
  212. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  213. return null;
  214. }
  215. }
  216. /// <summary>
  217. /// 检测单张Byte Image
  218. /// </summary>
  219. /// <param name="byteImage">图像资源</param>
  220. /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
  221. /// <returns></returns>
  222. public AIAmnioticFluidMeasurementResults DetectOneImage(byte[] byteImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
  223. {
  224. if (byteImage == null)
  225. {
  226. throw new ArgumentNullException(nameof(byteImage));
  227. }
  228. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  229. {
  230. throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
  231. }
  232. try
  233. {
  234. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  235. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  236. _singleImagePipeClient.SendBytes(byteImage);
  237. return _amnioticFluidDiagnosisService.DetectOneByteImage(cmPerPixel, transUltrasoundImageRegion);
  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. /// <summary>
  246. /// 设置超声机图像信息(当使用ImageProvider时需设置)
  247. /// </summary>
  248. /// <param name="ultrasoundImageRegion"></param>
  249. public void SetUltrasoundImageRegionInfo(IAIUltrasoundImageRegion ultrasoundImageRegion)
  250. {
  251. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  252. {
  253. throw new ArgumentException("SetUltrasoundImageRegionInfo Only Support AIConvexArrayUltrasoundImageRegion Type");
  254. }
  255. try
  256. {
  257. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  258. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  259. _amnioticFluidDiagnosisService.SetProbeImageInfo(transUltrasoundImageRegion);
  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. private void OnByteImageProvided(object sender, byte[] byteImage)
  267. {
  268. try
  269. {
  270. if (byteImage == null)
  271. {
  272. return;
  273. }
  274. _byteImageProviderPipeClient?.SendBytes(byteImage);
  275. _amnioticFluidDiagnosisService.SendByteImageData();
  276. }
  277. catch (Exception ex)
  278. {
  279. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  280. }
  281. }
  282. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  283. {
  284. try
  285. {
  286. if (rawImage == null)
  287. {
  288. return;
  289. }
  290. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  291. _amnioticFluidDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  292. }
  293. catch (Exception ex)
  294. {
  295. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  296. }
  297. }
  298. /// <summary>
  299. /// Close AmnioticFluid Diagnosis
  300. /// </summary>
  301. public void Close()
  302. {
  303. try
  304. {
  305. if (_disposed)
  306. {
  307. return;
  308. }
  309. _initialized = false;
  310. if (_imageProvider != null)
  311. {
  312. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  313. _imageProvider.RawImageProvided -= OnRawImageProvided;
  314. _imageProvider.Stop();
  315. _imageProvider = null;
  316. }
  317. _amnioticFluidDiagnosisService.Close();
  318. }
  319. catch (Exception ex)
  320. {
  321. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  322. }
  323. }
  324. public void Dispose()
  325. {
  326. try
  327. {
  328. if (!_disposed)
  329. {
  330. _initialized = false;
  331. if (AIManager.Instance.AINotificationManager != null)
  332. {
  333. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  334. }
  335. if (_imageProvider != null)
  336. {
  337. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  338. _imageProvider.RawImageProvided -= OnRawImageProvided;
  339. _imageProvider.Stop();
  340. _imageProvider = null;
  341. }
  342. if (_singleImagePipeClient != null)
  343. {
  344. _singleImagePipeClient.Dispose();
  345. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  346. _singleImagePipeClient = null;
  347. }
  348. if (_rawImageProviderPipeClient != null)
  349. {
  350. _rawImageProviderPipeClient.Dispose();
  351. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  352. _rawImageProviderPipeClient = null;
  353. }
  354. if (_byteImageProviderPipeClient != null)
  355. {
  356. _byteImageProviderPipeClient.Dispose();
  357. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  358. _byteImageProviderPipeClient = null;
  359. }
  360. _disposed = true;
  361. }
  362. }
  363. catch (Exception ex)
  364. {
  365. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  366. }
  367. }
  368. }
  369. }