123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- using System;
- using AIPractice.LabellerServer.Managers;
- using AIPractice.Shared.ImageRois;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Json;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using AIPractice.LabellerServer.Managers.Entities;
- using AIPractice.LabellerServer.ViewModels;
- using UserControl = System.Windows.Controls.UserControl;
- namespace AIPractice.LabellerServer.WPF
- {
- [DataContract]
- public class Roi
- {
- [DataMember]
- public System.Drawing.Point[] Points { get; set; }
- [DataMember]
- public string RootLabelId { get; set; }
- [DataMember]
- public string RootLabelTitle { get; set; }
- [DataMember]
- public string ConclusionId { get; set; }
- [DataMember]
- public string ConclusionTitle { get; set; }
- [DataMember]
- public bool IsParts { get; set; }
- }
- [DataContract]
- public class Label
- {
- [DataMember]
- public string Account { get; set; }
- [DataMember]
- public string RootLabelId { get; set; }
- [DataMember]
- public string RootLabelTitle { get; set; }
- [DataMember]
- public string ConclusionId { get; set; }
- [DataMember]
- public string ConclusionTitle { get; set; }
- [DataMember]
- public Roi[] Rois { get; set; }
- }
- /// <summary>
- /// Interaction logic for ExportView.xaml
- /// </summary>
- public partial class ExportView : UserControl
- {
- private bool _selectingUnSelectingAll = false;
- private readonly ObservableCollection<AccountViewModel> _accountViewModels = new ObservableCollection<AccountViewModel>();
- public ExportView()
- {
- InitializeComponent();
- AccountSelector.ItemsSource = _accountViewModels;
- LoadAccounts();
- LoadImageDescriptions();
- }
- private void OnSelectDirDirClick(object sender, RoutedEventArgs e)
- {
- var dialog = new FolderBrowserDialog();
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- if (!string.IsNullOrEmpty(dialog.SelectedPath))
- {
- ExportDir.Text = dialog.SelectedPath;
- }
- }
- }
- private void OnExportClick(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(ExportDir.Text))
- {
- System.Windows.MessageBox.Show("请选择导出目录。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- ExportDir.Focus();
- return;
- }
- var description = ExportDesc.Text;
- var accounts = new Dictionary<string, AccountViewModel>();
- foreach (var accountViewModel in _accountViewModels)
- {
- if (accountViewModel.IsSelected)
- {
- accounts.Add(accountViewModel.Id, accountViewModel);
- }
- }
- if(accounts.Count == 0)
- {
- System.Windows.MessageBox.Show("请选择要导出的人员。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- AccountSelector.Focus();
- return;
- }
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- int count = 0;
- if (string.IsNullOrEmpty(description))
- {
- count = dataManager.GetExportCount(accounts.Keys.ToList());
- }
- else
- {
- count = dataManager.GetExportCount(description, accounts.Keys.ToList());
- }
- if (count == 0)
- {
- System.Windows.MessageBox.Show("可导出的图片数量为0。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- var targetDir = ExportDir.Text;
- var imageDir = Path.Combine(targetDir, "Images");
- var labelDir = Path.Combine(targetDir, "Labels");
- if (!Directory.Exists(imageDir))
- {
- Directory.CreateDirectory(imageDir);
- }
- if (!Directory.Exists(labelDir))
- {
- Directory.CreateDirectory(labelDir);
- }
- IsEnabled = false;
- DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Label));
- Task.Run(() =>
- {
- try
- {
- var index = 1;
- var blockCount = 1000;
- var blocks = count / blockCount;
- var lastCount = count % blockCount;
- if (lastCount != 0)
- {
- blocks++;
- }
- for (var i = 0; i < blocks; i++)
- {
- IEnumerable<CommittedImage> images;
- if (i == blocks - 1)
- {
- if (!string.IsNullOrEmpty(description))
- {
- images = lastCount != 0 ? dataManager.GetCommittedImages(description, blockCount * i, lastCount, accounts.Keys.ToList()) : new List<CommittedImage>();
- }
- else
- {
- images = lastCount != 0 ? dataManager.GetCommittedImages(blockCount * i, lastCount, accounts.Keys.ToList()) : new List<CommittedImage>();
- }
-
- }
- else
- {
- if (!string.IsNullOrEmpty(description))
- {
- images = dataManager.GetCommittedImages(description, blockCount * i, blockCount, accounts.Keys.ToList());
- }
- else
- {
- images = dataManager.GetCommittedImages(blockCount * i, blockCount, accounts.Keys.ToList());
- }
-
- }
- foreach (var image in images)
- {
- var labelImage = dataManager.GetLabelImage(image.ImageId);
- if (!string.IsNullOrWhiteSpace(description) && labelImage.Description != description)
- {
- index++;
- Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
- continue;
- }
- if (!accounts.Keys.Contains(image.AccountId))
- {
- index++;
- Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
- continue;
- }
- var account = accounts[image.AccountId];
- var imageData = ImageData.FromXml(image.Content);
- var accountName = account.FriendlyName;
- var accountType = account.IsReviewer ? "Reviewer" : "User";
- var result = "未审核";
- switch(image.Result)
- {
- case ReviewResult.Best:
- result = "最佳";
- break;
- case ReviewResult.Fail:
- result = "糟糕";
- break;
- case ReviewResult.Ok:
- result = "合格";
- break;
- }
- var imageFileName = Path.Combine(imageDir, $"{imageData.Id}_{accountName}_{accountType}_{result}.jpg");
- var labelFileName = Path.Combine(labelDir, $"{imageData.Id}_{accountName}_{accountType}_{result}.txt");
- File.WriteAllBytes(imageFileName, imageData.Data);
- var label = new Label
- {
- Account = accountName,
- RootLabelId = imageData.RootLabelId,
- RootLabelTitle = imageData.RootLabelTitle
- };
- var conclusion = imageData.Conclusion;
- if (conclusion != null)
- {
- label.ConclusionId = conclusion.Id;
- label.ConclusionTitle = conclusion.Title;
- }
- else
- {
- var roiList = new List<Roi>();
- foreach (var roiData in imageData.Rois)
- {
- var roiConclusion = roiData.Conclusion;
- var roiPoints = roiData.Points.ToArray();
- var roi = new Roi
- {
- RootLabelId = roiData.RootLabelId,
- RootLabelTitle = roiData.RootLabelTitle,
- ConclusionId = roiConclusion.Id,
- ConclusionTitle = roiConclusion.Title,
- IsParts = roiConclusion.IsParts,
- Points = roiPoints
- };
- roiList.Add(roi);
- }
- label.Rois = roiList.ToArray();
- }
- using (var fs = File.Create(labelFileName))
- {
- js.WriteObject(fs, label);
- }
- index++;
- Dispatcher.Invoke(() => { PB.Value = index * 100 / count; });
- }
- }
- }
- catch (Exception ex)
- {
- LogManager.WriteLog($"Export error:{ex.Message}");
- }
- Dispatcher.Invoke(() => { IsEnabled = true; });
- });
- }
- private void OnAccountViewModelIsSelectedChanged(object sender, EventArgs e)
- {
- var selectedNames = _accountViewModels.Where(x => x.IsSelected).Select(x => x.FriendlyName).ToArray();
- AccountSelector.Text = string.Join(",", selectedNames);
- GetExportCount();
- }
- private void LoadAccounts()
- {
- foreach (var accountViewModel in _accountViewModels)
- {
- accountViewModel.IsSelectedChanged -= OnAccountViewModelIsSelectedChanged;
- }
- _accountViewModels.Clear();
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var accounts = dataManager.GetAccounts();
- foreach (var account in accounts)
- {
- var accountViewModel = new AccountViewModel
- {
- Id = account.Id,
- Name = account.Name,
- Description = account.Description,
- Password = account.Password,
- CreatedTime = account.CreatedTime,
- IsReviewer = account.IsReviewer
- };
- accountViewModel.IsSelectedChanged += OnAccountViewModelIsSelectedChanged;
- _accountViewModels.Add(accountViewModel);
- }
- _selectingUnSelectingAll = true;
- AccountSelector.SelectAll();
- _selectingUnSelectingAll = false;
- GetExportCount();
- }
- private void LoadImageDescriptions()
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var descriptions = dataManager.GetExistingLabels();
- ExportDesc.ItemsSource = descriptions;
- }
- private void OnDescSelectionChanged(object sender, EventArgs e)
- {
- GetExportCount();
- }
- private void GetExportCount()
- {
- if (!_selectingUnSelectingAll)
- {
- var accountIds = new List<string>();
- foreach (var accountViewModel in _accountViewModels)
- {
- if (accountViewModel.IsSelected)
- {
- accountIds.Add(accountViewModel.Id);
- }
- }
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- int count;
- if (ExportDesc.SelectedItem != null)
- {
- var desc = ExportDesc.SelectedItem.ToString();
- count = dataManager.GetExportCount(desc, accountIds);
- }
- else
- {
- count = dataManager.GetExportCount(accountIds);
- }
- ExportInfo.Content = $"可导出数量: {count}";
- }
- }
- private void OnSelectUnSelectAllAccounts(object sender, bool e)
- {
- _selectingUnSelectingAll = true;
- foreach (var accountViewModel in _accountViewModels)
- {
- accountViewModel.IsSelected = e;
- }
- _selectingUnSelectingAll = false;
- GetExportCount();
- }
- private void OnAccountsRefreshed(object sender, EventArgs e)
- {
- LoadAccounts();
- }
- private void OnExportDescRefreshed(object sender, EventArgs e)
- {
- LoadImageDescriptions();
- }
- }
- }
|