ExportView.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using AIPractice.LabellerServer.Managers;
  3. using AIPractice.Shared.ImageRois;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  9. using System.Runtime.Serialization.Json;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Forms;
  14. using AIPractice.LabellerServer.Managers.Entities;
  15. using AIPractice.LabellerServer.ViewModels;
  16. using UserControl = System.Windows.Controls.UserControl;
  17. namespace AIPractice.LabellerServer.WPF
  18. {
  19. [DataContract]
  20. public class Roi
  21. {
  22. [DataMember]
  23. public System.Drawing.Point[] Points { get; set; }
  24. [DataMember]
  25. public string RootLabelId { get; set; }
  26. [DataMember]
  27. public string RootLabelTitle { get; set; }
  28. [DataMember]
  29. public string ConclusionId { get; set; }
  30. [DataMember]
  31. public string ConclusionTitle { get; set; }
  32. [DataMember]
  33. public bool IsParts { get; set; }
  34. }
  35. [DataContract]
  36. public class Label
  37. {
  38. [DataMember]
  39. public string Account { get; set; }
  40. [DataMember]
  41. public string RootLabelId { get; set; }
  42. [DataMember]
  43. public string RootLabelTitle { get; set; }
  44. [DataMember]
  45. public string ConclusionId { get; set; }
  46. [DataMember]
  47. public string ConclusionTitle { get; set; }
  48. [DataMember]
  49. public Roi[] Rois { get; set; }
  50. }
  51. /// <summary>
  52. /// Interaction logic for ExportView.xaml
  53. /// </summary>
  54. public partial class ExportView : UserControl
  55. {
  56. private bool _selectingUnSelectingAll = false;
  57. private readonly ObservableCollection<AccountViewModel> _accountViewModels = new ObservableCollection<AccountViewModel>();
  58. public ExportView()
  59. {
  60. InitializeComponent();
  61. AccountSelector.ItemsSource = _accountViewModels;
  62. LoadAccounts();
  63. LoadImageDescriptions();
  64. }
  65. private void OnSelectDirDirClick(object sender, RoutedEventArgs e)
  66. {
  67. var dialog = new FolderBrowserDialog();
  68. if (dialog.ShowDialog() == DialogResult.OK)
  69. {
  70. if (!string.IsNullOrEmpty(dialog.SelectedPath))
  71. {
  72. ExportDir.Text = dialog.SelectedPath;
  73. }
  74. }
  75. }
  76. private void OnExportClick(object sender, RoutedEventArgs e)
  77. {
  78. if (string.IsNullOrWhiteSpace(ExportDir.Text))
  79. {
  80. System.Windows.MessageBox.Show("请选择导出目录。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  81. ExportDir.Focus();
  82. return;
  83. }
  84. var description = ExportDesc.Text;
  85. var accounts = new Dictionary<string, AccountViewModel>();
  86. foreach (var accountViewModel in _accountViewModels)
  87. {
  88. if (accountViewModel.IsSelected)
  89. {
  90. accounts.Add(accountViewModel.Id, accountViewModel);
  91. }
  92. }
  93. if(accounts.Count == 0)
  94. {
  95. System.Windows.MessageBox.Show("请选择要导出的人员。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  96. AccountSelector.Focus();
  97. return;
  98. }
  99. var dataManager = ManagerContainer.GetManager<IDataManager>();
  100. int count = 0;
  101. if (string.IsNullOrEmpty(description))
  102. {
  103. count = dataManager.GetExportCount(accounts.Keys.ToList());
  104. }
  105. else
  106. {
  107. count = dataManager.GetExportCount(description, accounts.Keys.ToList());
  108. }
  109. if (count == 0)
  110. {
  111. System.Windows.MessageBox.Show("可导出的图片数量为0。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  112. return;
  113. }
  114. var targetDir = ExportDir.Text;
  115. var imageDir = Path.Combine(targetDir, "Images");
  116. var labelDir = Path.Combine(targetDir, "Labels");
  117. if (!Directory.Exists(imageDir))
  118. {
  119. Directory.CreateDirectory(imageDir);
  120. }
  121. if (!Directory.Exists(labelDir))
  122. {
  123. Directory.CreateDirectory(labelDir);
  124. }
  125. IsEnabled = false;
  126. DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Label));
  127. Task.Run(() =>
  128. {
  129. try
  130. {
  131. var index = 1;
  132. var blockCount = 1000;
  133. var blocks = count / blockCount;
  134. var lastCount = count % blockCount;
  135. if (lastCount != 0)
  136. {
  137. blocks++;
  138. }
  139. for (var i = 0; i < blocks; i++)
  140. {
  141. IEnumerable<CommittedImage> images;
  142. if (i == blocks - 1)
  143. {
  144. if (!string.IsNullOrEmpty(description))
  145. {
  146. images = lastCount != 0 ? dataManager.GetCommittedImages(description, blockCount * i, lastCount, accounts.Keys.ToList()) : new List<CommittedImage>();
  147. }
  148. else
  149. {
  150. images = lastCount != 0 ? dataManager.GetCommittedImages(blockCount * i, lastCount, accounts.Keys.ToList()) : new List<CommittedImage>();
  151. }
  152. }
  153. else
  154. {
  155. if (!string.IsNullOrEmpty(description))
  156. {
  157. images = dataManager.GetCommittedImages(description, blockCount * i, blockCount, accounts.Keys.ToList());
  158. }
  159. else
  160. {
  161. images = dataManager.GetCommittedImages(blockCount * i, blockCount, accounts.Keys.ToList());
  162. }
  163. }
  164. foreach (var image in images)
  165. {
  166. var labelImage = dataManager.GetLabelImage(image.ImageId);
  167. if (!string.IsNullOrWhiteSpace(description) && labelImage.Description != description)
  168. {
  169. index++;
  170. Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
  171. continue;
  172. }
  173. if (!accounts.Keys.Contains(image.AccountId))
  174. {
  175. index++;
  176. Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
  177. continue;
  178. }
  179. var account = accounts[image.AccountId];
  180. var imageData = ImageData.FromXml(image.Content);
  181. var accountName = account.FriendlyName;
  182. var accountType = account.IsReviewer ? "Reviewer" : "User";
  183. var result = "未审核";
  184. switch(image.Result)
  185. {
  186. case ReviewResult.Best:
  187. result = "最佳";
  188. break;
  189. case ReviewResult.Fail:
  190. result = "糟糕";
  191. break;
  192. case ReviewResult.Ok:
  193. result = "合格";
  194. break;
  195. }
  196. var imageFileName = Path.Combine(imageDir, $"{imageData.Id}_{accountName}_{accountType}_{result}.jpg");
  197. var labelFileName = Path.Combine(labelDir, $"{imageData.Id}_{accountName}_{accountType}_{result}.txt");
  198. File.WriteAllBytes(imageFileName, imageData.Data);
  199. var label = new Label
  200. {
  201. Account = accountName,
  202. RootLabelId = imageData.RootLabelId,
  203. RootLabelTitle = imageData.RootLabelTitle
  204. };
  205. var conclusion = imageData.Conclusion;
  206. if (conclusion != null)
  207. {
  208. label.ConclusionId = conclusion.Id;
  209. label.ConclusionTitle = conclusion.Title;
  210. }
  211. else
  212. {
  213. var roiList = new List<Roi>();
  214. foreach (var roiData in imageData.Rois)
  215. {
  216. var roiConclusion = roiData.Conclusion;
  217. var roiPoints = roiData.Points.ToArray();
  218. var roi = new Roi
  219. {
  220. RootLabelId = roiData.RootLabelId,
  221. RootLabelTitle = roiData.RootLabelTitle,
  222. ConclusionId = roiConclusion.Id,
  223. ConclusionTitle = roiConclusion.Title,
  224. IsParts = roiConclusion.IsParts,
  225. Points = roiPoints
  226. };
  227. roiList.Add(roi);
  228. }
  229. label.Rois = roiList.ToArray();
  230. }
  231. using (var fs = File.Create(labelFileName))
  232. {
  233. js.WriteObject(fs, label);
  234. }
  235. index++;
  236. Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
  237. }
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. LogManager.WriteLog($"Export error:{ex.Message}");
  243. }
  244. Dispatcher.Invoke(() => { IsEnabled = true; });
  245. });
  246. }
  247. private void OnAccountViewModelIsSelectedChanged(object sender, EventArgs e)
  248. {
  249. var selectedNames = _accountViewModels.Where(x => x.IsSelected).Select(x => x.FriendlyName).ToArray();
  250. AccountSelector.Text = string.Join(",", selectedNames);
  251. GetExportCount();
  252. }
  253. private void LoadAccounts()
  254. {
  255. foreach (var accountViewModel in _accountViewModels)
  256. {
  257. accountViewModel.IsSelectedChanged -= OnAccountViewModelIsSelectedChanged;
  258. }
  259. _accountViewModels.Clear();
  260. var dataManager = ManagerContainer.GetManager<IDataManager>();
  261. var accounts = dataManager.GetAccounts();
  262. foreach (var account in accounts)
  263. {
  264. var accountViewModel = new AccountViewModel
  265. {
  266. Id = account.Id,
  267. Name = account.Name,
  268. Description = account.Description,
  269. Password = account.Password,
  270. CreatedTime = account.CreatedTime,
  271. IsReviewer = account.IsReviewer
  272. };
  273. accountViewModel.IsSelectedChanged += OnAccountViewModelIsSelectedChanged;
  274. _accountViewModels.Add(accountViewModel);
  275. }
  276. _selectingUnSelectingAll = true;
  277. AccountSelector.SelectAll();
  278. _selectingUnSelectingAll = false;
  279. GetExportCount();
  280. }
  281. private void LoadImageDescriptions()
  282. {
  283. var dataManager = ManagerContainer.GetManager<IDataManager>();
  284. var descriptions = dataManager.GetExistingLabels();
  285. ExportDesc.ItemsSource = descriptions;
  286. }
  287. private void OnDescSelectionChanged(object sender, EventArgs e)
  288. {
  289. GetExportCount();
  290. }
  291. private void GetExportCount()
  292. {
  293. if (!_selectingUnSelectingAll)
  294. {
  295. var accountIds = new List<string>();
  296. foreach (var accountViewModel in _accountViewModels)
  297. {
  298. if (accountViewModel.IsSelected)
  299. {
  300. accountIds.Add(accountViewModel.Id);
  301. }
  302. }
  303. var dataManager = ManagerContainer.GetManager<IDataManager>();
  304. int count;
  305. if (ExportDesc.SelectedItem != null)
  306. {
  307. var desc = ExportDesc.SelectedItem.ToString();
  308. count = dataManager.GetExportCount(desc, accountIds);
  309. }
  310. else
  311. {
  312. count = dataManager.GetExportCount(accountIds);
  313. }
  314. ExportInfo.Content = $"可导出数量: {count}";
  315. }
  316. }
  317. private void OnSelectUnSelectAllAccounts(object sender, bool e)
  318. {
  319. _selectingUnSelectingAll = true;
  320. foreach (var accountViewModel in _accountViewModels)
  321. {
  322. accountViewModel.IsSelected = e;
  323. }
  324. _selectingUnSelectingAll = false;
  325. GetExportCount();
  326. }
  327. private void OnAccountsRefreshed(object sender, EventArgs e)
  328. {
  329. LoadAccounts();
  330. }
  331. private void OnExportDescRefreshed(object sender, EventArgs e)
  332. {
  333. LoadImageDescriptions();
  334. }
  335. }
  336. }