AutoVTIDiagnosisService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosis.Common.Models;
  5. using AutoVTICalculationLib;
  6. using AutoVTIDiagnosisSDK;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Threading;
  12. using Vinno.AI.AutoVTIDiagnosisSDK.Interfaces;
  13. using Vinno.AI.AutoVTIDiagnosisSDK.Models;
  14. using Vinno.AI.AutoVTIDiagnosisService.Tools;
  15. using Vinno.AI.CommonSDK.Enums;
  16. using Vinno.AI.CommonSDK.Models;
  17. using Vinno.AI.CommonSDK.Tools;
  18. using Vinno.AI.Service.Common.Interfaces;
  19. using Vinno.AI.Service.Common.Models;
  20. using Vinno.AI.Service.Common.Tools;
  21. namespace Vinno.AI.AutoVTIDiagnosisService
  22. {
  23. public class AutoVTIDiagnosisService : IAutoVTIDiagnosisService, IDisposable, IEngine
  24. {
  25. private readonly string _modelFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  26. private readonly ConcurrentStack<byte[]> _singleImageCache;
  27. private PipeServer _singleImagePipeServer;
  28. private ImageProvider _imageProvider;
  29. private AutoVTIDiagnosis _autoVTIDiagnosis;
  30. private bool _disposed;
  31. public static List<FunctionInfo> AllFunctions { get; }
  32. static AutoVTIDiagnosisService()
  33. {
  34. AllFunctions = AutoVTIDiagnosis.AllFunctions;
  35. }
  36. public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
  37. {
  38. AutoVTIDiagnosis.Init(enableFunction, currentUsedFunction);
  39. }
  40. public AutoVTIDiagnosisService()
  41. {
  42. try
  43. {
  44. _singleImageCache = new ConcurrentStack<byte[]>();
  45. _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisSingleImage);
  46. _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeServer.DataReceived += OnDataReceived;
  48. _singleImagePipeServer.StartAndReceive();
  49. _imageProvider = new ImageProvider(AIEnumType.AutoVTIDiagnosis);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.Error($"AutoVTIDiagnosisService-Constructor error:{ex}");
  54. }
  55. }
  56. private void OnDataReceived(object sender, byte[] e)
  57. {
  58. try
  59. {
  60. _singleImageCache.Push(e);
  61. }
  62. catch (Exception ex)
  63. {
  64. Logger.Error($"AutoVTIDiagnosisService-OnDataReceived error:{ex}");
  65. }
  66. }
  67. public void Initialize(AutoVTIDiagnosisParameter autoVTIParameter, bool hasImageProvider)
  68. {
  69. try
  70. {
  71. CloseDiagnosis();
  72. var cpuNumber = autoVTIParameter.CPUNumber;
  73. var detectMode = (EnumDetectMode)autoVTIParameter.DetectMode;
  74. var detectTps = autoVTIParameter.DetectTps;
  75. var intervalTime = autoVTIParameter.IntervalTime;
  76. var cmPerPixel = autoVTIParameter.CmPerPixel;
  77. var isIncludeContour = autoVTIParameter.IsIncludeCountour;
  78. _autoVTIDiagnosis = new AutoVTIDiagnosis(Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, cpuNumber, _modelFolder, cmPerPixel, detectMode, detectTps, intervalTime, isIncludeContour);
  79. _autoVTIDiagnosis.StartEvaluation += OnStartEvaluation;
  80. _autoVTIDiagnosis.FinishEvaluation += OnFinishEvaluation;
  81. _autoVTIDiagnosis.NotifyError += OnNotifyError;
  82. _autoVTIDiagnosis.NotifyLog += OnNotifyLog;
  83. }
  84. catch (Exception ex)
  85. {
  86. Logger.Error($"AutoVTIDiagnosisService-Initialize error:{ex}");
  87. }
  88. }
  89. /// <summary>
  90. /// 切换引擎
  91. /// </summary>
  92. /// <param name="functionInfo"></param>
  93. public void SwitchEngines(FunctionInfo functionInfo)
  94. {
  95. try
  96. {
  97. AutoVTIDiagnosis.SwitchAIEngines(functionInfo);
  98. if (_autoVTIDiagnosis != null)
  99. {
  100. _autoVTIDiagnosis.SwitchAIEngines2(functionInfo);
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. Logger.Error($"AutoVTIDiagnosisService-SwitchEngines error:{ex}");
  106. }
  107. }
  108. public void Start()
  109. {
  110. try
  111. {
  112. if (_autoVTIDiagnosis != null)
  113. {
  114. _autoVTIDiagnosis.Start();
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. Logger.Error($"AutoVTIDiagnosisService-Start error:{ex}");
  120. }
  121. }
  122. public void Stop()
  123. {
  124. try
  125. {
  126. if (_autoVTIDiagnosis != null)
  127. {
  128. _autoVTIDiagnosis.Stop();
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Logger.Error($"AutoVTIDiagnosisService-Stop error:{ex}");
  134. }
  135. }
  136. public void SetDetectTps(int detectTps)
  137. {
  138. try
  139. {
  140. if (_autoVTIDiagnosis != null)
  141. {
  142. _autoVTIDiagnosis.DetectTps = detectTps;
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. Logger.Error($"AutoVTIDiagnosisService-SetDetectTps error:{ex}");
  148. }
  149. }
  150. public void SetIntervalTime(int intervalTime)
  151. {
  152. try
  153. {
  154. if (_autoVTIDiagnosis != null)
  155. {
  156. _autoVTIDiagnosis.IntervalTime = intervalTime;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Logger.Error($"AutoVTIDiagnosisService-SetIntervalTime error:{ex}");
  162. }
  163. }
  164. public void SetDetectMode(AIEnumDetectMode detectMode)
  165. {
  166. try
  167. {
  168. if (_autoVTIDiagnosis != null)
  169. {
  170. _autoVTIDiagnosis.DetectMode = (EnumDetectMode)detectMode;
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. Logger.Error($"AutoVTIDiagnosisService-SetDetectMode error:{ex}");
  176. }
  177. }
  178. /// <summary>
  179. /// 设置一个像素代表的实际物理距离是多少cm
  180. /// </summary>
  181. /// <param name="cmPerPixel"></param>
  182. public void SetCmPerPixel(float cmPerPixel)
  183. {
  184. try
  185. {
  186. if (_autoVTIDiagnosis != null)
  187. {
  188. _autoVTIDiagnosis.CmPerPixel = cmPerPixel;
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. Logger.Error($"AutoVTIDiagnosisService-SetCmPerPixel error:{ex}");
  194. }
  195. }
  196. /// <summary>
  197. /// 检测单张Byte Image
  198. /// </summary>
  199. /// <param name="cmPerPixel">图像上一个像素实际代表的物理距离是多少cm</param>
  200. /// <returns></returns>
  201. public AIAutoVTICalcResult DetectOneByteImage(float cmPerPixel)
  202. {
  203. try
  204. {
  205. byte[] byteImage = null;
  206. int retryCount = 0;
  207. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  208. {
  209. Thread.Sleep(10);
  210. retryCount++;
  211. }
  212. if (byteImage == null)
  213. {
  214. Logger.Error($"AutoVTIDiagnosisService-DetectOneByteImage Get Image Cache Fail");
  215. return null;
  216. }
  217. var autoVTICalculationResults = _autoVTIDiagnosis.DetectOneImage(byteImage, cmPerPixel);
  218. return AIConvertHelper.ConvertAutoVTICalcResultToAIAutoVTICalcResult(autoVTICalculationResults);
  219. }
  220. catch (Exception ex)
  221. {
  222. Logger.Error($"AutoVTIDiagnosisService-DetectOneByteImage error:{ex}");
  223. return null;
  224. }
  225. }
  226. public AIAutoVTICalcResult DetectOneRawImage(int height, int width, AIEnumColorType colorType, float cmPerPixel)
  227. {
  228. try
  229. {
  230. byte[] byteImage = null;
  231. int retryCount = 0;
  232. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  233. {
  234. Thread.Sleep(10);
  235. retryCount++;
  236. }
  237. if (byteImage == null)
  238. {
  239. Logger.Error($"AutoVTIDiagnosisService-DetectOneRawImage Get Image Cache Fail");
  240. return null;
  241. }
  242. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  243. var autoVTICalculationResults = _autoVTIDiagnosis.DetectOneImage(rawImage, cmPerPixel);
  244. return AIConvertHelper.ConvertAutoVTICalcResultToAIAutoVTICalcResult(autoVTICalculationResults);
  245. }
  246. catch (Exception ex)
  247. {
  248. Logger.Error($"AutoVTIDiagnosisService-DetectOneRawImage error:{ex}");
  249. return null;
  250. }
  251. }
  252. /// <summary>
  253. /// Send Raw Image Data For Pipe
  254. /// </summary>
  255. /// <param name="height"></param>
  256. /// <param name="width"></param>
  257. /// <param name="colorType"></param>
  258. public void SendRawImageData(int height, int width, AIEnumColorType colorType)
  259. {
  260. try
  261. {
  262. _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
  263. }
  264. catch (Exception ex)
  265. {
  266. Logger.Error($"AutoVTIDiagnosisService-SendRawImageData error:{ex}");
  267. }
  268. }
  269. /// <summary>
  270. /// Send Byte Image Data For Pipe
  271. /// </summary>
  272. public void SendByteImageData()
  273. {
  274. try
  275. {
  276. _imageProvider?.ReceiveByteImageData();
  277. }
  278. catch (Exception ex)
  279. {
  280. Logger.Error($"AutoVTIDiagnosisService-SendByteImageData error:{ex}");
  281. }
  282. }
  283. public void Close()
  284. {
  285. try
  286. {
  287. Logger.Info($"AutoVTIDiagnosisService-Close Invoke");
  288. CloseDiagnosis();
  289. }
  290. catch (Exception ex)
  291. {
  292. Logger.Error($"AutoVTIDiagnosisService-Close error:{ex}");
  293. }
  294. }
  295. private void CloseDiagnosis()
  296. {
  297. if (_autoVTIDiagnosis != null)
  298. {
  299. _autoVTIDiagnosis.StartEvaluation -= OnStartEvaluation;
  300. _autoVTIDiagnosis.FinishEvaluation -= OnFinishEvaluation;
  301. _autoVTIDiagnosis.NotifyError -= OnNotifyError;
  302. _autoVTIDiagnosis.NotifyLog -= OnNotifyLog;
  303. _autoVTIDiagnosis.Close();
  304. _autoVTIDiagnosis = null;
  305. }
  306. }
  307. public void Dispose()
  308. {
  309. try
  310. {
  311. if (!_disposed)
  312. {
  313. Logger.Info($"AutoVTIDiagnosisService-Start Dispose");
  314. CloseDiagnosis();
  315. if (_singleImagePipeServer != null)
  316. {
  317. _singleImagePipeServer.DataReceived -= OnDataReceived;
  318. _singleImagePipeServer.Dispose();
  319. _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
  320. _singleImagePipeServer = null;
  321. }
  322. _imageProvider.Dispose();
  323. _imageProvider = null;
  324. _disposed = true;
  325. Logger.Info($"AutoVTIDiagnosisService Dispose End");
  326. }
  327. }
  328. catch (Exception ex)
  329. {
  330. Logger.Error($"AutoVTIDiagnosisService-Dispose error:{ex}");
  331. }
  332. }
  333. private void OnStartEvaluation(object sender, EventArgs e)
  334. {
  335. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoVTIDiagnosisStartEvaluationRaised, Params = e });
  336. }
  337. private void OnFinishEvaluation(object sender, AutoVTICalcResult e)
  338. {
  339. var aiAutoVTICalculationResults = AIConvertHelper.ConvertAutoVTICalcResultToAIAutoVTICalcResult(e);
  340. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoVTIDiagnosisFinishEvaluationRaised, Params = aiAutoVTICalculationResults });
  341. }
  342. private void OnNotifyLog(object sender, LogEventArgs e)
  343. {
  344. if (e == null)
  345. {
  346. return;
  347. }
  348. switch (e.LogType)
  349. {
  350. case EnumLogType.InfoLog:
  351. Logger.Info($"AutoVTIDiagnosisService OnNotifyLog:{e.Msg}");
  352. break;
  353. case EnumLogType.WarnLog:
  354. Logger.Warning($"AutoVTIDiagnosisService OnNotifyLog:{e.Msg}");
  355. break;
  356. case EnumLogType.ErrorLog:
  357. case EnumLogType.FatalLog:
  358. default:
  359. Logger.Error($"AutoVTIDiagnosisService OnNotifyLog:{e.Msg}");
  360. break;
  361. }
  362. var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
  363. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoVTIDiagnosisNotifyLogRaised, Params = logEventArgs });
  364. }
  365. private void OnNotifyError(object sender, ErrorEventArgs e)
  366. {
  367. Logger.Error($"AutoVTIDiagnosisService OnNotifyError:{e.GetException()}");
  368. var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
  369. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoVTIDiagnosisNotifyLogRaised, Params = logEventArgs });
  370. }
  371. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  372. {
  373. if (e == null)
  374. {
  375. return;
  376. }
  377. switch (e.LogType)
  378. {
  379. case AIEnumLogType.ErrorLog:
  380. case AIEnumLogType.FatalLog:
  381. Logger.Error(e.Msg);
  382. break;
  383. case AIEnumLogType.WarnLog:
  384. Logger.Warning(e.Msg);
  385. break;
  386. case AIEnumLogType.InfoLog:
  387. default:
  388. Logger.Info(e.Msg);
  389. break;
  390. }
  391. }
  392. }
  393. }