UCConfig.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using Ookii.Dialogs.Wpf;
  17. using System.Collections.ObjectModel;
  18. using MeterVision.FreeAi;
  19. using System.IO;
  20. namespace MeterVision.Config
  21. {
  22. /// <summary>
  23. /// UCConfig.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class UCConfig : UserControl, INotifyPropertyChanged
  26. {
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. protected void OnPropertyChanged(string propertyName)
  29. {
  30. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  31. }
  32. public ObservableCollection<string> AiDlls { get; set; }
  33. public ObservableCollection<string> Onnxs { get; set; }
  34. public CfginiItem _cfginiItem;
  35. public CfginiItem mConfigItem
  36. {
  37. get => _cfginiItem;
  38. set
  39. {
  40. if(_cfginiItem != value)
  41. {
  42. _cfginiItem = value;
  43. OnPropertyChanged(nameof(mConfigItem));
  44. }
  45. }
  46. }
  47. //private ConfigItem _curConfigItem;
  48. //public ConfigItem CurConfigItem
  49. //{
  50. // get => _curConfigItem;
  51. // set
  52. // {
  53. // if(_curConfigItem != value)
  54. // {
  55. // _curConfigItem = value;
  56. // OnPropertyChanged(nameof(CurConfigItem));
  57. // }
  58. // }
  59. //}
  60. //private RulerDigitTypeEnum _selecteRulerDigitType;
  61. //public RulerDigitTypeEnum SelectedRulerDigitType
  62. //{
  63. // get { return _selecteRulerDigitType; }
  64. // set
  65. // {
  66. // if (_selecteRulerDigitType != value)
  67. // {
  68. // _selecteRulerDigitType = value;
  69. // OnPropertyChanged(nameof(SelectedRulerDigitType));
  70. // ConfigItem.SaveRulerDigitType((int)value);
  71. // }
  72. // }
  73. //}
  74. //private RulerDigitPointerTypeEnum _selecteRulerDigitPointerType;
  75. //public RulerDigitPointerTypeEnum SelectedRulerDigitPointerType
  76. //{
  77. // get { return _selecteRulerDigitPointerType; }
  78. // set
  79. // {
  80. // if (_selecteRulerDigitPointerType != value)
  81. // {
  82. // _selecteRulerDigitPointerType = value;
  83. // OnPropertyChanged(nameof(SelectedRulerDigitPointerType));
  84. // ConfigItem.SaveRulerDigitPointerType((int)value);
  85. // }
  86. // }
  87. //}
  88. //private RulerPointerTypeEnum _selecteRulerPointerType;
  89. //public RulerPointerTypeEnum SelectedRulerPointerType
  90. //{
  91. // get { return _selecteRulerPointerType; }
  92. // set
  93. // {
  94. // if (_selecteRulerPointerType != value)
  95. // {
  96. // _selecteRulerPointerType = value;
  97. // OnPropertyChanged(nameof(SelectedRulerPointerType));
  98. // ConfigItem.SaveRulerPointerType((int)value);
  99. // }
  100. // }
  101. //}
  102. public UCConfig()
  103. {
  104. InitializeComponent();
  105. /*Window parentWindow = Window.GetWindow(this);
  106. if (parentWindow is MainWindow mainWindow)
  107. {
  108. CurConfigItem = mainWindow.mConfigItem;
  109. }*/
  110. //CurConfigItem = ConfigItem.GetConfigItem();
  111. mConfigItem = CfginiItem.GetConfigItem();
  112. this.DataContext = this;
  113. //this.InitUI();
  114. FindFreeAIDllFiles();
  115. FindOnnxFiles();
  116. //CurAiDll = CurConfigItem.AiDll;
  117. this.Loaded += UCConfig_Loaded;
  118. }
  119. private void UCConfig_Loaded(object sender, RoutedEventArgs e)
  120. {
  121. //throw new NotImplementedException();
  122. //FindFreeAIDllFiles();
  123. //FindOnnxFiles();
  124. }
  125. //private void InitUI()
  126. //{
  127. // SelectedRulerDigitType = (RulerDigitTypeEnum)CurConfigItem.RulerDigitType;
  128. // SelectedRulerDigitPointerType = (RulerDigitPointerTypeEnum)CurConfigItem.RulerDigitPointerType;
  129. // SelectedRulerPointerType = (RulerPointerTypeEnum)CurConfigItem.RulerPointerType;
  130. //}
  131. // 查询 EXE 目录下所有包含 "freeAI" 且后缀为 ".dll" 的文件
  132. private void FindFreeAIDllFiles()
  133. {
  134. // 获取当前应用程序的 EXE 文件路径
  135. string exeDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  136. // 使用通配符查询符合条件的 DLL 文件
  137. string[] files = Directory.GetFiles(exeDirectory, "*freeAI*.dll");
  138. if(AiDlls == null)
  139. {
  140. AiDlls = new ObservableCollection<string>();
  141. }
  142. else
  143. {
  144. AiDlls.Clear();
  145. }
  146. // 输出找到的所有文件
  147. foreach (var file in files)
  148. {
  149. //Console.WriteLine(file);
  150. // 获取文件名(包含扩展名)
  151. string fileNameWithExtension = Path.GetFileName(file);
  152. // 获取文件名(不包含扩展名)
  153. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
  154. AiDlls.Add(fileNameWithExtension);
  155. }
  156. }
  157. private void FindOnnxFiles()
  158. {
  159. // 获取当前应用程序的 EXE 文件路径
  160. string exeDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  161. // 使用通配符查询符合条件的 DLL 文件
  162. string[] files = Directory.GetFiles(exeDirectory, "*.onnx");
  163. if (Onnxs == null)
  164. {
  165. Onnxs = new ObservableCollection<string>();
  166. }
  167. else
  168. {
  169. Onnxs.Clear();
  170. }
  171. // 输出找到的所有文件
  172. foreach (var file in files)
  173. {
  174. //Console.WriteLine(file);
  175. // 获取文件名(包含扩展名)
  176. string fileNameWithExtension = Path.GetFileName(file);
  177. // 获取文件名(不包含扩展名)
  178. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
  179. Onnxs.Add(fileNameWithExtension);
  180. }
  181. }
  182. private void BtnSelectOnnx_Click(object sender, RoutedEventArgs e)
  183. {
  184. // 创建一个 OpenFileDialog 实例
  185. OpenFileDialog openFileDialog = new OpenFileDialog();
  186. // 设置对话框的标题
  187. openFileDialog.Title = "选择文件";
  188. // 设置默认文件夹(可选)
  189. //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  190. // 设置过滤器,以便只显示特定类型的文件(可选)
  191. openFileDialog.Filter = "AI模型文件 (*.onnx)|*.onnx|所有文件 (*.*)|*.*";
  192. // 显示对话框并检查用户是否点击了“确定”
  193. if (openFileDialog.ShowDialog() == true)
  194. {
  195. // 获取所选文件的全路径
  196. string filePath = openFileDialog.FileName;
  197. // 在界面上显示文件路径
  198. //txtFilePath.Text = $"选择的文件路径: {filePath}";
  199. //if (mConfigItem != null)
  200. //{
  201. // mConfigItem.OnnxPath = filePath;
  202. // setOnnxPath(filePath);
  203. //}
  204. //---------------
  205. //ConfigItem.SaveOnnxPath(filePath);
  206. ////获取父窗体
  207. //Window parentWindow = Window.GetWindow(this);
  208. //if(parentWindow is MainWindow mainWindow)
  209. //{
  210. // mainWindow.mConfigItem.OnnxPath = filePath;
  211. //}
  212. mConfigItem.OnnxPath = filePath;
  213. }
  214. }
  215. private void BtnDstImgPath_Click(object sender, RoutedEventArgs e)
  216. {
  217. // 创建 VistaFolderBrowserDialog 实例
  218. var dialog = new VistaFolderBrowserDialog();
  219. //dialog.Description = "请选择目标图像输出文件夹";
  220. dialog.Description = "请选择识别数据输出文件夹";
  221. dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  222. // 显示对话框并检查用户是否点击了“确定”
  223. if (dialog.ShowDialog() == true)
  224. {
  225. // 获取用户选择的文件夹路径
  226. string selectedFolderPath = dialog.SelectedPath;
  227. //------------------
  228. //ConfigItem.SaveDstImgPath(selectedFolderPath);
  229. ////获取父窗体
  230. //Window parentWindow = Window.GetWindow(this);
  231. //if (parentWindow is MainWindow mainWindow)
  232. //{
  233. // mainWindow.mConfigItem.DstImgPath = selectedFolderPath;
  234. //}
  235. mConfigItem.Output = selectedFolderPath;
  236. }
  237. }
  238. //private void BtnAiRunLog_Click(object sender, RoutedEventArgs e)
  239. //{
  240. // // 创建 VistaFolderBrowserDialog 实例
  241. // var dialog = new VistaFolderBrowserDialog();
  242. // dialog.Description = "请选择AI运行日志文件夹";
  243. // dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  244. // // 显示对话框并检查用户是否点击了“确定”
  245. // if (dialog.ShowDialog() == true)
  246. // {
  247. // // 获取用户选择的文件夹路径
  248. // string selectedFolderPath = dialog.SelectedPath;
  249. // ConfigItem.SaveAiLogPath(selectedFolderPath);
  250. // //获取父窗体
  251. // Window parentWindow = Window.GetWindow(this);
  252. // if (parentWindow is MainWindow mainWindow)
  253. // {
  254. // mainWindow.mConfigItem.AiLogPath = selectedFolderPath;
  255. // }
  256. // }
  257. //}
  258. //private void BtnTransImgPath_Click(object sender, RoutedEventArgs e)
  259. //{
  260. // var dialog = new VistaFolderBrowserDialog();
  261. // dialog.Description = "请选择图像转换后的存储文件夹";
  262. // dialog.UseDescriptionForTitle = true; // 使用 Description 作为窗口标题
  263. // // 显示对话框并检查用户是否点击了“确定”
  264. // if (dialog.ShowDialog() == true)
  265. // {
  266. // // 获取用户选择的文件夹路径
  267. // string selectedFolderPath = dialog.SelectedPath;
  268. // ConfigItem.SaveTransImgPath(selectedFolderPath);
  269. // //获取父窗体
  270. // Window parentWindow = Window.GetWindow(this);
  271. // if (parentWindow is MainWindow mainWindow)
  272. // {
  273. // mainWindow.mConfigItem.TransImgPath = selectedFolderPath;
  274. // }
  275. // }
  276. //}
  277. /////////////////////////////////////////////////////////////////////////
  278. }
  279. }