fix editing non-timed annotations

This commit is contained in:
Alex Bezdieniezhnykh
2024-09-20 09:23:12 +03:00
parent 2236eb7fcb
commit 742f1ffee9
10 changed files with 218 additions and 119 deletions
+66 -35
View File
@@ -1,10 +1,8 @@
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;
using Azaion.Annotator.DTO;
using Azaion.Annotator.Extensions;
using Microsoft.Extensions.Logging;
@@ -35,7 +33,8 @@ public partial class DatasetExplorer
Config config,
ILogger<DatasetExplorer> logger,
IConfigRepository configRepository,
FormState formState)
FormState formState,
IGalleryManager galleryManager)
{
_config = config;
_logger = logger;
@@ -93,6 +92,8 @@ public partial class DatasetExplorer
SizeChanged += async (_, _) => await SaveUserSettings();
LocationChanged += async (_, _) => await SaveUserSettings();
StateChanged += async (_, _) => await SaveUserSettings();
RefreshThumbBar.Value = galleryManager.ThumbnailsPercentage;
};
Closing += (sender, args) =>
@@ -139,7 +140,12 @@ public partial class DatasetExplorer
}
};
ExplorerEditor.GetTimeFunc = () => _formState.GetTime(CurrentThumbnail!.ImagePath)!.Value;
ExplorerEditor.GetTimeFunc = () => _formState.GetTime(CurrentThumbnail!.ImagePath);
galleryManager.ThumbnailsUpdate += thumbnailsPercentage =>
{
Dispatcher.Invoke(() => RefreshThumbBar.Value = thumbnailsPercentage);
};
}
private async Task EditAnnotation()
@@ -161,7 +167,7 @@ public partial class DatasetExplorer
Switcher.SelectedIndex = 1;
LvClasses.SelectedIndex = 1;
var time = _formState.GetTime(dto.ImagePath)!.Value;
var time = _formState.GetTime(dto.ImagePath);
ExplorerEditor.RemoveAllAnns();
foreach (var ann in await YoloLabel.ReadFromFile(dto.LabelPath))
{
@@ -215,6 +221,9 @@ public partial class DatasetExplorer
private async Task ReloadThumbnails()
{
LoadingAnnsCaption.Visibility = Visibility.Visible;
LoadingAnnsBar.Visibility = Visibility.Visible;
if (!Directory.Exists(_config.ThumbnailsDirectory))
return;
@@ -227,50 +236,72 @@ public partial class DatasetExplorer
await AddThumbnail(thumbnail);
if (thumbNum % 1000 == 0)
{
await File.WriteAllTextAsync(_thumbnailsCacheFile, JsonConvert.SerializeObject(LabelsCache));
LoadingAnnsBar.Value = thumbNum * 100.0 / thumbnails.Length;
}
thumbNum++;
}
LoadingAnnsCaption.Visibility = Visibility.Collapsed;
LoadingAnnsBar.Visibility = Visibility.Collapsed;
await File.WriteAllTextAsync(_thumbnailsCacheFile, JsonConvert.SerializeObject(LabelsCache));
}
private async Task AddThumbnail(string thumbnail)
{
var name = Path.GetFileNameWithoutExtension(thumbnail)[..^Config.ThumbnailPrefix.Length];
var imageName = Path.Combine(_config.ImagesDirectory, name);
foreach (var f in _config.ImageFormats)
try
{
var curName = $"{imageName}.{f}";
if (File.Exists(curName))
var name = Path.GetFileNameWithoutExtension(thumbnail)[..^Config.ThumbnailPrefix.Length];
var imageName = Path.Combine(_config.ImagesDirectory, name);
foreach (var f in _config.ImageFormats)
{
imageName = curName;
break;
}
}
var labelPath = Path.Combine(_config.LabelsDirectory, $"{name}.txt");
if (!LabelsCache.TryGetValue(name, out var classes))
{
if (!File.Exists(labelPath))
{
_logger.LogError($"No label {labelPath} found ! Image {(!File.Exists(imageName) ? "not exists!" : "exists.")}");
return;
var curName = $"{imageName}.{f}";
if (File.Exists(curName))
{
imageName = curName;
break;
}
}
var labels = await YoloLabel.ReadFromFile(labelPath);
classes = labels.Select(x => x.ClassNumber).Distinct().ToList();
LabelsCache.Add(name, classes);
}
var labelPath = Path.Combine(_config.LabelsDirectory, $"{name}.txt");
if (classes.Contains(ExplorerEditor.CurrentAnnClass.Id) || ExplorerEditor.CurrentAnnClass.Id == -1)
{
ThumbnailsDtos.Add(new ThumbnailDto
if (!LabelsCache.TryGetValue(name, out var classes))
{
ThumbnailPath = thumbnail,
ImagePath = imageName,
LabelPath = labelPath
});
}
if (!File.Exists(labelPath))
{
var imageExists = File.Exists(imageName);
if (!imageExists)
{
_logger.LogError($"No label {labelPath} found ! Image {imageName} not found, removing thumbnail {thumbnail}");
File.Delete(thumbnail);
}
else
{
_logger.LogError($"No label {labelPath} found! But Image {imageName} exists! Image moved to {_config.UnknownImages} directory!");
File.Move(imageName, Path.Combine(_config.UnknownImages, imageName));
}
return;
}
var labels = await YoloLabel.ReadFromFile(labelPath);
classes = labels.Select(x => x.ClassNumber).Distinct().ToList();
LabelsCache.Add(name, classes);
}
if (classes.Contains(ExplorerEditor.CurrentAnnClass.Id) || ExplorerEditor.CurrentAnnClass.Id == -1)
{
ThumbnailsDtos.Add(new ThumbnailDto
{
ThumbnailPath = thumbnail,
ImagePath = imageName,
LabelPath = labelPath
});
}
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
}
}
}