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
+13 -15
View File
@@ -273,7 +273,7 @@ public partial class DatasetExplorer
var thumbnailDtos = new List<ThumbnailDto>();
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<ThumbnailDto?> 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,27 +307,23 @@ 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");
}
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)
return null;
+19 -7
View File
@@ -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<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
@@ -230,6 +241,7 @@ public interface IGalleryManager
event ThumbnailsUpdatedEventHandler ThumbnailsUpdate;
double ThumbnailsPercentage { get; set; }
Task SaveLabelsCache();
LabelInfo AddToCache(string imgPath, List<int> classes);
ConcurrentDictionary<string, LabelInfo> LabelsCache { get; set; }
Task<ThumbnailDto?> CreateThumbnail(string imgPath, CancellationToken cancellationToken = default);
Task RefreshThumbnails();