123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using AIPractice.LabellerServer.Managers;
- using AIPractice.LabellerServer.Managers.Entities;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using AIPractice.LabellerServer.ViewModels;
- namespace AIPractice.LabellerServer.WPF
- {
- /// <summary>
- /// Interaction logic for AssignSettingView.xaml
- /// </summary>
- public partial class AssignSettingView
- {
- private readonly ObservableCollection<AccountViewModel> _accountViewModels = new ObservableCollection<AccountViewModel>();
- public AssignSettingView()
- {
- InitializeComponent();
- AccountSelector.ItemsSource = _accountViewModels;
- LoadAccounts();
- LoadImageDescriptions();
- OnImageDescSelectionChanged(this,null);
- }
- private void OnAssignClick(object sender, RoutedEventArgs e)
- {
- if (!int.TryParse(AssignCount.Text, out int assignCount))
- {
- MessageBox.Show("请输入数字","错误",MessageBoxButton.OK, MessageBoxImage.Error);
- AssignCount.Focus();
- return;
- }
- if (!int.TryParse(DuplicateCount.Text, out int duplicateCount))
- {
- MessageBox.Show("请输入数字", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- DuplicateCount.Focus();
- return;
- }
- if (string.IsNullOrEmpty(ImageDesc.Text))
- {
- MessageBox.Show("请输入要分配的图像的标签", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- ImageDesc.Focus();
- return;
- }
- var description = ImageDesc.Text;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var unAssignedImages = dataManager.GetUnAssignedLabelImages(assignCount, description);
- if(unAssignedImages.Count < assignCount)
- {
- assignCount = unAssignedImages.Count;
- }
- var duplicateImages = unAssignedImages.Take(duplicateCount).ToList();
- if(duplicateImages.Count < duplicateCount)
- {
- duplicateCount = duplicateImages.Count;
- }
- var assignImages = unAssignedImages.Skip(duplicateCount).ToList();
- var accounts = _accountViewModels.Where(x=>x.IsSelected).ToList();
- if (accounts.Count == 0)
- {
- MessageBox.Show("请输入要分配的人员。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- AccountSelector.Focus();
- return;
- }
- var eachCount = (assignCount - duplicateCount) / accounts.Count;
- if(eachCount < 0)
- {
- eachCount = 0;
- }
- if (duplicateCount > 0 && accounts.Count == 1)
- {
- MessageBox.Show("当共享图片数量大于0时,参与分配的人员必须大于1。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- AccountSelector.Focus();
- return;
- }
- if (Application.Current.MainWindow != null)
- {
- Application.Current.MainWindow.IsEnabled = false;
- LogManager.WriteLog("开始分配图像资源...");
- Task.Run(() =>
- {
- try
- {
- var assignIndex = 0;
- var accountIndex = 0;
- foreach (var account in accounts)
- {
- var assignImageCount = 0;
- if (duplicateCount > 0 || eachCount > 0)
- {
- var virtualCase = new VirtualCase
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- Description = description,
- CreatedTime = DateTime.Now
- };
- dataManager.AddVirtualCase(virtualCase);
- var caseImageIndex = 0;
- var virtualCaseImages = new List<VirtualCaseImage>();
- foreach (var duplicateImage in duplicateImages)
- {
- var virtualCaseImage = new VirtualCaseImage
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- ImageId = duplicateImage.Id,
- ImageIndex = caseImageIndex,
- CreatedTime = DateTime.Now,
- CaseId = virtualCase.Id
- };
- duplicateImage.Assigned = true;
- duplicateImage.IsShared = true;
- virtualCaseImages.Add(virtualCaseImage);
- caseImageIndex++;
- assignImageCount++;
- }
- for (var i = 0; i < eachCount; i++)
- {
- var image = assignImages[assignIndex];
- var virtualCaseImage = new VirtualCaseImage
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- ImageId = image.Id,
- ImageIndex = caseImageIndex,
- CreatedTime = DateTime.Now,
- CaseId = virtualCase.Id
- };
- image.Assigned = true;
- virtualCaseImages.Add(virtualCaseImage);
- caseImageIndex++;
- assignIndex++;
- assignImageCount++;
- }
- dataManager.AddVirtualCaseImages(virtualCaseImages.ToArray());
-
- var assignedCaseCount = dataManager.GetVirtualCaseCount(account.Id);
- var assignedCase = new AssignedCase
- {
- Id = Guid.NewGuid().ToString("N").ToUpper(),
- AccountId = account.Id,
- CaseId = virtualCase.Id,
- CaseIndex = assignedCaseCount,
- CreatedTime = DateTime.Now,
-
- };
- dataManager.AddAssignedCase(assignedCase);
- LogManager.WriteLog($"分配了{duplicateImages.Count + assignImageCount}图像资源到 {account.FriendlyName}");
- accountIndex++;
- var progress = (int) ((float) accountIndex / accounts.Count * 100);
- LogManager.WriteLog($"分配图像资源...{progress}%");
- }
- }
- dataManager.UpdateLabelImages(duplicateImages.ToArray());
- dataManager.UpdateLabelImages(assignImages.ToArray());
- LogManager.WriteLog("分配图像资源完毕.");
- }
- catch (Exception ex)
- {
- LogManager.WriteLog($"分配图像资源失败:{ex.Message}.");
- }
- finally
- {
- Dispatcher.Invoke(() => { Application.Current.MainWindow.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);
- }
- 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);
- }
- AccountSelector.SelectAll();
- }
- private void LoadImageDescriptions()
- {
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- var descriptions = dataManager.GetExistingLabels();
- ImageDesc.ItemsSource = descriptions;
- }
- private void OnImageDescSelectionChanged(object sender, EventArgs e)
- {
- int unassignedCount;
- var dataManager = ManagerContainer.GetManager<IDataManager>();
- if (ImageDesc.SelectedItem != null)
- {
- unassignedCount = dataManager.GetUnAssignedImageCount(ImageDesc.SelectedItem.ToString());
- }
- else
- {
- unassignedCount = dataManager.GetUnAssignedImageCount();
- }
- LeftCount.Content = $"剩余可分配数量: {unassignedCount}";
- }
- private void OnSelectUnSelectAllAccounts(object sender, bool e)
- {
- foreach (var accountViewModel in _accountViewModels)
- {
- accountViewModel.IsSelected = e;
- }
- }
- private void OnAccountsRefreshed(object sender, EventArgs e)
- {
- LoadAccounts();
- }
- private void OnImageDescRefreshed(object sender, EventArgs e)
- {
- LoadImageDescriptions();
- }
- }
- }
|