123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- using System;
- using System.Reflection;
- using System.Text.Json;
- using Vinno.AI.AmnioticFluidDiagnosisSDK.Interfaces;
- using Vinno.AI.AmnioticFluidDiagnosisSDK.Models;
- using Vinno.AI.CommonSDK.Enums;
- using Vinno.AI.CommonSDK.Interfaces;
- using Vinno.AI.CommonSDK.Models;
- using Vinno.AI.CommonSDK.Tools;
- namespace Vinno.AI.AmnioticFluidDiagnosisSDK
- {
- public class AmnioticFluidDiagnosis : IAmnioticFluidDiagnosis
- {
- private readonly IAmnioticFluidDiagnosisService _amnioticFluidDiagnosisService;
- private IAIImageProvider _imageProvider;
- private bool _initialized;
- private bool _disposed;
- /// <summary>
- /// Used For Detect One Image
- /// </summary>
- private PipeClient _singleImagePipeClient;
- /// <summary>
- /// Used For Provide Raw Image
- /// </summary>
- private PipeClient _rawImageProviderPipeClient;
- /// <summary>
- /// Used For Provide Byte Image
- /// </summary>
- private PipeClient _byteImageProviderPipeClient;
- /// <summary>
- /// Raised when the image evaluation is started.
- /// </summary>
- public event EventHandler StartEvaluationNotification;
- /// <summary>
- /// Raised when the image evaluation is finished.
- /// </summary>
- public event EventHandler<AIAmnioticFluidMeasurementResults> FinishEvaluationNotification;
- public AmnioticFluidDiagnosis()
- {
- _amnioticFluidDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAmnioticFluidDiagnosisService>();
- if (AIManager.Instance.AINotificationManager != null)
- {
- AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
- }
- _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisSingleImage);
- _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
- _singleImagePipeClient.Start();
- _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisRawImageProvider);
- _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
- _rawImageProviderPipeClient.Start();
- _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisByteImageProvider);
- _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
- _byteImageProviderPipeClient.Start();
- }
- /// <summary>
- /// 初始化AmnioticFluid Diagnosis
- /// </summary>
- /// <param name="amnioticFluidDiagnosisParameter">AmnioticFluid Diagnosis Parameter</param>
- /// <param name="imageProvider">Image Provider</param>
- public void Initialize(AmnioticFluidDiagnosisParameter amnioticFluidDiagnosisParameter, IAIImageProvider imageProvider = null)
- {
- try
- {
- if (!_initialized)
- {
- _imageProvider = imageProvider;
- _amnioticFluidDiagnosisService.Initialize(amnioticFluidDiagnosisParameter, _imageProvider != null);
- _initialized = true;
- }
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// Start Image Provider
- /// </summary>
- public void Start()
- {
- if (_imageProvider != null)
- {
- _amnioticFluidDiagnosisService.Start();
- _imageProvider.ByteImageProvided += OnByteImageProvided;
- _imageProvider.RawImageProvided += OnRawImageProvided;
- _imageProvider.Start();
- }
- }
- /// <summary>
- /// Stop Image Provider
- /// </summary>
- public void Stop()
- {
- if (_imageProvider != null)
- {
- _imageProvider.ByteImageProvided -= OnByteImageProvided;
- _imageProvider.RawImageProvided -= OnRawImageProvided;
- _imageProvider.Stop();
- }
- _amnioticFluidDiagnosisService.Stop();
- }
- private void OnLogMsgThrow(object sender, AILogEventArgs e)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(e);
- }
- private void OnNotificationReceived(object sender, AINotificationArgs e)
- {
- switch (e.NotificationType)
- {
- case AIEnumNotificationType.AmnioticFluidDiagnosisStartEvaluationRaised:
- StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
- break;
- case AIEnumNotificationType.AmnioticFluidDiagnosisFinishEvaluationRaised:
- var aiAmnioticFluidMeasurementResults = JsonSerializer.Deserialize<AIAmnioticFluidMeasurementResults>(e.Params?.ToString());
- FinishEvaluationNotification?.Invoke(this, aiAmnioticFluidMeasurementResults);
- break;
- case AIEnumNotificationType.AmnioticFluidDiagnosisNotifyLogRaised:
- var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params?.ToString());
- AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
- break;
- }
- }
- /// <summary>
- /// 设置每秒图片吞吐量
- /// </summary>
- /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
- public void SetDetectTps(int detectTps)
- {
- if (detectTps <= 0)
- {
- throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
- }
- try
- {
- _amnioticFluidDiagnosisService.SetDetectTps(detectTps);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置间隔时间
- /// </summary>
- /// <param name="intervalTime"></param>
- public void SetIntervalTime(int intervalTime)
- {
- try
- {
- _amnioticFluidDiagnosisService.SetIntervalTime(intervalTime);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置一个像素代表的实际物理距离是多少cm
- /// </summary>
- /// <param name="cmPerPixel"></param>
- public void SetCmPerPixel(float cmPerPixel)
- {
- try
- {
- _amnioticFluidDiagnosisService.SetCmPerPixel(cmPerPixel);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置检测模式
- /// </summary>
- /// <param name="detectMode"></param>
- public void SetDetectMode(AIEnumDetectMode detectMode)
- {
- try
- {
- _amnioticFluidDiagnosisService.SetDetectMode(detectMode);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 检测单张Raw Image
- /// </summary>
- /// <param name="rawImage">图像资源</param>
- /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
- /// <returns></returns>
- public AIAmnioticFluidMeasurementResults DetectOneImage(AIRawImage rawImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
- {
- if (rawImage == null)
- {
- throw new ArgumentNullException(nameof(rawImage));
- }
- if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
- {
- throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
- }
- try
- {
- var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
- var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
- _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
- return _amnioticFluidDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel, transUltrasoundImageRegion);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Byte Image
- /// </summary>
- /// <param name="byteImage">图像资源</param>
- /// <param name="cmPerPixel">设置一个像素代表的实际物理距离是多少cm</param>
- /// <returns></returns>
- public AIAmnioticFluidMeasurementResults DetectOneImage(byte[] byteImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
- {
- if (byteImage == null)
- {
- throw new ArgumentNullException(nameof(byteImage));
- }
- if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
- {
- throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
- }
- try
- {
- var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
- var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
- _singleImagePipeClient.SendBytes(byteImage);
- return _amnioticFluidDiagnosisService.DetectOneByteImage(cmPerPixel, transUltrasoundImageRegion);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 设置超声机图像信息(当使用ImageProvider时需设置)
- /// </summary>
- /// <param name="ultrasoundImageRegion"></param>
- public void SetUltrasoundImageRegionInfo(IAIUltrasoundImageRegion ultrasoundImageRegion)
- {
- if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
- {
- throw new ArgumentException("SetUltrasoundImageRegionInfo Only Support AIConvexArrayUltrasoundImageRegion Type");
- }
- try
- {
- var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
- var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
- _amnioticFluidDiagnosisService.SetProbeImageInfo(transUltrasoundImageRegion);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- private void OnByteImageProvided(object sender, byte[] byteImage)
- {
- try
- {
- if (byteImage == null)
- {
- return;
- }
- _byteImageProviderPipeClient?.SendBytes(byteImage);
- _amnioticFluidDiagnosisService.SendByteImageData();
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- private void OnRawImageProvided(object sender, AIRawImage rawImage)
- {
- try
- {
- if (rawImage == null)
- {
- return;
- }
- _rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
- _amnioticFluidDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// Close AmnioticFluid Diagnosis
- /// </summary>
- public void Close()
- {
- try
- {
- if (_disposed)
- {
- return;
- }
- _initialized = false;
- if (_imageProvider != null)
- {
- _imageProvider.ByteImageProvided -= OnByteImageProvided;
- _imageProvider.RawImageProvided -= OnRawImageProvided;
- _imageProvider.Stop();
- _imageProvider = null;
- }
- _amnioticFluidDiagnosisService.Close();
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- public void Dispose()
- {
- try
- {
- if (!_disposed)
- {
- _initialized = false;
- if (AIManager.Instance.AINotificationManager != null)
- {
- AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived;
- }
- if (_imageProvider != null)
- {
- _imageProvider.ByteImageProvided -= OnByteImageProvided;
- _imageProvider.RawImageProvided -= OnRawImageProvided;
- _imageProvider.Stop();
- _imageProvider = null;
- }
- if (_singleImagePipeClient != null)
- {
- _singleImagePipeClient.Dispose();
- _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
- _singleImagePipeClient = null;
- }
- if (_rawImageProviderPipeClient != null)
- {
- _rawImageProviderPipeClient.Dispose();
- _rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
- _rawImageProviderPipeClient = null;
- }
- if (_byteImageProviderPipeClient != null)
- {
- _byteImageProviderPipeClient.Dispose();
- _byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
- _byteImageProviderPipeClient = null;
- }
- _disposed = true;
- }
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- }
- }
|