123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- using System;
- using System.Reflection;
- using System.Text.Json;
- using Vinno.AI.AIDiagnosisSDK.Interfaces;
- using Vinno.AI.AIDiagnosisSDK.Models;
- using Vinno.AI.CommonSDK.Enums;
- using Vinno.AI.CommonSDK.Interfaces;
- using Vinno.AI.CommonSDK.Models;
- using Vinno.AI.CommonSDK.Tools;
- namespace Vinno.AI.AIDiagnosisSDK
- {
- public class BreastDiagnosis : IAIDiagnosis
- {
- private readonly IAIDiagnosisService _aiDiagnosisService;
- private readonly AIEnumModuleType _type = AIEnumModuleType.Breast;
- 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<AIDiagResultPerImg> FinishEvaluationNotification;
- public BreastDiagnosis()
- {
- _aiDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService<IAIDiagnosisService>();
- if (AIManager.Instance.AINotificationManager != null)
- {
- AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
- }
- _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForBreastDiagnosisSingleImage);
- _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
- _singleImagePipeClient.Start();
- _rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForBreastDiagnosisRawImageProvider);
- _rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
- _rawImageProviderPipeClient.Start();
- _byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForBreastDiagnosisByteImageProvider);
- _byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
- _byteImageProviderPipeClient.Start();
- }
- /// <summary>
- /// 初始化AI Diagnosis
- /// </summary>
- /// <param name="BreastDiagnosisParameter">AI Diagnosis Parameter</param>
- /// <param name="imageProvider">Image Provider</param>
- public void Initialize(AIDiagnosisParameter BreastDiagnosisParameter, IAIImageProvider imageProvider = null)
- {
- try
- {
- if (!_initialized)
- {
- _imageProvider = imageProvider;
- _aiDiagnosisService.InitializeBreastDiagnosis(BreastDiagnosisParameter, _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)
- {
- _imageProvider.ByteImageProvided += OnByteImageProvided;
- _imageProvider.RawImageProvided += OnRawImageProvided;
- _imageProvider.Start();
- _aiDiagnosisService.Start();
- }
- }
- /// <summary>
- /// Stop Image Provider
- /// </summary>
- public void Stop()
- {
- if (_imageProvider != null)
- {
- _imageProvider.ByteImageProvided -= OnByteImageProvided;
- _imageProvider.RawImageProvided -= OnRawImageProvided;
- _imageProvider.Stop();
- }
- _aiDiagnosisService.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.BreastDiagnosisStartEvaluationRaised:
- StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
- break;
- case AIEnumNotificationType.BreastDiagnosisFinishEvaluationRaised:
- var transAIDiagResultPerImg = JsonSerializer.Deserialize<TransAIDiagResultPerImg>(e.Params?.ToString());
- var aiDiagResultPerImg = AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(transAIDiagResultPerImg);
- FinishEvaluationNotification?.Invoke(this, aiDiagResultPerImg);
- break;
- case AIEnumNotificationType.AIDiagnosisNotifyLogRaised:
- var logEventArgs = JsonSerializer.Deserialize<AILogEventArgs>(e.Params.ToString());
- AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs);
- break;
- }
- }
- /// <summary>
- /// 设置图片是否已裁剪
- /// </summary>
- /// <param name="isCropped"></param>
- public void SetIsCropped(bool isCropped)
- {
- try
- {
- _aiDiagnosisService.SetIsCropped(_type, isCropped);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置每秒图片吞吐量
- /// </summary>
- /// <param name="detectTps">每秒图片吞吐量,必须大于0</param>
- public void SetDetectTps(int detectTps)
- {
- if (detectTps <= 0)
- {
- throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}");
- }
- try
- {
- _aiDiagnosisService.SetDetectTps(_type, 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
- {
- _aiDiagnosisService.SetIntervalTime(_type, intervalTime);
- }
- 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
- {
- _aiDiagnosisService.SetDetectMode(_type, detectMode);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置是否启用轮廓计算
- /// </summary>
- /// <param name="enableLesionSeg"></param>
- public void SetEnableLesionSeg(bool enableLesionSeg)
- {
- try
- {
- _aiDiagnosisService.SetEnableLesionSeg(_type, enableLesionSeg);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置是否启用描述
- /// </summary>
- /// <param name="enableDescription">是否启用病灶及脏器描述</param>
- public void SetEnableDescription(bool enableDescription)
- {
- try
- {
- _aiDiagnosisService.SetEnableDescription(_type, enableDescription, enableDescription);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 设置是否启用描述
- /// </summary>
- /// <param name="enableLessionDescription">是否启用病灶描述</param>
- /// <param name="enableOrganDescription">是否启用脏器描述</param>
- public void SetEnableDescription(bool enableLessionDescription, bool enableOrganDescription)
- {
- try
- {
- _aiDiagnosisService.SetEnableDescription(_type, enableLessionDescription, enableOrganDescription);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- }
- }
- /// <summary>
- /// 检测单张Byte Image
- /// </summary>
- /// <param name="byteImage"></param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(byte[] byteImage)
- {
- if (byteImage == null)
- {
- throw new ArgumentNullException(nameof(byteImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(byteImage);
- var result = _aiDiagnosisService.DetectOneByteImage(_type);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Byte Image,其中enableLesionSeg与enableDescription仅此次调用生效
- /// </summary>
- /// <param name="byteImage"></param>
- /// <param name="enableLesionSeg">是否计算轮廓</param>
- /// <param name="enableDescription">是否计算病灶及脏器描述结果</param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(byte[] byteImage, bool enableLesionSeg, bool enableDescription)
- {
- if (byteImage == null)
- {
- throw new ArgumentNullException(nameof(byteImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(byteImage);
- var result = _aiDiagnosisService.DetectOneByteImageWithTempSetting(_type, enableLesionSeg, enableDescription, enableDescription);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Byte Image,其中enableLesionSeg与enableLesionDescription,enableOrganDescription仅此次调用生效
- /// </summary>
- /// <param name="byteImage"></param>
- /// <param name="enableLesionSeg">是否计算轮廓</param>
- /// <param name="enableLesionDescription">是否计算病灶描述结果</param>
- /// <param name="enableOrganDescription">是否计算脏器描述结果</param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(byte[] byteImage, bool enableLesionSeg, bool enableLesionDescription, bool enableOrganDescription)
- {
- if (byteImage == null)
- {
- throw new ArgumentNullException(nameof(byteImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(byteImage);
- var result = _aiDiagnosisService.DetectOneByteImageWithTempSetting(_type, enableLesionSeg, enableLesionDescription, enableOrganDescription);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Raw Image
- /// </summary>
- /// <param name="rawImage"></param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(AIRawImage rawImage)
- {
- if (rawImage == null)
- {
- throw new ArgumentNullException(nameof(rawImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
- var result = _aiDiagnosisService.DetectOneRawImage(_type, rawImage.Height, rawImage.Width, rawImage.ColorType);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Raw Image,其中enableLesionSeg与enableDescription仅此次调用生效
- /// </summary>
- /// <param name="rawImage"></param>
- /// <param name="enableLesionSeg">是否计算轮廓</param>
- /// <param name="enableDescription">是否计算病灶及脏器描述结果</param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(AIRawImage rawImage, bool enableLesionSeg, bool enableDescription)
- {
- if (rawImage == null)
- {
- throw new ArgumentNullException(nameof(rawImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
- var result = _aiDiagnosisService.DetectOneRawImageWithTempSetting(_type, rawImage.Height, rawImage.Width, rawImage.ColorType, enableLesionSeg, enableDescription, enableDescription);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 检测单张Raw Image,其中enableLesionSeg与enableLesionDescription,enableOrganDescription仅此次调用生效
- /// </summary>
- /// <param name="rawImage"></param>
- /// <param name="enableLesionSeg">是否计算轮廓</param>
- /// <param name="enableLesionDescription">是否计算病灶描述结果</param>
- /// <param name="enableOrganDescription">是否计算脏器描述结果</param>
- /// <returns></returns>
- public AIDiagResultPerImg DetectOneImage(AIRawImage rawImage, bool enableLesionSeg, bool enableLesionDescription, bool enableOrganDescription = false)
- {
- if (rawImage == null)
- {
- throw new ArgumentNullException(nameof(rawImage));
- }
- try
- {
- _singleImagePipeClient.SendBytes(rawImage.DataBuffer);
- var result = _aiDiagnosisService.DetectOneRawImageWithTempSetting(_type, rawImage.Height, rawImage.Width, rawImage.ColorType, enableLesionSeg, enableLesionDescription, enableOrganDescription);
- return AICommonConvertHelper.ConvertTransAIDiagResultPerImgToAIDiagResultPerImg(result);
- }
- catch (Exception ex)
- {
- AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
- return null;
- }
- }
- /// <summary>
- /// 评估单幅图像 ,图像计算完成后,使用者希望调整结果,使部分结果在选定的矩形框内。
- /// </summary>
- /// <param name="aiRect">使用者给出的矩形框</param>
- /// <param name="aiDiagResultPerImg">图像的初始计算结果</param>
- /// <summary>
- public AIDiagResultPerImg CalculateCarotidInnerDiameterAndIntimaMediaThicknessInSpecifiedRect(byte[] bytesImage, AIRect aiRect, AIDiagResultPerImg aiDiagResultPerImg)
- {
- throw new NotSupportedException("CalculateCarotidInnerDiameterAndIntimaMediaThicknessInSpecifiedRectWithBytesImage Only Support Cartoid Diagnosis");
- }
- /// <summary>
- /// 评估单幅图像 ,图像计算完成后,使用者希望调整结果,使部分结果在选定的矩形框内。
- /// </summary>
- /// <param name="aiRect">使用者给出的矩形框</param>
- /// <param name="aiDiagResultPerImg">图像的初始计算结果</param>
- public AIDiagResultPerImg CalculateCarotidInnerDiameterAndIntimaMediaThicknessInSpecifiedRect(AIRawImage rawImage, AIRect aiRect, AIDiagResultPerImg aiDiagResultPerImg)
- {
- throw new NotSupportedException("CalculateCarotidInnerDiameterAndIntimaMediaThicknessInSpecifiedRectWithBytesImage Only Support Cartoid Diagnosis");
- }
- private void OnByteImageProvided(object sender, byte[] byteImage)
- {
- try
- {
- if (byteImage == null)
- {
- return;
- }
- _byteImageProviderPipeClient?.SendBytes(byteImage);
- _aiDiagnosisService.SendByteImageData(_type);
- }
- 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);
- _aiDiagnosisService.SendRawImageData(_type, 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 AI Diagnosis
- /// </summary>
- public void Close()
- {
- try
- {
- if (_disposed)
- {
- return;
- }
- _initialized = false;
- if (_imageProvider != null)
- {
- _imageProvider.ByteImageProvided -= OnByteImageProvided;
- _imageProvider.RawImageProvided -= OnRawImageProvided;
- _imageProvider.Stop();
- _imageProvider = null;
- }
- _aiDiagnosisService.Close(_type);
- }
- 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}"));
- }
- }
- }
- }
|