AutoBLineDiagnosis.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. using Vinno.AI.AutoBLineDiagnosisSDK.Interfaces;
  5. using Vinno.AI.AutoBLineDiagnosisSDK.Models;
  6. using Vinno.AI.AutoBLineDiagnosisSDK.Tools;
  7. using Vinno.AI.CommonSDK.Enums;
  8. using Vinno.AI.CommonSDK.Interfaces;
  9. using Vinno.AI.CommonSDK.Models;
  10. using Vinno.AI.CommonSDK.Tools;
  11. namespace Vinno.AI.AutoBLineDiagnosisSDK
  12. {
  13. public class AutoBLineDiagnosis : IAutoBLineDiagnosis
  14. {
  15. private readonly IAutoBLineDiagnosisService _autoBLineDiagnosisService;
  16. private IAIImageProvider _imageProvider;
  17. private bool _initialized;
  18. private bool _disposed;
  19. /// <summary>
  20. /// Used For Detect One Image
  21. /// </summary>
  22. private PipeClient _singleImagePipeClient;
  23. /// <summary>
  24. /// Used For Provide Raw Image
  25. /// </summary>
  26. private PipeClient _rawImageProviderPipeClient;
  27. /// <summary>
  28. /// Used For Provide Byte Image
  29. /// </summary>
  30. private PipeClient _byteImageProviderPipeClient;
  31. /// <summary>
  32. /// Raised when the image evaluation is started.
  33. /// </summary>
  34. public event EventHandler StartEvaluationNotification;
  35. /// <summary>
  36. /// Raised when the image evaluation is finished.
  37. /// </summary>
  38. public event EventHandler<AIBlineScoringResults> FinishEvaluationNotification;
  39. public AutoBLineDiagnosis()
  40. {
  41. _autoBLineDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAutoBLineDiagnosisService>();
  42. if (AIManager.Instance.AINotificationManager != null)
  43. {
  44. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  45. }
  46. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoBLineDiagnosisSingleImage);
  47. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  48. _singleImagePipeClient.Start();
  49. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoBLineDiagnosisRawImageProvider);
  50. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  51. _rawImageProviderPipeClient.Start();
  52. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoBLineDiagnosisByteImageProvider);
  53. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  54. _byteImageProviderPipeClient.Start();
  55. }
  56. /// <summary>
  57. /// 初始化AutoBLine Diagnosis
  58. /// </summary>
  59. /// <param name="autoBLineParameter">AutoBLine Diagnosis Parameter</param>
  60. /// <param name="imageProvider">Image Provider</param>
  61. public void Initialize(AutoBLineDiagnosisParameter autoBLineParameter, IAIImageProvider imageProvider = null)
  62. {
  63. try
  64. {
  65. if (!_initialized)
  66. {
  67. _imageProvider = imageProvider;
  68. _autoBLineDiagnosisService.Initialize(autoBLineParameter, _imageProvider != null);
  69. _initialized = true;
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  75. }
  76. }
  77. /// <summary>
  78. /// Start Image Provider
  79. /// </summary>
  80. public void Start()
  81. {
  82. if (_imageProvider != null)
  83. {
  84. _autoBLineDiagnosisService.Start();
  85. _imageProvider.ByteImageProvided += OnByteImageProvided;
  86. _imageProvider.RawImageProvided += OnRawImageProvided;
  87. _imageProvider.Start();
  88. }
  89. }
  90. /// <summary>
  91. /// Stop Image Provider
  92. /// </summary>
  93. public void Stop()
  94. {
  95. if (_imageProvider != null)
  96. {
  97. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  98. _imageProvider.RawImageProvided -= OnRawImageProvided;
  99. _imageProvider.Stop();
  100. }
  101. _autoBLineDiagnosisService.Stop();
  102. }
  103. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  104. {
  105. AIManager.Instance.AILogManager?.WriteLogInfo(e);
  106. }
  107. private void OnNotificationReceived(object sender, AINotificationArgs e)
  108. {
  109. switch (e.NotificationType)
  110. {
  111. case AIEnumNotificationType.AutoBLineDiagnosisStartEvaluationRaised:
  112. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  113. break;
  114. case AIEnumNotificationType.AutoBLineDiagnosisFinishEvaluationRaised:
  115. var transAIBlineScoringResults = JsonSerializer.Deserialize<TransAIBlineScoringResults>(e.Params?.ToString());
  116. var result = AIConvertHelper.ConvertTransAIBlineScoringResultsToAIBlineScoringResults(transAIBlineScoringResults);
  117. FinishEvaluationNotification?.Invoke(this, result);
  118. break;
  119. case AIEnumNotificationType.AutoBLineDiagnosisNotifyLogRaised:
  120. var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
  121. AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
  122. break;
  123. }
  124. }
  125. /// <summary>
  126. /// 设置每秒图片吞吐量
  127. /// </summary>
  128. /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
  129. public void SetDetectTps(int detectTps)
  130. {
  131. if (detectTps <= 0)
  132. {
  133. throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
  134. }
  135. try
  136. {
  137. _autoBLineDiagnosisService.SetDetectTps(detectTps);
  138. }
  139. catch (Exception ex)
  140. {
  141. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  142. }
  143. }
  144. /// <summary>
  145. /// 设置间隔时间
  146. /// </summary>
  147. /// <param name="intervalTime"></param>
  148. public void SetIntervalTime(int intervalTime)
  149. {
  150. try
  151. {
  152. _autoBLineDiagnosisService.SetIntervalTime(intervalTime);
  153. }
  154. catch (Exception ex)
  155. {
  156. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  157. }
  158. }
  159. /// <summary>
  160. /// 设置一个像素代表的实际物理距离是多少cm
  161. /// </summary>
  162. /// <param name="cmPerPixel"></param>
  163. public void SetCmPerPixel(float cmPerPixel)
  164. {
  165. try
  166. {
  167. _autoBLineDiagnosisService.SetCmPerPixel(cmPerPixel);
  168. }
  169. catch (Exception ex)
  170. {
  171. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  172. }
  173. }
  174. /// <summary>
  175. /// 设置检测模式
  176. /// </summary>
  177. /// <param name="detectMode"></param>
  178. public void SetDetectMode(AIEnumDetectMode detectMode)
  179. {
  180. try
  181. {
  182. _autoBLineDiagnosisService.SetDetectMode(detectMode);
  183. }
  184. catch (Exception ex)
  185. {
  186. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  187. }
  188. }
  189. /// <summary>
  190. /// 检测单张Raw Image
  191. /// </summary>
  192. /// <param name="rawImage">图像资源</param>
  193. /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
  194. /// <returns></returns>
  195. public AIBlineScoringResults DetectOneImage(AIRawImage rawImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
  196. {
  197. if (rawImage == null)
  198. {
  199. throw new ArgumentNullException(nameof(rawImage));
  200. }
  201. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  202. {
  203. throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
  204. }
  205. try
  206. {
  207. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  208. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  209. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  210. var result = _autoBLineDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel, transUltrasoundImageRegion);
  211. return AIConvertHelper.ConvertTransAIBlineScoringResultsToAIBlineScoringResults(result);
  212. }
  213. catch (Exception ex)
  214. {
  215. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  216. return null;
  217. }
  218. }
  219. /// <summary>
  220. /// 检测单张Byte Image
  221. /// </summary>
  222. /// <param name="byteImage">图像资源</param>
  223. /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
  224. /// <returns></returns>
  225. public AIBlineScoringResults DetectOneImage(byte[] byteImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
  226. {
  227. if (byteImage == null)
  228. {
  229. throw new ArgumentNullException(nameof(byteImage));
  230. }
  231. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  232. {
  233. throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
  234. }
  235. try
  236. {
  237. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  238. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  239. _singleImagePipeClient.SendBytes(byteImage);
  240. var result = _autoBLineDiagnosisService.DetectOneByteImage(cmPerPixel, transUltrasoundImageRegion);
  241. return AIConvertHelper.ConvertTransAIBlineScoringResultsToAIBlineScoringResults(result);
  242. }
  243. catch (Exception ex)
  244. {
  245. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  246. return null;
  247. }
  248. }
  249. /// <summary>
  250. /// 设置超声机图像信息(当使用ImageProvider时需设置)
  251. /// </summary>
  252. /// <param name="ultrasoundImageRegion"></param>
  253. public void SetUltrasoundImageRegionInfo(IAIUltrasoundImageRegion ultrasoundImageRegion)
  254. {
  255. if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
  256. {
  257. throw new ArgumentException("SetUltrasoundImageRegionInfo Only Support AIConvexArrayUltrasoundImageRegion Type");
  258. }
  259. try
  260. {
  261. var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
  262. var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
  263. _autoBLineDiagnosisService.SetProbeImageInfo(transUltrasoundImageRegion);
  264. }
  265. catch (Exception ex)
  266. {
  267. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  268. }
  269. }
  270. private void OnByteImageProvided(object sender, byte[] byteImage)
  271. {
  272. try
  273. {
  274. if (byteImage == null)
  275. {
  276. return;
  277. }
  278. _byteImageProviderPipeClient?.SendBytes(byteImage);
  279. _autoBLineDiagnosisService.SendByteImageData();
  280. }
  281. catch (Exception ex)
  282. {
  283. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  284. }
  285. }
  286. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  287. {
  288. try
  289. {
  290. if (rawImage == null)
  291. {
  292. return;
  293. }
  294. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  295. _autoBLineDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  296. }
  297. catch (Exception ex)
  298. {
  299. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  300. }
  301. }
  302. /// <summary>
  303. /// Close AutoBLine Diagnosis
  304. /// </summary>
  305. public void Close()
  306. {
  307. try
  308. {
  309. if (_disposed)
  310. {
  311. return;
  312. }
  313. _initialized = false;
  314. if (_imageProvider != null)
  315. {
  316. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  317. _imageProvider.RawImageProvided -= OnRawImageProvided;
  318. _imageProvider.Stop();
  319. _imageProvider = null;
  320. }
  321. _autoBLineDiagnosisService.Close();
  322. }
  323. catch (Exception ex)
  324. {
  325. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  326. }
  327. }
  328. public void Dispose()
  329. {
  330. try
  331. {
  332. if (!_disposed)
  333. {
  334. _initialized = false;
  335. if (AIManager.Instance.AINotificationManager != null)
  336. {
  337. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  338. }
  339. if (_imageProvider != null)
  340. {
  341. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  342. _imageProvider.RawImageProvided -= OnRawImageProvided;
  343. _imageProvider.Stop();
  344. _imageProvider = null;
  345. }
  346. if (_singleImagePipeClient != null)
  347. {
  348. _singleImagePipeClient.Dispose();
  349. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  350. _singleImagePipeClient = null;
  351. }
  352. if (_rawImageProviderPipeClient != null)
  353. {
  354. _rawImageProviderPipeClient.Dispose();
  355. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  356. _rawImageProviderPipeClient = null;
  357. }
  358. if (_byteImageProviderPipeClient != null)
  359. {
  360. _byteImageProviderPipeClient.Dispose();
  361. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  362. _byteImageProviderPipeClient = null;
  363. }
  364. _disposed = true;
  365. }
  366. }
  367. catch (Exception ex)
  368. {
  369. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  370. }
  371. }
  372. }
  373. }