SettingPageViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using AForge.Video.DirectShow;
  2. using AI.DiagSystem;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosisDemo.Settings;
  5. using Common.Client;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Windows;
  11. namespace AIDiagnosisDemo.ViewModel
  12. {
  13. public class SettingPageViewModel : Common.Client.ViewModel
  14. {
  15. private bool _isEnableAI;
  16. private Sourcetype _ultrasoundImageSourceType;
  17. private FilterInfo _ultrasoundImageSource;
  18. private bool _isDeviceSelected;
  19. private string _videoFilePath;
  20. private bool _cropImage;
  21. private int _detectTps;
  22. private EnumDetectMode _detectMode;
  23. private int _intervalTime;
  24. private bool _isIntervalTimeMode;
  25. private int _cpuUsed;
  26. private int _ramUsed;
  27. private bool _isSimulation;
  28. private IEnumerable<FilterInfo> _deviceSourceList;
  29. private EnumPerformance _performance;
  30. private bool _isShowOrgansContour;
  31. private EnumDisplayType _showLesions;
  32. private AIDiagnosisType _aiDiagnosisType;
  33. private bool _completeMyocardialImg;
  34. /// <summary>
  35. /// AI开关
  36. /// </summary>
  37. public bool IsEnableAI
  38. {
  39. get { return _isEnableAI; }
  40. set
  41. {
  42. if (_isEnableAI != value)
  43. {
  44. _isEnableAI = value;
  45. OnPropertyChanged(() => IsEnableAI);
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// 超声图像来源类型
  51. /// </summary>
  52. public IEnumerable<Sourcetype> UltrasoundImageSourceTypeList
  53. {
  54. get; private set;
  55. }
  56. /// <summary>
  57. /// 超声图像来源
  58. /// </summary>
  59. public Sourcetype UltrasoundImageSourceType
  60. {
  61. get => _ultrasoundImageSourceType;
  62. set
  63. {
  64. if (_ultrasoundImageSourceType != value)
  65. {
  66. _ultrasoundImageSourceType = value;
  67. IsDeviceSelected = _ultrasoundImageSourceType == Sourcetype.VideoCaptureDevice;
  68. if (IsDeviceSelected)
  69. {
  70. DeviceSourceList = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType<FilterInfo>();
  71. if (UltrasoundImageSource == null)
  72. {
  73. UltrasoundImageSource = DeviceSourceList.FirstOrDefault();
  74. }
  75. }
  76. OnPropertyChanged(() => UltrasoundImageSourceType);
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 图像采集设备被选中
  82. /// </summary>
  83. public bool IsDeviceSelected
  84. {
  85. get => _isDeviceSelected;
  86. set
  87. {
  88. if (_isDeviceSelected != value)
  89. {
  90. _isDeviceSelected = value;
  91. OnPropertyChanged(() => IsDeviceSelected);
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// 按间隔时间检测模式被选中
  97. /// </summary>
  98. public bool IsIntervalTimeMode
  99. {
  100. get => _isIntervalTimeMode;
  101. set
  102. {
  103. if (_isIntervalTimeMode != value)
  104. {
  105. _isIntervalTimeMode = value;
  106. OnPropertyChanged(() => IsIntervalTimeMode);
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// 图像文件路径
  112. /// </summary>
  113. public string VideoFilePath
  114. {
  115. get => _videoFilePath;
  116. set
  117. {
  118. if (_videoFilePath != value)
  119. {
  120. _videoFilePath = value;
  121. OnPropertyChanged(() => VideoFilePath);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// 图像源
  127. /// </summary>
  128. public FilterInfo UltrasoundImageSource
  129. {
  130. get => _ultrasoundImageSource;
  131. set
  132. {
  133. if (_ultrasoundImageSource != value)
  134. {
  135. _ultrasoundImageSource = value;
  136. OnPropertyChanged(() => UltrasoundImageSource);
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// AI每秒频次
  142. /// </summary>
  143. public int DetectTps
  144. {
  145. get => _detectTps;
  146. set
  147. {
  148. if (_detectTps != value)
  149. {
  150. _detectTps = value;
  151. OnPropertyChanged(() => DetectTps);
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 图像裁切
  157. /// </summary>
  158. public bool CropImage
  159. {
  160. get => _cropImage;
  161. set
  162. {
  163. if (_cropImage != value)
  164. {
  165. _cropImage = value;
  166. OnPropertyChanged(() => CropImage);
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 采集设备清单
  172. /// </summary>
  173. public IEnumerable<FilterInfo> DeviceSourceList
  174. {
  175. get => _deviceSourceList;
  176. private set
  177. {
  178. if (_deviceSourceList != value)
  179. {
  180. _deviceSourceList = value;
  181. OnPropertyChanged(() => DeviceSourceList);
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// 检测模式
  187. /// </summary>
  188. public EnumDetectMode DetectMode
  189. {
  190. get => _detectMode;
  191. set
  192. {
  193. if (_detectMode != value)
  194. {
  195. _detectMode = value;
  196. if (_detectMode == EnumDetectMode.PeriodicIntervals)
  197. {
  198. IsIntervalTimeMode = true;
  199. }
  200. else
  201. {
  202. IsIntervalTimeMode = false;
  203. }
  204. OnPropertyChanged(() => DetectMode);
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// 间隔时间
  210. /// </summary>
  211. public int IntervalTime
  212. {
  213. get => _intervalTime;
  214. set
  215. {
  216. if (_intervalTime != value)
  217. {
  218. _intervalTime = value;
  219. OnPropertyChanged(() => IntervalTime);
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// CPU占用
  225. /// </summary>
  226. public int CPUUsed
  227. {
  228. get => _cpuUsed;
  229. set
  230. {
  231. if (_cpuUsed != value)
  232. {
  233. _cpuUsed = value;
  234. OnPropertyChanged(() => CPUUsed);
  235. }
  236. }
  237. }
  238. /// <summary>
  239. /// 内存占用
  240. /// </summary>
  241. public int RAMUsed
  242. {
  243. get => _ramUsed;
  244. set
  245. {
  246. if (_ramUsed != value)
  247. {
  248. _ramUsed = value;
  249. OnPropertyChanged(() => RAMUsed);
  250. }
  251. }
  252. }
  253. /// <summary>
  254. /// 是否开启模拟占用CPU与内存
  255. /// </summary>
  256. public bool IsSimulation
  257. {
  258. get => _isSimulation;
  259. set
  260. {
  261. if (_isSimulation != value)
  262. {
  263. _isSimulation = value;
  264. OnPropertyChanged(() => IsSimulation);
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// 性能
  270. /// </summary>
  271. public EnumPerformance Performance
  272. {
  273. get => _performance;
  274. set
  275. {
  276. if (_performance != value)
  277. {
  278. _performance = value;
  279. OnPropertyChanged(() => Performance);
  280. }
  281. }
  282. }
  283. /// <summary>
  284. /// 是否显示脏器
  285. /// </summary>
  286. public bool IsShowOrgansContour
  287. {
  288. get => _isShowOrgansContour;
  289. set
  290. {
  291. if (_isShowOrgansContour != value)
  292. {
  293. _isShowOrgansContour = value;
  294. OnPropertyChanged(() => IsShowOrgansContour);
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// 显示病灶的方式
  300. /// </summary>
  301. public EnumDisplayType ShowLesions
  302. {
  303. get => _showLesions;
  304. set
  305. {
  306. if (_showLesions != value)
  307. {
  308. _showLesions = value;
  309. OnPropertyChanged(() => ShowLesions);
  310. }
  311. }
  312. }
  313. /// <summary>
  314. /// 选择AI SDK
  315. /// </summary>
  316. public AIDiagnosisType AIDiagnosisType
  317. {
  318. get => _aiDiagnosisType;
  319. set
  320. {
  321. if (_aiDiagnosisType != value)
  322. {
  323. _aiDiagnosisType = value;
  324. OnPropertyChanged(() => AIDiagnosisType);
  325. }
  326. }
  327. }
  328. public ButtonCommand BrowseCommand { get; }
  329. public bool CompleteMyocardialImg
  330. {
  331. get => _completeMyocardialImg;
  332. set
  333. {
  334. if (_completeMyocardialImg != value)
  335. {
  336. _completeMyocardialImg = value;
  337. OnPropertyChanged(() => CompleteMyocardialImg);
  338. }
  339. }
  340. }
  341. public SettingPageViewModel()
  342. {
  343. BrowseCommand = new ButtonCommand(OnBrowse);
  344. DeviceSourceList = new FilterInfoCollection(FilterCategory.VideoInputDevice).OfType<FilterInfo>();
  345. UltrasoundImageSourceTypeList = new List<Sourcetype>
  346. {
  347. Sourcetype.VideoCaptureDevice,
  348. Sourcetype.PictureOrVideoFile
  349. };
  350. LoadSettings();
  351. }
  352. private void OnBrowse(object args)
  353. {
  354. OpenFileDialog openFileDialog = new OpenFileDialog
  355. {
  356. Filter = "图像或视频文件|*.png;*.bmp;*.jpg;*.jpeg;*.mp4;*.avi",
  357. Multiselect = false,
  358. Title = "选择一个图像或视频"
  359. };
  360. if (openFileDialog.ShowDialog() ?? false)
  361. {
  362. VideoFilePath = openFileDialog.FileName;
  363. }
  364. }
  365. private void LoadSettings()
  366. {
  367. IsEnableAI = SettingConfig.Instance.IsEnableAI;
  368. UltrasoundImageSourceType = SettingConfig.Instance.UltrasoundImageSourceType;
  369. CropImage = SettingConfig.Instance.CropImage;
  370. DetectTps = SettingConfig.Instance.DetectTps;
  371. IntervalTime = SettingConfig.Instance.IntervalTime;
  372. DetectMode = SettingConfig.Instance.DetectMode;
  373. IsDeviceSelected = _ultrasoundImageSourceType == Sourcetype.VideoCaptureDevice;
  374. if (IsDeviceSelected)
  375. {
  376. UltrasoundImageSource = DeviceSourceList.FirstOrDefault(s => s.MonikerString == SettingConfig.Instance.UltrasoundImageSource);
  377. if (UltrasoundImageSource == null)
  378. {
  379. UltrasoundImageSource = DeviceSourceList.FirstOrDefault();
  380. }
  381. }
  382. else
  383. {
  384. VideoFilePath = SettingConfig.Instance.UltrasoundImageSource;
  385. }
  386. IsSimulation = SettingConfig.Instance.IsSimulation;
  387. CPUUsed = SettingConfig.Instance.CPUUsed;
  388. RAMUsed = SettingConfig.Instance.RAMUsed;
  389. Performance = SettingConfig.Instance.Performance;
  390. IsShowOrgansContour = SettingConfig.Instance.IsShowOrgansContour;
  391. ShowLesions = SettingConfig.Instance.DisplayType;
  392. AIDiagnosisType = SettingConfig.Instance.AIDiagnosisType;
  393. CompleteMyocardialImg = SettingConfig.Instance.CompleteMyocardialImg;
  394. }
  395. public bool Save()
  396. {
  397. try
  398. {
  399. if (IntervalTime < 0)
  400. {
  401. MessageBox.Show("IntervalTime must be >=0");
  402. return false;
  403. }
  404. SettingConfig.PreInstance.UltrasoundImageSourceType = UltrasoundImageSourceType;
  405. SettingConfig.PreInstance.UltrasoundImageSource = UltrasoundImageSourceType == Sourcetype.VideoCaptureDevice ? UltrasoundImageSource?.MonikerString : VideoFilePath;
  406. SettingConfig.PreInstance.IsEnableAI = IsEnableAI;
  407. SettingConfig.PreInstance.Performance = Performance;
  408. SettingConfig.PreInstance.CropImage = CropImage;
  409. SettingConfig.PreInstance.DetectTps = DetectTps;
  410. SettingConfig.PreInstance.DetectMode = DetectMode;
  411. SettingConfig.PreInstance.IntervalTime = IntervalTime;
  412. SettingConfig.PreInstance.RAMUsed = RAMUsed;
  413. SettingConfig.PreInstance.CPUUsed = CPUUsed;
  414. SettingConfig.PreInstance.IsSimulation = IsSimulation;
  415. SettingConfig.PreInstance.IsShowOrgansContour = IsShowOrgansContour;
  416. SettingConfig.PreInstance.DisplayType = ShowLesions;
  417. SettingConfig.PreInstance.AIDiagnosisType = AIDiagnosisType;
  418. SettingConfig.PreInstance.CompleteMyocardialImg = CompleteMyocardialImg;
  419. SettingConfig.PreInstance.Save();
  420. return true;
  421. }
  422. catch (Exception ex)
  423. {
  424. MessageBox.Show($"OnSave command exception: {ex}");
  425. return false;
  426. }
  427. }
  428. }
  429. }