AutoVTIDiagnosis.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Reflection;
  3. using System.Text.Json;
  4. using Vinno.AI.AutoVTIDiagnosisSDK.Interfaces;
  5. using Vinno.AI.AutoVTIDiagnosisSDK.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.AutoVTIDiagnosisSDK
  11. {
  12. public class AutoVTIDiagnosis : IAutoVTIDiagnosis
  13. {
  14. private readonly IAutoVTIDiagnosisService _autoVTIDiagnosisService;
  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<AIAutoVTICalcResult> FinishEvaluationNotification;
  38. public AutoVTIDiagnosis()
  39. {
  40. _autoVTIDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAutoVTIDiagnosisService>();
  41. if (AIManager.Instance.AINotificationManager != null)
  42. {
  43. AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
  44. }
  45. _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisSingleImage);
  46. _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeClient.Start();
  48. _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisRawImageProvider);
  49. _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  50. _rawImageProviderPipeClient.Start();
  51. _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisByteImageProvider);
  52. _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
  53. _byteImageProviderPipeClient.Start();
  54. }
  55. /// <summary>
  56. /// 初始化AutoVTI Diagnosis
  57. /// </summary>
  58. /// <param name="autoVTIParameter">AutoVTI Diagnosis Parameter</param>
  59. /// <param name="imageProvider">Image Provider</param>
  60. public void Initialize(AutoVTIDiagnosisParameter autoVTIParameter, IAIImageProvider imageProvider = null)
  61. {
  62. try
  63. {
  64. if (!_initialized)
  65. {
  66. _imageProvider = imageProvider;
  67. _autoVTIDiagnosisService.Initialize(autoVTIParameter, _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. _autoVTIDiagnosisService.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. _autoVTIDiagnosisService.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.AutoVTIDiagnosisStartEvaluationRaised:
  111. StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
  112. break;
  113. case AIEnumNotificationType.AutoVTIDiagnosisFinishEvaluationRaised:
  114. var aiAutoVTICalcResult = JsonSerializer.Deserialize<AIAutoVTICalcResult>(e.Params?.ToString());
  115. FinishEvaluationNotification?.Invoke(this, aiAutoVTICalcResult);
  116. break;
  117. case AIEnumNotificationType.AutoVTIDiagnosisNotifyLogRaised:
  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. _autoVTIDiagnosisService.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. _autoVTIDiagnosisService.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. _autoVTIDiagnosisService.SetCmPerPixel(cmPerPixel);
  166. }
  167. catch (Exception ex)
  168. {
  169. 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. _autoVTIDiagnosisService.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 AIAutoVTICalcResult DetectOneImage(AIRawImage rawImage, float cmPerPixel)
  194. {
  195. if (rawImage == null)
  196. {
  197. throw new ArgumentNullException(nameof(rawImage));
  198. }
  199. try
  200. {
  201. _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
  202. return _autoVTIDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel);
  203. }
  204. catch (Exception ex)
  205. {
  206. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  207. return null;
  208. }
  209. }
  210. /// <summary>
  211. /// 检测单张Byte Image
  212. /// </summary>
  213. /// <param name="byteImage">图像资源</param>
  214. /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
  215. /// <returns></returns>
  216. public AIAutoVTICalcResult DetectOneImage(byte[] byteImage, float cmPerPixel)
  217. {
  218. if (byteImage == null)
  219. {
  220. throw new ArgumentNullException(nameof(byteImage));
  221. }
  222. try
  223. {
  224. _singleImagePipeClient.SendBytes(byteImage);
  225. return _autoVTIDiagnosisService.DetectOneByteImage(cmPerPixel);
  226. }
  227. catch (Exception ex)
  228. {
  229. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  230. return null;
  231. }
  232. }
  233. private void OnByteImageProvided(object sender, byte[] byteImage)
  234. {
  235. try
  236. {
  237. if (byteImage == null)
  238. {
  239. return;
  240. }
  241. _byteImageProviderPipeClient?.SendBytes(byteImage);
  242. _autoVTIDiagnosisService.SendByteImageData();
  243. }
  244. catch (Exception ex)
  245. {
  246. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  247. }
  248. }
  249. private void OnRawImageProvided(object sender, AIRawImage rawImage)
  250. {
  251. try
  252. {
  253. if (rawImage == null)
  254. {
  255. return;
  256. }
  257. _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
  258. _autoVTIDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
  259. }
  260. catch (Exception ex)
  261. {
  262. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  263. }
  264. }
  265. /// <summary>
  266. /// Close AutoVTI Diagnosis
  267. /// </summary>
  268. public void Close()
  269. {
  270. try
  271. {
  272. if (_disposed)
  273. {
  274. return;
  275. }
  276. _initialized = false;
  277. if (_imageProvider != null)
  278. {
  279. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  280. _imageProvider.RawImageProvided -= OnRawImageProvided;
  281. _imageProvider.Stop();
  282. _imageProvider = null;
  283. }
  284. _autoVTIDiagnosisService.Close();
  285. }
  286. catch (Exception ex)
  287. {
  288. AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
  289. }
  290. }
  291. public void Dispose()
  292. {
  293. try
  294. {
  295. if (!_disposed)
  296. {
  297. _initialized = false;
  298. if (AIManager.Instance.AINotificationManager != null)
  299. {
  300. AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
  301. }
  302. if (_imageProvider != null)
  303. {
  304. _imageProvider.ByteImageProvided -= OnByteImageProvided;
  305. _imageProvider.RawImageProvided -= OnRawImageProvided;
  306. _imageProvider.Stop();
  307. _imageProvider = null;
  308. }
  309. if (_singleImagePipeClient != null)
  310. {
  311. _singleImagePipeClient.Dispose();
  312. _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
  313. _singleImagePipeClient = null;
  314. }
  315. if (_rawImageProviderPipeClient != null)
  316. {
  317. _rawImageProviderPipeClient.Dispose();
  318. _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  319. _rawImageProviderPipeClient = null;
  320. }
  321. if (_byteImageProviderPipeClient != null)
  322. {
  323. _byteImageProviderPipeClient.Dispose();
  324. _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
  325. _byteImageProviderPipeClient = null;
  326. }
  327. _disposed = true;
  328. }
  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. }
  336. }