using AI.DiagSystem; using AIDiagnosis.Common.Enums; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace AIDiagnosisDemo.Settings { public enum AIDiagnosisType { Normal, Myocardial, MiceMyocardial, RatMyocardial, ThyroidClassification, OrganIdentification } public class SettingConfig { public event EventHandler> ValueChangedEvent; public static SettingConfig Instance { get; private set; } public static SettingConfig PreInstance { get; private set; } /// /// CPU占用 /// public int CPUUsed { get; set; } /// /// 图像裁剪 /// public bool CropImage { get; set; } /// /// 检测模式 /// public EnumDetectMode DetectMode { get; set; } /// /// 检测间隔 /// public int DetectTps { get; set; } /// /// 间隔时间 /// public int IntervalTime { get; set; } /// /// AI开关 /// public bool IsEnableAI { get; set; } /// /// 模拟占用模式是否开启 /// public bool IsSimulation { get; set; } /// /// 是否是视频 /// public bool IsVideo { get { if (UltrasoundImageSourceType == Sourcetype.PictureOrVideoFile) { var fileExtension = Path.GetExtension(UltrasoundImageSource); if (fileExtension == ".mp4" || fileExtension == ".avi") { return true; } else { return false; } } else { return true; } } } /// /// AI性能 /// public EnumPerformance Performance { get; set; } /// /// 内存占用 /// public int RAMUsed { get; set; } /// /// 超声图像来源 /// public string UltrasoundImageSource { get; set; } /// /// 超声图像来源类型 /// public Sourcetype UltrasoundImageSourceType { get; set; } /// /// 显示方式 /// public EnumDisplayType DisplayType { get; set; } /// /// 是否显示脏器轮廓 /// public bool IsShowOrgansContour { get; set; } /// /// 是否播放循环TestImages文件夹内单帧图 /// public bool IsCycleImagesPlay { get; set; } /// /// AI计算类型,按理来说应该全部合成到AIDiagnosisSDK,但是有一些AI组并没有合成到AIDiagSystemLib, 有些数据结构名字用途一样但是属于不同的dll,所以目前没合成的单独创建SDK project并引入。 /// 后期AI组会统一合成到AIDiagSystemLib.dll. /// public AIDiagnosisType AIDiagnosisType { get; set; } /// /// 是否对心肌圆形轮廓进行补全 /// public bool CompleteMyocardialImg { get; set; } static SettingConfig() { if (!LoadConfig()) { Instance = new SettingConfig { UltrasoundImageSourceType = Sourcetype.PictureOrVideoFile, UltrasoundImageSource = null, IsEnableAI = true, Performance = EnumPerformance.Low, DetectTps = 2, CropImage = false, DetectMode = EnumDetectMode.PeriodicIntervals, IntervalTime = 1000, IsShowOrgansContour = true, DisplayType = EnumDisplayType.Border, IsSimulation = true, RAMUsed = 0, CPUUsed = 0, IsCycleImagesPlay = false, AIDiagnosisType = AIDiagnosisType.Normal }; PreInstance = new SettingConfig { UltrasoundImageSourceType = Sourcetype.PictureOrVideoFile, UltrasoundImageSource = null, IsEnableAI = true, Performance = EnumPerformance.Low, DetectTps = 2, CropImage = false, DetectMode = EnumDetectMode.PeriodicIntervals, IntervalTime = 1000, IsShowOrgansContour = true, DisplayType = EnumDisplayType.Border, IsSimulation = true, RAMUsed = 0, CPUUsed = 0, IsCycleImagesPlay = false, AIDiagnosisType = AIDiagnosisType.Normal }; } } private static bool LoadConfig() { var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json"); if (!File.Exists(settingFilePath)) { return false; } try { var jsonString = File.ReadAllText(settingFilePath); Instance = JsonConvert.DeserializeObject(jsonString); PreInstance = JsonConvert.DeserializeObject(jsonString); return true; } catch { } return false; } public void RaiseSavedEvent(List changeList) { Instance.ValueChangedEvent?.Invoke(this, changeList); } public async void Save() { var changeList = new List(); if (Instance.UltrasoundImageSource != UltrasoundImageSource) { Instance.UltrasoundImageSource = UltrasoundImageSource; changeList.Add(SettingProperty.UltrasoundImageSource); } if (Instance.UltrasoundImageSourceType != UltrasoundImageSourceType) { Instance.UltrasoundImageSourceType = UltrasoundImageSourceType; changeList.Add(SettingProperty.UltrasoundImageSourceType); } if (Instance.IsEnableAI != IsEnableAI) { Instance.IsEnableAI = IsEnableAI; changeList.Add(SettingProperty.IsEnableAI); } if (Instance.Performance != Performance) { Instance.Performance = Performance; changeList.Add(SettingProperty.Performance); } if (Instance.CropImage != CropImage) { Instance.CropImage = CropImage; changeList.Add(SettingProperty.CropImage); } if (Instance.DetectTps != DetectTps) { Instance.DetectTps = DetectTps; changeList.Add(SettingProperty.DetectTps); } if (Instance.DetectMode != DetectMode) { Instance.DetectMode = DetectMode; changeList.Add(SettingProperty.DetectMode); } if (Instance.IntervalTime != IntervalTime) { Instance.IntervalTime = IntervalTime; changeList.Add(SettingProperty.IntervalTime); } if (Instance.RAMUsed != RAMUsed) { Instance.RAMUsed = RAMUsed; changeList.Add(SettingProperty.RAMUsed); } if (Instance.CPUUsed != CPUUsed) { Instance.CPUUsed = CPUUsed; changeList.Add(SettingProperty.CPUUsed); } if (Instance.IsSimulation != IsSimulation) { Instance.IsSimulation = IsSimulation; changeList.Add(SettingProperty.IsSimulation); } if (Instance.DisplayType != DisplayType) { Instance.DisplayType = DisplayType; changeList.Add(SettingProperty.DisplayType); } if (Instance.IsShowOrgansContour != IsShowOrgansContour) { Instance.IsShowOrgansContour = IsShowOrgansContour; changeList.Add(SettingProperty.IsShowOrgansContour); } if (Instance.AIDiagnosisType != AIDiagnosisType) { Instance.AIDiagnosisType = AIDiagnosisType; changeList.Add(SettingProperty.AIDiagnosisType); } if (Instance.CompleteMyocardialImg != CompleteMyocardialImg) { Instance.CompleteMyocardialImg = CompleteMyocardialImg; changeList.Add(SettingProperty.AIDiagnosisType); } var settingFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting"); var settingFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Setting", "Setting.json"); var jsonString = JsonConvert.SerializeObject(Instance); if (!Directory.Exists(settingFolderPath)) { Directory.CreateDirectory(settingFolderPath); } File.WriteAllText(settingFilePath, jsonString); if (changeList.Count > 0) { await Task.Run(() => { Instance.ValueChangedEvent?.Invoke(this, changeList); }); } } } }