fix dataset explorer

This commit is contained in:
Alex Bezdieniezhnykh
2024-11-05 22:24:46 +02:00
parent 5d537aeef6
commit 36afeb7379
2 changed files with 33 additions and 23 deletions
+14 -16
View File
@@ -84,7 +84,7 @@ public partial class DatasetExplorer
ExplorerEditor.CurrentAnnClass = (AnnotationClass)LvClasses.SelectedItem; ExplorerEditor.CurrentAnnClass = (AnnotationClass)LvClasses.SelectedItem;
await ReloadThumbnails(); await ReloadThumbnails();
LoadClassDistribution(); LoadClassDistribution();
SizeChanged += async (_, _) => await SaveUserSettings(); SizeChanged += async (_, _) => await SaveUserSettings();
LocationChanged += async (_, _) => await SaveUserSettings(); LocationChanged += async (_, _) => await SaveUserSettings();
StateChanged += async (_, _) => await SaveUserSettings(); StateChanged += async (_, _) => await SaveUserSettings();
@@ -273,7 +273,7 @@ public partial class DatasetExplorer
var thumbnailDtos = new List<ThumbnailDto>(); var thumbnailDtos = new List<ThumbnailDto>();
for (int i = 0; i < thumbnails.Length; i++) for (int i = 0; i < thumbnails.Length; i++)
{ {
var thumbnailDto = GetThumbnail(thumbnails[i]); var thumbnailDto = await GetThumbnail(thumbnails[i]);
if (thumbnailDto != null) if (thumbnailDto != null)
thumbnailDtos.Add(thumbnailDto); thumbnailDtos.Add(thumbnailDto);
@@ -289,12 +289,14 @@ public partial class DatasetExplorer
LoadingAnnsBar.Visibility = Visibility.Collapsed; LoadingAnnsBar.Visibility = Visibility.Collapsed;
} }
private ThumbnailDto? GetThumbnail(string thumbnail) private async Task<ThumbnailDto?> GetThumbnail(string thumbnail)
{ {
try try
{ {
var name = Path.GetFileNameWithoutExtension(thumbnail)[..^Config.THUMBNAIL_PREFIX.Length]; var name = Path.GetFileNameWithoutExtension(thumbnail)[..^Config.THUMBNAIL_PREFIX.Length];
var imagePath = Path.Combine(_config.ImagesDirectory, name); var imagePath = Path.Combine(_config.ImagesDirectory, name);
var labelPath = Path.Combine(_config.LabelsDirectory, $"{name}.txt");
foreach (var f in _config.ImageFormats) foreach (var f in _config.ImageFormats)
{ {
var curName = $"{imagePath}.{f}"; 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 (!_galleryManager.LabelsCache.TryGetValue(Path.GetFileName(imagePath), out var info))
{ {
if (File.Exists(labelPath)) if (!File.Exists(imagePath) || !File.Exists(labelPath))
return null;
var imageExists = File.Exists(imagePath);
if (!imageExists)
{ {
File.Delete(thumbnail); File.Delete(thumbnail);
_logger.LogError($"No label {labelPath} found ! Image {imagePath} not found, thumbnail {thumbnail} was removed"); _logger.LogError($"No label {labelPath} found ! Image {imagePath} not found, thumbnail {thumbnail} was removed");
return null;
} }
else
{ var classes = (await YoloLabel.ReadFromFile(labelPath))
File.Move(imagePath, Path.Combine(_config.UnknownImages, imagePath)); .Select(x => x.ClassNumber)
_logger.LogError($"No label {labelPath} found! But Image {imagePath} exists! Image moved to {_config.UnknownImages} directory!"); .Distinct()
} .ToList();
return null;
info = _galleryManager.AddToCache(imagePath, classes);
} }
if (!info.Classes.Contains(ExplorerEditor.CurrentAnnClass.Id) && ExplorerEditor.CurrentAnnClass.Id != -1) if (!info.Classes.Contains(ExplorerEditor.CurrentAnnClass.Id) && ExplorerEditor.CurrentAnnClass.Id != -1)
+19 -7
View File
@@ -148,20 +148,17 @@ public class GalleryManager : IGalleryManager
_logger.LogInformation($"No labels found for image {imgName}! Image deleted!"); _logger.LogInformation($"No labels found for image {imgName}! Image deleted!");
return null; return null;
} }
var labels = (await YoloLabel.ReadFromFile(labelName, cancellationToken)) var labels = (await YoloLabel.ReadFromFile(labelName, cancellationToken))
.Select(x => new CanvasLabel(x, size, size)) .Select(x => new CanvasLabel(x, size, size))
.ToList(); .ToList();
var classes = labels.Select(x => x.ClassNumber).Distinct().ToList();
AddToCache(imgPath, classes);
var thumbWhRatio = width / (float)height; var thumbWhRatio = width / (float)height;
var border = _config.ThumbnailConfig.Border; 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 frameX = 0.0;
var frameY = 0.0; var frameY = 0.0;
var frameHeight = size.Height; var frameHeight = size.Height;
@@ -223,6 +220,20 @@ public class GalleryManager : IGalleryManager
return null; return null;
} }
} }
public LabelInfo AddToCache(string imgPath, List<int> 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 public interface IGalleryManager
@@ -230,6 +241,7 @@ public interface IGalleryManager
event ThumbnailsUpdatedEventHandler ThumbnailsUpdate; event ThumbnailsUpdatedEventHandler ThumbnailsUpdate;
double ThumbnailsPercentage { get; set; } double ThumbnailsPercentage { get; set; }
Task SaveLabelsCache(); Task SaveLabelsCache();
LabelInfo AddToCache(string imgPath, List<int> classes);
ConcurrentDictionary<string, LabelInfo> LabelsCache { get; set; } ConcurrentDictionary<string, LabelInfo> LabelsCache { get; set; }
Task<ThumbnailDto?> CreateThumbnail(string imgPath, CancellationToken cancellationToken = default); Task<ThumbnailDto?> CreateThumbnail(string imgPath, CancellationToken cancellationToken = default);
Task RefreshThumbnails(); Task RefreshThumbnails();