FatLayerIdentificationService.cs 16 KB

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