fix editing annotation

This commit is contained in:
Alex Bezdieniezhnykh
2024-09-13 17:39:53 +03:00
parent 52371ace3a
commit 42fdee599e
11 changed files with 171 additions and 67 deletions
+50 -39
View File
@@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
@@ -24,16 +25,23 @@ public partial class DatasetExplorer
private readonly string _thumbnailsCacheFile;
private IConfigRepository _configRepository;
private readonly FormState _formState;
private readonly IGalleryManager _galleryManager;
private static Dictionary<string, List<int>> LabelsCache { get; set; } = new();
public string CurrentImage { get; set; }
public ThumbnailDto? CurrentThumbnail { get; set; }
public DatasetExplorer(Config config, ILogger<DatasetExplorer> logger, IConfigRepository configRepository, FormState formState)
public DatasetExplorer(
Config config,
ILogger<DatasetExplorer> logger,
IConfigRepository configRepository,
FormState formState,
IGalleryManager galleryManager)
{
_config = config;
_logger = logger;
_configRepository = configRepository;
_formState = formState;
_galleryManager = galleryManager;
_thumbnailsCacheFile = Path.Combine(config.ThumbnailsDirectory, Config.ThumbnailsCacheFile);
if (File.Exists(_thumbnailsCacheFile))
{
@@ -43,19 +51,45 @@ public partial class DatasetExplorer
InitializeComponent();
DataContext = this;
Loaded += (_, _) =>
Loaded += async (_, _) =>
{
AllAnnotationClasses = new ObservableCollection<AnnotationClass>(
new List<AnnotationClass> { new(-1, "All") }
.Concat(_config.AnnotationClasses));
LvClasses.ItemsSource = AllAnnotationClasses;
LvClasses.SelectionChanged += async (_, _) =>
LvClasses.MouseUp += async (_, _) =>
{
var selectedClass = (AnnotationClass)LvClasses.SelectedItem;
await SelectClass(selectedClass);
ExplorerEditor.CurrentAnnClass = selectedClass;
config.LastSelectedExplorerClass = selectedClass.Id;
if (Switcher.SelectedIndex == 0)
await ReloadThumbnails();
else
foreach (var ann in ExplorerEditor.CurrentAnns.Where(x => x.IsSelected))
ann.AnnotationClass = selectedClass;
};
LvClasses.SelectedIndex = 0;
LvClasses.SelectionChanged += (_, _) =>
{
if (Switcher.SelectedIndex != 1)
return;
var selectedClass = (AnnotationClass)LvClasses.SelectedItem;
if (selectedClass == null)
return;
ExplorerEditor.CurrentAnnClass = selectedClass;
foreach (var ann in ExplorerEditor.CurrentAnns.Where(x => x.IsSelected))
ann.AnnotationClass = selectedClass;
};
LvClasses.SelectedIndex = config.LastSelectedExplorerClass ?? 0;
ExplorerEditor.CurrentAnnClass = (AnnotationClass)LvClasses.SelectedItem;
await ReloadThumbnails();
SizeChanged += async (_, _) => await SaveUserSettings();
LocationChanged += async (_, _) => await SaveUserSettings();
@@ -68,7 +102,7 @@ public partial class DatasetExplorer
Visibility = Visibility.Hidden;
};
ThumbnailsView.KeyDown += (sender, args) =>
ThumbnailsView.KeyDown += async (sender, args) =>
{
switch (args.Key)
{
@@ -76,29 +110,15 @@ public partial class DatasetExplorer
DeleteAnnotations();
break;
case Key.Enter:
EditAnnotation();
await EditAnnotation();
break;
}
};
ThumbnailsView.MouseDoubleClick += async (_, _) => await EditAnnotation();
ExplorerEditor.KeyDown += (_, args) =>
{
var key = args.Key;
var keyNumber = (int?)null;
Activated += (_, _) => { _formState.ActiveWindow = WindowsEnum.DatasetExplorer; };
if ((int)key >= (int)Key.D1 && (int)key <= (int)Key.D9)
keyNumber = key - Key.D1;
if ((int)key >= (int)Key.NumPad1 && (int)key <= (int)Key.NumPad9)
keyNumber = key - Key.NumPad1;
if (!keyNumber.HasValue)
return;
LvClasses.SelectedIndex = keyNumber.Value;
};
ExplorerEditor.GetTimeFunc = () => _formState.GetTime(CurrentImage!)!.Value;
ExplorerEditor.GetTimeFunc = () => _formState.GetTime(CurrentThumbnail!.ImagePath)!.Value;
}
private async Task EditAnnotation()
@@ -111,11 +131,12 @@ public partial class DatasetExplorer
{
ImageSource = new BitmapImage(new Uri(dto.ImagePath))
};
CurrentImage = dto.ImagePath;
CurrentThumbnail = dto;
Switcher.SelectedIndex = 1;
LvClasses.SelectedIndex = 1;
var time = _formState.GetTime(CurrentImage)!.Value;
var time = _formState.GetTime(dto.ImagePath)!.Value;
foreach (var ann in await YoloLabel.ReadFromFile(dto.LabelPath))
{
var annClass = _config.AnnotationClasses[ann.ClassNumber];
@@ -123,33 +144,23 @@ public partial class DatasetExplorer
Dispatcher.Invoke(() => ExplorerEditor.CreateAnnotation(annClass, time, annInfo));
}
Switcher.SelectionChanged += (_, args) =>
Switcher.SelectionChanged += (sender, args) =>
{
//From Explorer to Editor
if (Switcher.SelectedIndex == 1)
{
//Editor
_tempSelectedClassIdx = LvClasses.SelectedIndex;
LvClasses.ItemsSource = _config.AnnotationClasses;
}
else
{
//Explorer
LvClasses.ItemsSource = AllAnnotationClasses;
LvClasses.SelectedIndex = _tempSelectedClassIdx;
}
};
}
private async Task SelectClass(AnnotationClass annClass)
{
ExplorerEditor.CurrentAnnClass = annClass;
if (Switcher.SelectedIndex == 0)
await ReloadThumbnails();
else
foreach (var ann in ExplorerEditor.CurrentAnns.Where(x => x.IsSelected))
ann.AnnotationClass = annClass;
}
private async Task SaveUserSettings()
{
_config.DatasetExplorerConfig = this.GetConfig();