123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- using AI.Common.Interface;
- using AI.Customize.Tasks.SampleCustomizeTask;
- using CustomizeDiagnosisSDK.Enums;
- using CustomizeDiagnosisSDK.Interfaces;
- using CustomizeDiagnosisSDK.Models;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Threading;
- namespace CustomizeDiagnosisSDK
- {
- public class CustomizeDiagnosis
- {
- private static readonly List<string> _relatedDllList = new List<string>()
- {
- "AI.Customize.Tasks.SampleCustomizeTask.dll",
- };
- private readonly ManualResetEvent _handlingImageEvent = new ManualResetEvent(true);
- private readonly object _detectObject = new object();
- private SampleCustomizeTask _sampleCustomizeTask;
- private IImageProvider _usImageProvider;
- private EnumDetectMode _detectmode;
- /// <summary>
- /// 图片计数
- /// </summary>
- private int _imageCounter;
- /// <summary>
- /// 帧率
- /// </summary>
- private int _displayFps = -1;
- /// <summary>
- /// 每隔多少帧检测一次
- /// </summary>
- private int _detectInterval;
- /// <summary>
- /// 计时用
- /// </summary>
- private int _startTickCount = -1;
- /// <summary>
- /// 所有功能列表
- /// </summary>
- public static List<FunctionInfo> AllFunctions { get; }
- /// <summary>
- /// 检测模式
- /// </summary>
- public EnumDetectMode DetectMode
- {
- get
- {
- return _detectmode;
- }
- set
- {
- if (_detectmode != value)
- {
- _detectmode = value;
- ResetParameters();
- }
- }
- }
- /// <summary>
- /// 每秒检测次数
- /// </summary>
- public int DetectTps
- {
- get; set;
- }
- /// <summary>
- /// 每次检测间隔时间
- /// </summary>
- public int IntervalTime
- {
- get; set;
- }
- /// <summary>
- /// 模型文件夹路径
- /// </summary>
- public string ModelFolder
- {
- get; private set;
- }
- /// <summary>
- /// Raised when the image evaluation is started.
- /// </summary>
- public event EventHandler StartEvaluation;
- /// <summary>
- /// Raised when the image evaluation is finished.
- /// </summary>
- public event EventHandler<IDetectedObject[]> FinishEvaluation;
- /// <summary>
- /// Raised when event NotifyLog raised from AI diag system
- /// </summary>
- public event EventHandler<LogEventArgs> NotifyLog;
- static CustomizeDiagnosis()
- {
- try
- {
- var sampleCustomizeTask = new SampleCustomizeTask();
- var versionInfo = sampleCustomizeTask.GetInfo();
- AllFunctions = new List<FunctionInfo>
- {
- new FunctionInfo(EnumCustomizeDiagnosisFunction.CustomizeDiagnosis,true,Assembly.GetExecutingAssembly().GetName().Version.ToString(),null, new List<EngineInfo>
- {
- new EngineInfo(EnumCustomizeDiagnosisEngineName.CustomizeDiagnosisEngine,EnumCustomizeDiagnosisEngineType.CustomizeDiagnosisEngine,true,versionInfo.Version,versionInfo.Company,null, _relatedDllList),
- }),
- };
- sampleCustomizeTask.Dispose();
- }
- catch
- {
- }
- }
- /// <summary>
- /// 初始化并加载SampleCustomizeTask
- /// </summary>
- /// <param name="usImageProvider">ImageProvider</param>
- /// <param name="detectMode">检测模式</param>
- /// <param name="detectTps">每秒图片吞吐量</param>
- /// <param name="intervalTime">间隔时间</param>
- /// <param name="modelFolder">模型文件夹路径,默认路径为AIDiagnosisSDK.dll所在目录下的子文件夹Network</param>
- public void Initialize(string modelFolder, IImageProvider usImageProvider, EnumDetectMode detectMode, int detectTps, int intervalTime)
- {
- _usImageProvider = usImageProvider;
- DetectMode = detectMode;
- ModelFolder = modelFolder;
- IntervalTime = intervalTime;
- DetectTps = detectTps;
- //Load SampleCustomizeTask.
- try
- {
- if (_sampleCustomizeTask == null)
- {
- _sampleCustomizeTask = new SampleCustomizeTask();
- _sampleCustomizeTask.NotifyLog += OnNotifyLog;
- _sampleCustomizeTask.NotifyProcessFinish += OnNotifyProcessFinish;
- _sampleCustomizeTask.LoadInferNet(modelFolder);
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"Load SampleCustomizeTask Failed!:{ex}");
- }
- }
- /// <summary>
- /// Start the Image Provider
- /// </summary>
- public void Start()
- {
- ResetParameters();
- if (_usImageProvider != null)
- {
- _usImageProvider.ImageProvided += OnUsImageProvided;
- _usImageProvider.Start();
- }
- }
- /// <summary>
- /// Stop the Image Provider
- /// </summary>
- public void Stop()
- {
- if (_usImageProvider != null)
- {
- _usImageProvider.ImageProvided -= OnUsImageProvided;
- _usImageProvider.Stop();
- }
- }
- /// <summary>
- /// Stop the Image Provider and Close the AutoVTI Diagnosis
- /// </summary>
- public void Close()
- {
- Stop();
- _handlingImageEvent.WaitOne();
- lock (_detectObject)
- {
- CloseDiagnosis();
- }
- }
- /// <summary>
- /// 检测单张图片
- /// </summary>
- public IDetectedObject[] DetectOneImage(ImageInputData imageData)
- {
- return HandleImage(false, imageData);
- }
- /// <summary>
- /// 检测单张图片
- /// </summary>
- public string DetectOneImageAsync(ImageInputData imageData)
- {
- return HandleImageAsync(imageData);
- }
- private void OnUsImageProvided(object sender, ImageInputData imageData)
- {
- OnUsImageChanged(imageData);
- }
- private void OnUsImageChanged(ImageInputData imageData)
- {
- if (DetectMode == EnumDetectMode.TimesPerSecond)///检测模式:每秒检测几次
- {//计算fps, 3秒检测一次
- if (_startTickCount == -1)
- {
- _startTickCount = Environment.TickCount;
- HandleImage(true, imageData);
- }
- else
- {
- var fpscurrentTickCount = Environment.TickCount;
- var timespan = fpscurrentTickCount - _startTickCount;
- if (timespan >= 3000)
- {
- _displayFps = _imageCounter * 1000 / timespan;
- _detectInterval = (int)Math.Round((float)_displayFps / DetectTps);
- _imageCounter = 0;
- _startTickCount = Environment.TickCount; ;
- }
- if (_detectInterval > 0)
- {
- if (_imageCounter % _detectInterval == 0)
- {
- HandleImage(true, imageData);
- }
- }
- }
- _imageCounter++;
- if (_imageCounter > 1000000)
- {
- _imageCounter = 0;
- }
- }
- else///检测模式:间隔固定时间检测
- {
- if (_startTickCount == -1)
- {
- _startTickCount = Environment.TickCount;
- HandleImage(true, imageData);
- }
- else
- {
- var currentTickCount = Environment.TickCount;
- if (IntervalTime <= currentTickCount - _startTickCount)
- {
- HandleImage(true, imageData);
- _startTickCount = Environment.TickCount;
- }
- }
- }
- }
- private IDetectedObject[] HandleImage(bool isImageProviderMode, ImageInputData imageData)
- {
- _handlingImageEvent.Reset();
- IDetectedObject[] diagResult = default;
- try
- {
- if (isImageProviderMode)
- {
- StartEvaluation?.Invoke(this, EventArgs.Empty);
- }
- lock (_detectObject)
- {
- diagResult = _sampleCustomizeTask.ProcessOneImage(imageData);
- }
- if (isImageProviderMode)
- {
- FinishEvaluation?.Invoke(this, diagResult);
- }
- }
- catch (Exception ex)
- {
- NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
- }
- finally
- {
- _handlingImageEvent.Set();
- }
- return diagResult;
- }
- private string HandleImageAsync(ImageInputData imageData)
- {
- _handlingImageEvent.Reset();
- string resultId = null;
- try
- {
- lock (_detectObject)
- {
- resultId = _sampleCustomizeTask.AddProcessRequest(imageData);
- }
- }
- catch (Exception ex)
- {
- NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
- }
- return resultId;
- }
- private void OnNotifyProcessFinish(object sender, KeyValuePair<string, IDetectedObject[]> e)
- {
- try
- {
- FinishEvaluation?.Invoke(e.Key, e.Value);
- }
- catch (Exception ex)
- {
- NotifyLog?.Invoke(this, new LogEventArgs(EnumLogType.ErrorLog, ex.ToString()));
- }
- finally
- {
- _handlingImageEvent.Set();
- }
- }
- private void OnNotifyLog(object sender, LogEventArgs e)
- {
- NotifyLog?.Invoke(this, e);
- }
- private void ResetParameters()
- {
- _startTickCount = -1;
- _imageCounter = 0;
- _displayFps = -1;
- _detectInterval = 0;
- }
- private void CloseDiagnosis()
- {
- if (_sampleCustomizeTask != null)
- {
- _sampleCustomizeTask.Dispose();
- _sampleCustomizeTask.NotifyLog -= OnNotifyLog;
- _sampleCustomizeTask.NotifyProcessFinish -= OnNotifyProcessFinish;
- _sampleCustomizeTask = null;
- }
- }
- }
- }
|