CustomizeDiagnosis.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using AI.Common.Interface;
  2. using AI.Customize.Tasks.SampleCustomizeTask;
  3. using CustomizeDiagnosisSDK.Enums;
  4. using CustomizeDiagnosisSDK.Interfaces;
  5. using CustomizeDiagnosisSDK.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Threading;
  10. namespace CustomizeDiagnosisSDK
  11. {
  12. public class CustomizeDiagnosis
  13. {
  14. private static readonly List<string> _relatedDllList = new List<string>()
  15. {
  16. "AI.Customize.Tasks.SampleCustomizeTask.dll",
  17. };
  18. private readonly ManualResetEvent _handlingImageEvent = new ManualResetEvent(true);
  19. private readonly object _detectObject = new object();
  20. private SampleCustomizeTask _sampleCustomizeTask;
  21. private IImageProvider _usImageProvider;
  22. private EnumDetectMode _detectmode;
  23. /// <summary>
  24. /// 图片计数
  25. /// </summary>
  26. private int _imageCounter;
  27. /// <summary>
  28. /// 帧率
  29. /// </summary>
  30. private int _displayFps = -1;
  31. /// <summary>
  32. /// 每隔多少帧检测一次
  33. /// </summary>
  34. private int _detectInterval;
  35. /// <summary>
  36. /// 计时用
  37. /// </summary>
  38. private int _startTickCount = -1;
  39. /// <summary>
  40. /// 所有功能列表
  41. /// </summary>
  42. public static List<FunctionInfo> AllFunctions { get; }
  43. /// <summary>
  44. /// 检测模式
  45. /// </summary>
  46. public EnumDetectMode DetectMode
  47. {
  48. get
  49. {
  50. return _detectmode;
  51. }
  52. set
  53. {
  54. if (_detectmode != value)
  55. {
  56. _detectmode = value;
  57. ResetParameters();
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 每秒检测次数
  63. /// </summary>
  64. public int DetectTps
  65. {
  66. get; set;
  67. }
  68. /// <summary>
  69. /// 每次检测间隔时间
  70. /// </summary>
  71. public int IntervalTime
  72. {
  73. get; set;
  74. }
  75. /// <summary>
  76. /// 模型文件夹路径
  77. /// </summary>
  78. public string ModelFolder
  79. {
  80. get; private set;
  81. }
  82. /// <summary>
  83. /// Raised when the image evaluation is started.
  84. /// </summary>
  85. public event EventHandler StartEvaluation;
  86. /// <summary>
  87. /// Raised when the image evaluation is finished.
  88. /// </summary>
  89. public event EventHandler<IDetectedObject[]> FinishEvaluation;
  90. /// <summary>
  91. /// Raised when event NotifyLog raised from AI diag system
  92. /// </summary>
  93. public event EventHandler<LogEventArgs> NotifyLog;
  94. static CustomizeDiagnosis()
  95. {
  96. try
  97. {
  98. var sampleCustomizeTask = new SampleCustomizeTask();
  99. var versionInfo = sampleCustomizeTask.GetInfo();
  100. AllFunctions = new List<FunctionInfo>
  101. {
  102. new FunctionInfo(EnumCustomizeDiagnosisFunction.CustomizeDiagnosis,true,Assembly.GetExecutingAssembly().GetName().Version.ToString(),null, new List<EngineInfo>
  103. {
  104. new EngineInfo(EnumCustomizeDiagnosisEngineName.CustomizeDiagnosisEngine,EnumCustomizeDiagnosisEngineType.CustomizeDiagnosisEngine,true,versionInfo.Version,versionInfo.Company,null, _relatedDllList),
  105. }),
  106. };
  107. sampleCustomizeTask.Dispose();
  108. }
  109. catch
  110. {
  111. }
  112. }
  113. /// <summary>
  114. /// 初始化并加载SampleCustomizeTask
  115. /// </summary>
  116. /// <param name="usImageProvider">ImageProvider</param>
  117. /// <param name="detectMode">检测模式</param>
  118. /// <param name="detectTps">每秒图片吞吐量</param>
  119. /// <param name="intervalTime">间隔时间</param>
  120. /// <param name="modelFolder">模型文件夹路径,默认路径为AIDiagnosisSDK.dll所在目录下的子文件夹Network</param>
  121. public void Initialize(string modelFolder, IImageProvider usImageProvider, EnumDetectMode detectMode, int detectTps, int intervalTime)
  122. {
  123. _usImageProvider = usImageProvider;
  124. DetectMode = detectMode;
  125. ModelFolder = modelFolder;
  126. IntervalTime = intervalTime;
  127. DetectTps = detectTps;
  128. //Load SampleCustomizeTask.
  129. try
  130. {
  131. if (_sampleCustomizeTask == null)
  132. {
  133. _sampleCustomizeTask = new SampleCustomizeTask();
  134. _sampleCustomizeTask.NotifyLog += OnNotifyLog;
  135. _sampleCustomizeTask.NotifyProcessFinish += OnNotifyProcessFinish;
  136. _sampleCustomizeTask.LoadInferNet(modelFolder);
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. throw new Exception($"Load SampleCustomizeTask Failed!:{ex}");
  142. }
  143. }
  144. /// <summary>
  145. /// Start the Image Provider
  146. /// </summary>
  147. public void Start()
  148. {
  149. ResetParameters();
  150. if (_usImageProvider != null)
  151. {
  152. _usImageProvider.ImageProvided += OnUsImageProvided;
  153. _usImageProvider.Start();
  154. }
  155. }
  156. /// <summary>
  157. /// Stop the Image Provider
  158. /// </summary>
  159. public void Stop()
  160. {
  161. if (_usImageProvider != null)
  162. {
  163. _usImageProvider.ImageProvided -= OnUsImageProvided;
  164. _usImageProvider.Stop();
  165. }
  166. }
  167. /// <summary>
  168. /// Stop the Image Provider and Close the AutoVTI Diagnosis
  169. /// </summary>
  170. public void Close()
  171. {
  172. Stop();
  173. _handlingImageEvent.WaitOne();
  174. lock (_detectObject)
  175. {
  176. CloseDiagnosis();
  177. }
  178. }
  179. /// <summary>
  180. /// 检测单张图片
  181. /// </summary>
  182. public IDetectedObject[] DetectOneImage(ImageInputData imageData)
  183. {
  184. return HandleImage(false, imageData);
  185. }
  186. /// <summary>
  187. /// 检测单张图片
  188. /// </summary>
  189. public string DetectOneImageAsync(ImageInputData imageData)
  190. {
  191. return HandleImageAsync(imageData);
  192. }
  193. private void OnUsImageProvided(object sender, ImageInputData imageData)
  194. {
  195. OnUsImageChanged(imageData);
  196. }
  197. private void OnUsImageChanged(ImageInputData imageData)
  198. {
  199. if (DetectMode == EnumDetectMode.TimesPerSecond)///检测模式:每秒检测几次
  200. {//计算fps, 3秒检测一次
  201. if (_startTickCount == -1)
  202. {
  203. _startTickCount = Environment.TickCount;
  204. HandleImage(true, imageData);
  205. }
  206. else
  207. {
  208. var fpscurrentTickCount = Environment.TickCount;
  209. var timespan = fpscurrentTickCount - _startTickCount;
  210. if (timespan >= 3000)
  211. {
  212. _displayFps = _imageCounter * 1000 / timespan;
  213. _detectInterval = (int)Math.Round((float)_displayFps / DetectTps);
  214. _imageCounter = 0;
  215. _startTickCount = Environment.TickCount; ;
  216. }
  217. if (_detectInterval > 0)
  218. {
  219. if (_imageCounter % _detectInterval == 0)
  220. {
  221. HandleImage(true, imageData);
  222. }
  223. }
  224. }
  225. _imageCounter++;
  226. if (_imageCounter > 1000000)
  227. {
  228. _imageCounter = 0;
  229. }
  230. }
  231. else///检测模式:间隔固定时间检测
  232. {
  233. if (_startTickCount == -1)
  234. {
  235. _startTickCount = Environment.TickCount;
  236. HandleImage(true, imageData);
  237. }
  238. else
  239. {
  240. var currentTickCount = Environment.TickCount;
  241. if (IntervalTime <= currentTickCount - _startTickCount)
  242. {
  243. HandleImage(true, imageData);
  244. _startTickCount = Environment.TickCount;
  245. }
  246. }
  247. }
  248. }
  249. private IDetectedObject[] HandleImage(bool isImageProviderMode, ImageInputData imageData)
  250. {
  251. _handlingImageEvent.Reset();
  252. IDetectedObject[] diagResult = default;
  253. try
  254. {
  255. if (isImageProviderMode)
  256. {
  257. StartEvaluation?.Invoke(this, EventArgs.Empty);
  258. }
  259. lock (_detectObject)
  260. {
  261. diagResult = _sampleCustomizeTask.ProcessOneImage(imageData);
  262. }
  263. if (isImageProviderMode)
  264. {
  265. FinishEvaluation?.Invoke(this, diagResult);
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
  271. }
  272. finally
  273. {
  274. _handlingImageEvent.Set();
  275. }
  276. return diagResult;
  277. }
  278. private string HandleImageAsync(ImageInputData imageData)
  279. {
  280. _handlingImageEvent.Reset();
  281. string resultId = null;
  282. try
  283. {
  284. lock (_detectObject)
  285. {
  286. resultId = _sampleCustomizeTask.AddProcessRequest(imageData);
  287. }
  288. }
  289. catch (Exception ex)
  290. {
  291. NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
  292. }
  293. return resultId;
  294. }
  295. private void OnNotifyProcessFinish(object sender, KeyValuePair<string, IDetectedObject[]> e)
  296. {
  297. try
  298. {
  299. FinishEvaluation?.Invoke(e.Key, e.Value);
  300. }
  301. catch (Exception ex)
  302. {
  303. NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
  304. }
  305. finally
  306. {
  307. _handlingImageEvent.Set();
  308. }
  309. }
  310. private void OnNotifyLog(object sender, LogEventArgs e)
  311. {
  312. NotifyLog?.Invoke(this, e);
  313. }
  314. private void ResetParameters()
  315. {
  316. _startTickCount = -1;
  317. _imageCounter = 0;
  318. _displayFps = -1;
  319. _detectInterval = 0;
  320. }
  321. private void CloseDiagnosis()
  322. {
  323. if (_sampleCustomizeTask != null)
  324. {
  325. _sampleCustomizeTask.Dispose();
  326. _sampleCustomizeTask.NotifyLog -= OnNotifyLog;
  327. _sampleCustomizeTask.NotifyProcessFinish -= OnNotifyProcessFinish;
  328. _sampleCustomizeTask = null;
  329. }
  330. }
  331. }
  332. }