123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- using AI.Common;
- using AI.Common.Log;
- using AI.DiagSystem;
- using AIDiagnosis.Common.Enums;
- using AIDiagnosis.Common.Models;
- using AIDiagnosisSDK.Enums;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using Vinno.AI.CommonSDK.Enums;
- using Vinno.AI.CommonSDK.Models;
- using Vinno.AI.CommonSDK.Tools;
- using Vinno.AI.OrganIdentificationSDK.Interfaces;
- using Vinno.AI.OrganIdentificationSDK.Models;
- using Vinno.AI.OrganIdentificationService.Tools;
- using Vinno.AI.Service.Common.Interfaces;
- using Vinno.AI.Service.Common.Models;
- using Vinno.AI.Service.Common.Tools;
- namespace Vinno.AI.OrganIdentificationService
- {
- public class OrganIdentificationService : IOrganIdentificationService, IDisposable, IEngine
- {
- private readonly ConcurrentStack<byte[]> _singleImageCache;
- private readonly string _modelFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- private PipeServer _singleImagePipeServer;
- private ImageProvider _imageProvider;
- private AIDiagnosisSDK.AIDiagnosis _organIndentification;
- private bool _disposed;
- public static List<FunctionInfo> AllFunctions { get; }
- static OrganIdentificationService()
- {
- AllFunctions = AIDiagnosisSDK.AIDiagnosis.AllFunctions.Where(x => x.FunctionName == EnumAIDiagnosisFunction.OrganIdentification.ToString()).ToList();
- }
- public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
- {
- AIDiagnosisSDK.AIDiagnosis.InitForOrganIdentification(enableFunction, currentUsedFunction);
- }
- public OrganIdentificationService()
- {
- try
- {
- _singleImageCache = new ConcurrentStack<byte[]>();
- _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForOrganIdentificationSingleImage);
- _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
- _singleImagePipeServer.DataReceived += OnDataReceived;
- _singleImagePipeServer.StartAndReceive();
- _imageProvider = new ImageProvider(AIEnumType.OrganIdentification);
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Constructor error:{ex}");
- }
- }
- private void OnLogMsgThrow(object sender, AILogEventArgs e)
- {
- if (e == null)
- {
- return;
- }
- switch (e.LogType)
- {
- case AIEnumLogType.ErrorLog:
- case AIEnumLogType.FatalLog:
- Logger.Error(e.Msg);
- break;
- case AIEnumLogType.WarnLog:
- Logger.Warning(e.Msg);
- break;
- case AIEnumLogType.InfoLog:
- default:
- Logger.Info(e.Msg);
- break;
- }
- }
- public void Initialize(OrganIdentificationParameter organIndentificationParameter, bool hasImageProvider)
- {
- try
- {
- CloseDiagnosis();
- var performance = (EnumPerformance)organIndentificationParameter.Performance;
- var detectMode = (EnumDetectMode)organIndentificationParameter.DetectMode;
- var isCropped = organIndentificationParameter.IsCropped;
- var detectTps = organIndentificationParameter.DetectTps;
- var intervalTime = organIndentificationParameter.IntervalTime;
- _organIndentification = new AIDiagnosisSDK.AIDiagnosis(EnumAIModule.OrganIdentification, Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, performance, detectMode, isCropped, detectTps, intervalTime, _modelFolder, false, false, Setting.Instance.CorrectAIResults, Setting.Instance.IsIntelligentIdentificationMode, Setting.Instance.IntelligentIdentificationThreshold, Setting.Instance.IntelligentIdentificationTimeRange, false);
- _organIndentification.StartEvaluation += OnStartEvaluation;
- _organIndentification.FinishEvaluation += OnFinishEvaluation;
- _organIndentification.NotifyError += OnNotifyError;
- _organIndentification.NotifyLog += OnNotifyLog;
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Initialize error:{ex}");
- }
- }
- /// <summary>
- /// 切换引擎
- /// </summary>
- /// <param name="functionInfo"></param>
- public void SwitchEngines(FunctionInfo functionInfo)
- {
- try
- {
- AIDiagnosisSDK.AIDiagnosis.SwitchAIEngines(functionInfo);
- if (_organIndentification != null)
- {
- _organIndentification.SwitchAIEngines2(functionInfo);
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SwitchEngines error:{ex}");
- }
- }
- public void Start()
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.Start();
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Start error:{ex}");
- }
- }
- public void Stop()
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.Stop();
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Stop error:{ex}");
- }
- }
- private void OnStartEvaluation(object sender, EventArgs e)
- {
- NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.OrganIndentificationStartEvaluationRaised, Params = e });
- }
- private void OnFinishEvaluation(object sender, global::AI.DiagSystem.AIDiagResultPerImg e)
- {
- var transAIDiagresultPerImg = AIConvertHelper.ConvertAIDiagResultPerImgToTransAIDiagResultPerImg(e);
- NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.OrganIndentificationFinishEvaluationRaised, Params = transAIDiagresultPerImg });
- }
- private void OnNotifyError(object sender, ErrorEventArgs e)
- {
- Logger.Error($"OrganIdentificationService OnNotifyError:{e.GetException()}");
- var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
- NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.OrganIndentificationNotifyLogRaised, Params = logEventArgs });
- }
- private void OnNotifyLog(object sender, LogEventArgs e)
- {
- if (e == null)
- {
- return;
- }
- switch (e.LogType)
- {
- case EnumLogType.InfoLog:
- Logger.Info($"OrganIdentificationService OnNotifyLog:{e.Msg}");
- break;
- case EnumLogType.WarnLog:
- Logger.Warning($"OrganIdentificationService OnNotifyLog:{e.Msg}");
- break;
- case EnumLogType.ErrorLog:
- case EnumLogType.FatalLog:
- default:
- Logger.Error($"OrganIdentificationService OnNotifyLog:{e.Msg}");
- break;
- }
- var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
- NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.OrganIndentificationNotifyLogRaised, Params = logEventArgs });
- }
- public TransAIDiagResultPerImg DetectOneByteImage()
- {
- try
- {
- byte[] byteImage = null;
- int retryCount = 0;
- while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
- {
- Thread.Sleep(10);
- retryCount++;
- }
- if (byteImage == null)
- {
- Logger.Error($"OrganIdentificationService-DetectOneByteImage Get Image Cache Fail");
- return null;
- }
- if (_organIndentification == null)
- {
- Logger.Error($"Detect One Byte Image Error:Organ Indentification is null");
- return null;
- }
- var result = _organIndentification?.DetectOneImage(byteImage);
- return AIConvertHelper.ConvertAIDiagResultPerImgToTransAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-DetectOneByteImage error:{ex}");
- return null;
- }
- }
- public TransAIDiagResultPerImg DetectOneRawImage(int height, int width, AIEnumColorType colorType)
- {
- try
- {
- byte[] byteImage = null;
- int retryCount = 0;
- while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
- {
- Thread.Sleep(10);
- retryCount++;
- }
- if (byteImage == null)
- {
- Logger.Error($"OrganIdentificationService-DetectOneRawImage Get Image Cache Fail");
- return null;
- }
- if (_organIndentification == null)
- {
- Logger.Error($"Detect One Byte Image Error:Organ Indentification is null");
- return null;
- }
- var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
- var result = _organIndentification?.DetectOneImage(rawImage);
- return AIConvertHelper.ConvertAIDiagResultPerImgToTransAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-DetectOneRawImage error:{ex}");
- return null;
- }
- }
- public void SetIsCropped(bool isCropped)
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.IsCropped = isCropped;
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SetIsCropped error:{ex}");
- }
- }
- public void SetDetectTps(int detectTps)
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.DetectTps = detectTps;
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SetDetectTps error:{ex}");
- }
- }
- public void SetIntervalTime(int intervalTime)
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.IntervalTime = intervalTime;
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SetIntervalTime error:{ex}");
- }
- }
- public void SetDetectMode(AIEnumDetectMode detectMode)
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.DetectMode = (EnumDetectMode)detectMode;
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SetDetectMode error:{ex}");
- }
- }
- public void Close()
- {
- try
- {
- Logger.Info($"OrganIdentificationService-Close Invoke");
- CloseDiagnosis();
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Close error:{ex}");
- }
- }
- private void CloseDiagnosis()
- {
- if (_organIndentification != null)
- {
- _organIndentification.StartEvaluation -= OnStartEvaluation;
- _organIndentification.FinishEvaluation -= OnFinishEvaluation;
- _organIndentification.NotifyError -= OnNotifyError;
- _organIndentification.NotifyLog -= OnNotifyLog;
- _organIndentification.Close();
- _organIndentification = null;
- }
- }
- public void Dispose()
- {
- try
- {
- if (!_disposed)
- {
- Logger.Info($"OrganIdentificationService-Start Dispose");
- CloseDiagnosis();
- if (_singleImagePipeServer != null)
- {
- _singleImagePipeServer.DataReceived -= OnDataReceived;
- _singleImagePipeServer.Dispose();
- _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
- _singleImagePipeServer = null;
- }
- _imageProvider.Dispose();
- _imageProvider = null;
- _disposed = true;
- Logger.Info($"OrganIdentificationService Dispose End");
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-Dispose error:{ex}");
- }
- }
- /// <summary>
- /// Send Raw Image Data For Pipe
- /// </summary>
- /// <param name="height"></param>
- /// <param name="width"></param>
- /// <param name="colorType"></param>
- public void SendRawImageData(int height, int width, AIEnumColorType colorType)
- {
- try
- {
- _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SendRawImageData error:{ex}");
- }
- }
- /// <summary>
- /// Send Byte Image Data For Pipe
- /// </summary>
- public void SendByteImageData()
- {
- try
- {
- _imageProvider?.ReceiveByteImageData();
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-SendByteImageData error:{ex}");
- }
- }
- /// <summary>
- /// 清除脏器识别的缓存
- /// </summary>
- public void ClearOrganIdentificationCache()
- {
- try
- {
- if (_organIndentification != null)
- {
- _organIndentification.ClearOrganIdentificationCache();
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-ClearOrganIdentificationCache error:{ex}");
- }
- }
- private void OnDataReceived(object sender, byte[] e)
- {
- try
- {
- _singleImageCache.Push(e);
- }
- catch (Exception ex)
- {
- Logger.Error($"OrganIdentificationService-OnDataReceived error:{ex}");
- }
- }
- }
- }
|