diff --git a/Azaion.Annotator/DatasetExplorer.xaml.cs b/Azaion.Annotator/DatasetExplorer.xaml.cs index b22cac0..bff4812 100644 --- a/Azaion.Annotator/DatasetExplorer.xaml.cs +++ b/Azaion.Annotator/DatasetExplorer.xaml.cs @@ -84,7 +84,7 @@ public partial class DatasetExplorer ExplorerEditor.CurrentAnnClass = (AnnotationClass)LvClasses.SelectedItem; await ReloadThumbnails(); LoadClassDistribution(); - + SizeChanged += async (_, _) => await SaveUserSettings(); LocationChanged += async (_, _) => await SaveUserSettings(); StateChanged += async (_, _) => await SaveUserSettings(); @@ -273,7 +273,7 @@ public partial class DatasetExplorer var thumbnailDtos = new List(); for (int i = 0; i < thumbnails.Length; i++) { - var thumbnailDto = GetThumbnail(thumbnails[i]); + var thumbnailDto = await GetThumbnail(thumbnails[i]); if (thumbnailDto != null) thumbnailDtos.Add(thumbnailDto); @@ -289,12 +289,14 @@ public partial class DatasetExplorer LoadingAnnsBar.Visibility = Visibility.Collapsed; } - private ThumbnailDto? GetThumbnail(string thumbnail) + private async Task GetThumbnail(string thumbnail) { try { var name = Path.GetFileNameWithoutExtension(thumbnail)[..^Config.THUMBNAIL_PREFIX.Length]; var imagePath = Path.Combine(_config.ImagesDirectory, name); + var labelPath = Path.Combine(_config.LabelsDirectory, $"{name}.txt"); + foreach (var f in _config.ImageFormats) { var curName = $"{imagePath}.{f}"; @@ -305,25 +307,21 @@ public partial class DatasetExplorer } } - var labelPath = Path.Combine(_config.LabelsDirectory, $"{name}.txt"); - if (!_galleryManager.LabelsCache.TryGetValue(Path.GetFileName(imagePath), out var info)) { - if (File.Exists(labelPath)) - return null; - - var imageExists = File.Exists(imagePath); - if (!imageExists) + if (!File.Exists(imagePath) || !File.Exists(labelPath)) { File.Delete(thumbnail); _logger.LogError($"No label {labelPath} found ! Image {imagePath} not found, thumbnail {thumbnail} was removed"); + return null; } - else - { - File.Move(imagePath, Path.Combine(_config.UnknownImages, imagePath)); - _logger.LogError($"No label {labelPath} found! But Image {imagePath} exists! Image moved to {_config.UnknownImages} directory!"); - } - return null; + + var classes = (await YoloLabel.ReadFromFile(labelPath)) + .Select(x => x.ClassNumber) + .Distinct() + .ToList(); + + info = _galleryManager.AddToCache(imagePath, classes); } if (!info.Classes.Contains(ExplorerEditor.CurrentAnnClass.Id) && ExplorerEditor.CurrentAnnClass.Id != -1) diff --git a/Azaion.Annotator/GalleryManager.cs b/Azaion.Annotator/GalleryManager.cs index 73c5820..ad0f2e9 100644 --- a/Azaion.Annotator/GalleryManager.cs +++ b/Azaion.Annotator/GalleryManager.cs @@ -148,20 +148,17 @@ public class GalleryManager : IGalleryManager _logger.LogInformation($"No labels found for image {imgName}! Image deleted!"); return null; } + var labels = (await YoloLabel.ReadFromFile(labelName, cancellationToken)) .Select(x => new CanvasLabel(x, size, size)) .ToList(); + var classes = labels.Select(x => x.ClassNumber).Distinct().ToList(); + + AddToCache(imgPath, classes); var thumbWhRatio = width / (float)height; var border = _config.ThumbnailConfig.Border; - var classes = labels.Select(x => x.ClassNumber).Distinct().ToList(); - LabelsCache.TryAdd(imgName, new LabelInfo - { - Classes = classes, - ImageDateTime = File.GetCreationTimeUtc(imgPath) - }); - var frameX = 0.0; var frameY = 0.0; var frameHeight = size.Height; @@ -223,6 +220,20 @@ public class GalleryManager : IGalleryManager return null; } } + + public LabelInfo AddToCache(string imgPath, List classes) + { + var labelInfo = new LabelInfo + { + Classes = classes, + ImageDateTime = File.GetCreationTimeUtc(imgPath) + }; + LabelsCache.TryAdd(Path.GetFileName(imgPath), labelInfo); + + //Save to file only each 2 seconds + _ = ThrottleExt.Throttle(async () => await SaveLabelsCache(), TimeSpan.FromSeconds(2)); + return labelInfo; + } } public interface IGalleryManager @@ -230,6 +241,7 @@ public interface IGalleryManager event ThumbnailsUpdatedEventHandler ThumbnailsUpdate; double ThumbnailsPercentage { get; set; } Task SaveLabelsCache(); + LabelInfo AddToCache(string imgPath, List classes); ConcurrentDictionary LabelsCache { get; set; } Task CreateThumbnail(string imgPath, CancellationToken cancellationToken = default); Task RefreshThumbnails();