using AForge.Video.DirectShow; using AI.DiagSystem; using AIDiagnosis.Common.Enums; using AIDiagnosisDemo.Settings; using Common.Client; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Windows; namespace AIDiagnosisDemo.ViewModel { public class SettingPageViewModel : Common.Client.ViewModel { private bool _isEnableAI; private Sourcetype _ultrasoundImageSourceType; private FilterInfo _ultrasoundImageSource; private bool _isDeviceSelected; private string _videoFilePath; private bool _cropImage; private int _detectTps; private EnumDetectMode _detectMode; private int _intervalTime; private bool _isIntervalTimeMode; private int _cpuUsed; private int _ramUsed; private bool _isSimulation; private IEnumerable _deviceSourceList; private EnumPerformance _performance; private bool _isShowOrgansContour; private EnumDisplayType _showLesions; private AIDiagnosisType _aiDiagnosisType; private bool _completeMyocardialImg; /// /// AI开关 /// public bool IsEnableAI { get { return _isEnableAI; } set { if (_isEnableAI != value) { _isEnableAI = value; OnPropertyChanged(() => IsEnableAI); } } } /// /// 超声图像来源类型 /// public IEnumerable UltrasoundImageSourceTypeList { get; private set; } /// /// 超声图像来源 /// public Sourcetype UltrasoundImageSourceType { get => _ultrasoundImageSourceType; set { if (_ultrasoundImageSourceType != value) { _ultrasoundImageSourceType = value; IsDeviceSelected = _ultrasoundImageSourceType == Sourcetype.VideoCaptureDevice; if (IsDeviceSelected) { DeviceSourceList = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType(); if (UltrasoundImageSource == null) { UltrasoundImageSource = DeviceSourceList.FirstOrDefault(); } } OnPropertyChanged(() => UltrasoundImageSourceType); } } } /// /// 图像采集设备被选中 /// public bool IsDeviceSelected { get => _isDeviceSelected; set { if (_isDeviceSelected != value) { _isDeviceSelected = value; OnPropertyChanged(() => IsDeviceSelected); } } } /// /// 按间隔时间检测模式被选中 /// public bool IsIntervalTimeMode { get => _isIntervalTimeMode; set { if (_isIntervalTimeMode != value) { _isIntervalTimeMode = value; OnPropertyChanged(() => IsIntervalTimeMode); } } } /// /// 图像文件路径 /// public string VideoFilePath { get => _videoFilePath; set { if (_videoFilePath != value) { _videoFilePath = value; OnPropertyChanged(() => VideoFilePath); } } } /// /// 图像源 /// public FilterInfo UltrasoundImageSource { get => _ultrasoundImageSource; set { if (_ultrasoundImageSource != value) { _ultrasoundImageSource = value; OnPropertyChanged(() => UltrasoundImageSource); } } } /// /// AI每秒频次 /// public int DetectTps { get => _detectTps; set { if (_detectTps != value) { _detectTps = value; OnPropertyChanged(() => DetectTps); } } } /// /// 图像裁切 /// public bool CropImage { get => _cropImage; set { if (_cropImage != value) { _cropImage = value; OnPropertyChanged(() => CropImage); } } } /// /// 采集设备清单 /// public IEnumerable DeviceSourceList { get => _deviceSourceList; private set { if (_deviceSourceList != value) { _deviceSourceList = value; OnPropertyChanged(() => DeviceSourceList); } } } /// /// 检测模式 /// public EnumDetectMode DetectMode { get => _detectMode; set { if (_detectMode != value) { _detectMode = value; if (_detectMode == EnumDetectMode.PeriodicIntervals) { IsIntervalTimeMode = true; } else { IsIntervalTimeMode = false; } OnPropertyChanged(() => DetectMode); } } } /// /// 间隔时间 /// public int IntervalTime { get => _intervalTime; set { if (_intervalTime != value) { _intervalTime = value; OnPropertyChanged(() => IntervalTime); } } } /// /// CPU占用 /// public int CPUUsed { get => _cpuUsed; set { if (_cpuUsed != value) { _cpuUsed = value; OnPropertyChanged(() => CPUUsed); } } } /// /// 内存占用 /// public int RAMUsed { get => _ramUsed; set { if (_ramUsed != value) { _ramUsed = value; OnPropertyChanged(() => RAMUsed); } } } /// /// 是否开启模拟占用CPU与内存 /// public bool IsSimulation { get => _isSimulation; set { if (_isSimulation != value) { _isSimulation = value; OnPropertyChanged(() => IsSimulation); } } } /// /// 性能 /// public EnumPerformance Performance { get => _performance; set { if (_performance != value) { _performance = value; OnPropertyChanged(() => Performance); } } } /// /// 是否显示脏器 /// public bool IsShowOrgansContour { get => _isShowOrgansContour; set { if (_isShowOrgansContour != value) { _isShowOrgansContour = value; OnPropertyChanged(() => IsShowOrgansContour); } } } /// /// 显示病灶的方式 /// public EnumDisplayType ShowLesions { get => _showLesions; set { if (_showLesions != value) { _showLesions = value; OnPropertyChanged(() => ShowLesions); } } } /// /// 选择AI SDK /// public AIDiagnosisType AIDiagnosisType { get => _aiDiagnosisType; set { if (_aiDiagnosisType != value) { _aiDiagnosisType = value; OnPropertyChanged(() => AIDiagnosisType); } } } public ButtonCommand BrowseCommand { get; } public bool CompleteMyocardialImg { get => _completeMyocardialImg; set { if (_completeMyocardialImg != value) { _completeMyocardialImg = value; OnPropertyChanged(() => CompleteMyocardialImg); } } } public SettingPageViewModel() { BrowseCommand = new ButtonCommand(OnBrowse); DeviceSourceList = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType(); UltrasoundImageSourceTypeList = new List { Sourcetype.VideoCaptureDevice, Sourcetype.PictureOrVideoFile }; LoadSettings(); } private void OnBrowse(object args) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "图像或视频文件|*.png;*.bmp;*.jpg;*.jpeg;*.mp4;*.avi", Multiselect = false, Title = "选择一个图像或视频" }; if (openFileDialog.ShowDialog() ?? false) { VideoFilePath = openFileDialog.FileName; } } private void LoadSettings() { IsEnableAI = SettingConfig.Instance.IsEnableAI; UltrasoundImageSourceType = SettingConfig.Instance.UltrasoundImageSourceType; CropImage = SettingConfig.Instance.CropImage; DetectTps = SettingConfig.Instance.DetectTps; IntervalTime = SettingConfig.Instance.IntervalTime; DetectMode = SettingConfig.Instance.DetectMode; IsDeviceSelected = _ultrasoundImageSourceType == Sourcetype.VideoCaptureDevice; if (IsDeviceSelected) { UltrasoundImageSource = DeviceSourceList.FirstOrDefault(s => s.MonikerString == SettingConfig.Instance.UltrasoundImageSource); if (UltrasoundImageSource == null) { UltrasoundImageSource = DeviceSourceList.FirstOrDefault(); } } else { VideoFilePath = SettingConfig.Instance.UltrasoundImageSource; } IsSimulation = SettingConfig.Instance.IsSimulation; CPUUsed = SettingConfig.Instance.CPUUsed; RAMUsed = SettingConfig.Instance.RAMUsed; Performance = SettingConfig.Instance.Performance; IsShowOrgansContour = SettingConfig.Instance.IsShowOrgansContour; ShowLesions = SettingConfig.Instance.DisplayType; AIDiagnosisType = SettingConfig.Instance.AIDiagnosisType; CompleteMyocardialImg = SettingConfig.Instance.CompleteMyocardialImg; } public bool Save() { try { if (IntervalTime < 0) { MessageBox.Show("IntervalTime must be >=0"); return false; } SettingConfig.PreInstance.UltrasoundImageSourceType = UltrasoundImageSourceType; SettingConfig.PreInstance.UltrasoundImageSource = UltrasoundImageSourceType == Sourcetype.VideoCaptureDevice ? UltrasoundImageSource?.MonikerString : VideoFilePath; SettingConfig.PreInstance.IsEnableAI = IsEnableAI; SettingConfig.PreInstance.Performance = Performance; SettingConfig.PreInstance.CropImage = CropImage; SettingConfig.PreInstance.DetectTps = DetectTps; SettingConfig.PreInstance.DetectMode = DetectMode; SettingConfig.PreInstance.IntervalTime = IntervalTime; SettingConfig.PreInstance.RAMUsed = RAMUsed; SettingConfig.PreInstance.CPUUsed = CPUUsed; SettingConfig.PreInstance.IsSimulation = IsSimulation; SettingConfig.PreInstance.IsShowOrgansContour = IsShowOrgansContour; SettingConfig.PreInstance.DisplayType = ShowLesions; SettingConfig.PreInstance.AIDiagnosisType = AIDiagnosisType; SettingConfig.PreInstance.CompleteMyocardialImg = CompleteMyocardialImg; SettingConfig.PreInstance.Save(); return true; } catch (Exception ex) { MessageBox.Show($"OnSave command exception: {ex}"); return false; } } } }