mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:46:30 +00:00
253 lines
9.8 KiB
C#
253 lines
9.8 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using Azaion.Annotator.Extensions;
|
|
using Azaion.Common;
|
|
using Azaion.Common.DTO;
|
|
using Azaion.Common.Extensions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using Color = System.Drawing.Color;
|
|
using ParallelOptions = Azaion.Annotator.Extensions.ParallelOptions;
|
|
using Size = System.Windows.Size;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Drawing2D;
|
|
using Azaion.Common.DTO.Config;
|
|
|
|
namespace Azaion.Dataset;
|
|
|
|
public delegate void ThumbnailsUpdatedEventHandler(double thumbnailsPercentage);
|
|
|
|
public class GalleryManager(
|
|
IOptions<DirectoriesConfig> directoriesConfig,
|
|
IOptions<ThumbnailConfig> thumbnailConfig,
|
|
IOptions<AnnotationConfig> annotationConfig,
|
|
ILogger<GalleryManager> logger) : IGalleryManager
|
|
{
|
|
private readonly DirectoriesConfig _dirConfig = directoriesConfig.Value;
|
|
private readonly ThumbnailConfig _thumbnailConfig = thumbnailConfig.Value;
|
|
private readonly AnnotationConfig _annotationConfig = annotationConfig.Value;
|
|
|
|
private readonly string _thumbnailsCacheFile = Path.Combine(directoriesConfig.Value.ThumbnailsDirectory, Constants.THUMBNAILS_CACHE_FILE);
|
|
public event ThumbnailsUpdatedEventHandler? ThumbnailsUpdate;
|
|
|
|
|
|
private readonly SemaphoreSlim _updateLock = new(1);
|
|
|
|
public double ThumbnailsPercentage { get; set; }
|
|
public ConcurrentDictionary<string, LabelInfo> LabelsCache { get; set; } = new();
|
|
|
|
private DirectoryInfo? _thumbnailsDirectory;
|
|
|
|
private DirectoryInfo ThumbnailsDirectory
|
|
{
|
|
get
|
|
{
|
|
if (_thumbnailsDirectory != null)
|
|
return _thumbnailsDirectory;
|
|
|
|
var dir = new DirectoryInfo(_dirConfig.ThumbnailsDirectory);
|
|
if (!dir.Exists)
|
|
Directory.CreateDirectory(_dirConfig.ThumbnailsDirectory);
|
|
_thumbnailsDirectory = new DirectoryInfo(_dirConfig.ThumbnailsDirectory);
|
|
return _thumbnailsDirectory;
|
|
}
|
|
}
|
|
|
|
public void ClearThumbnails()
|
|
{
|
|
foreach(var file in new DirectoryInfo(_dirConfig.ThumbnailsDirectory).GetFiles())
|
|
file.Delete();
|
|
}
|
|
|
|
public async Task RefreshThumbnails()
|
|
{
|
|
await _updateLock.WaitAsync();
|
|
try
|
|
{
|
|
var prefixLen = Constants.THUMBNAIL_PREFIX.Length;
|
|
|
|
var thumbnails = ThumbnailsDirectory.GetFiles()
|
|
.Select(x => Path.GetFileNameWithoutExtension(x.Name)[..^prefixLen])
|
|
.GroupBy(x => x)
|
|
.Select(gr => gr.Key)
|
|
.ToHashSet();
|
|
|
|
if (File.Exists(_thumbnailsCacheFile))
|
|
{
|
|
var cache = JsonConvert.DeserializeObject<ConcurrentDictionary<string, LabelInfo>>(
|
|
await File.ReadAllTextAsync(_thumbnailsCacheFile), new DenseDateTimeConverter());
|
|
LabelsCache = cache ?? new ConcurrentDictionary<string, LabelInfo>();
|
|
}
|
|
else
|
|
LabelsCache = new ConcurrentDictionary<string, LabelInfo>();
|
|
|
|
var files = new DirectoryInfo(_dirConfig.ImagesDirectory).GetFiles();
|
|
var imagesCount = files.Length;
|
|
|
|
await ParallelExt.ForEachAsync(files, async (file, cancellationToken) =>
|
|
{
|
|
var imgName = Path.GetFileNameWithoutExtension(file.Name);
|
|
if (thumbnails.Contains(imgName))
|
|
return;
|
|
try
|
|
{
|
|
await CreateThumbnail(file.FullName, cancellationToken);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, $"Failed to generate thumbnail for {file.Name}! Error: {e.Message}");
|
|
}
|
|
}, new ParallelOptions
|
|
{
|
|
ProgressFn = async num =>
|
|
{
|
|
Console.WriteLine($"Processed {num} item by Thread {Environment.CurrentManagedThreadId}");
|
|
ThumbnailsPercentage = imagesCount == 0 ? 0 : Math.Min(100, num * 100 / (double)imagesCount);
|
|
ThumbnailsUpdate?.Invoke(ThumbnailsPercentage);
|
|
await Task.CompletedTask;
|
|
},
|
|
CpuUtilPercent = 100,
|
|
ProgressUpdateInterval = 200
|
|
});
|
|
}
|
|
finally
|
|
{
|
|
await SaveLabelsCache();
|
|
_updateLock.Release();
|
|
}
|
|
}
|
|
|
|
public async Task SaveLabelsCache()
|
|
{
|
|
var labelsCacheStr = JsonConvert.SerializeObject(LabelsCache, new DenseDateTimeConverter());
|
|
await File.WriteAllTextAsync(_thumbnailsCacheFile, labelsCacheStr);
|
|
}
|
|
|
|
public async Task<(ThumbnailDto? thumbnailDto, List<int>? classes)> CreateThumbnail(string imgPath, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
var width = (int)_thumbnailConfig.Size.Width;
|
|
var height = (int)_thumbnailConfig.Size.Height;
|
|
|
|
var imgName = Path.GetFileName(imgPath);
|
|
var labelName = Path.Combine(_dirConfig.LabelsDirectory, $"{Path.GetFileNameWithoutExtension(imgPath)}.txt");
|
|
|
|
var originalImage = Image.FromStream(new MemoryStream(await File.ReadAllBytesAsync(imgPath, cancellationToken)));
|
|
|
|
var bitmap = new Bitmap(width, height);
|
|
|
|
using var g = Graphics.FromImage(bitmap);
|
|
g.CompositingQuality = CompositingQuality.HighSpeed;
|
|
g.SmoothingMode = SmoothingMode.HighSpeed;
|
|
g.InterpolationMode = InterpolationMode.Default;
|
|
|
|
var size = new Size(originalImage.Width, originalImage.Height);
|
|
if (!File.Exists(labelName))
|
|
{
|
|
File.Delete(imgPath);
|
|
logger.LogInformation($"No labels found for image {imgName}! Image deleted!");
|
|
return (null, 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 = _thumbnailConfig.Border;
|
|
|
|
var frameX = 0.0;
|
|
var frameY = 0.0;
|
|
var frameHeight = size.Height;
|
|
var frameWidth = size.Width;
|
|
|
|
if (labels.Any())
|
|
{
|
|
var labelsMinX = labels.Min(x => x.X);
|
|
var labelsMaxX = labels.Max(x => x.X + x.Width);
|
|
|
|
var labelsMinY = labels.Min(x => x.Y);
|
|
var labelsMaxY = labels.Max(x => x.Y + x.Height);
|
|
|
|
var labelsHeight = labelsMaxY - labelsMinY + 2 * border;
|
|
var labelsWidth = labelsMaxX - labelsMinX + 2 * border;
|
|
|
|
if (labelsWidth / labelsHeight > thumbWhRatio)
|
|
{
|
|
frameWidth = labelsWidth;
|
|
frameHeight = Math.Min(labelsWidth / thumbWhRatio, size.Height);
|
|
frameX = Math.Max(0, labelsMinX - border);
|
|
frameY = Math.Max(0, 0.5 * (labelsMinY + labelsMaxY - frameHeight) - border);
|
|
}
|
|
else
|
|
{
|
|
frameHeight = labelsHeight;
|
|
frameWidth = Math.Min(labelsHeight * thumbWhRatio, size.Width);
|
|
frameY = Math.Max(0, labelsMinY - border);
|
|
frameX = Math.Max(0, 0.5 * (labelsMinX + labelsMaxX - frameWidth) - border);
|
|
}
|
|
}
|
|
|
|
var scale = frameHeight / height;
|
|
g.DrawImage(originalImage, new Rectangle(0, 0, width, height), new RectangleF((float)frameX, (float)frameY, (float)frameWidth, (float)frameHeight), GraphicsUnit.Pixel);
|
|
|
|
foreach (var label in labels)
|
|
{
|
|
var color = _annotationConfig.AnnotationClassesDict[label.ClassNumber].Color;
|
|
var brush = new SolidBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
|
|
|
|
var rectangle = new RectangleF((float)((label.X - frameX) / scale), (float)((label.Y - frameY) / scale), (float)(label.Width / scale), (float)(label.Height / scale));
|
|
g.FillRectangle(brush, rectangle);
|
|
}
|
|
|
|
var thumbnailName = Path.Combine(ThumbnailsDirectory.FullName, $"{Path.GetFileNameWithoutExtension(imgPath)}{Constants.THUMBNAIL_PREFIX}.jpg");
|
|
bitmap.Save(thumbnailName, ImageFormat.Jpeg);
|
|
|
|
var thumbnailDto = new ThumbnailDto
|
|
{
|
|
ThumbnailPath = thumbnailName,
|
|
ImagePath = imgPath,
|
|
LabelPath = labelName,
|
|
ImageDate = File.GetCreationTimeUtc(imgPath)
|
|
};
|
|
return (thumbnailDto, classes);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, e.Message);
|
|
return (null, 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
|
|
{
|
|
event ThumbnailsUpdatedEventHandler? ThumbnailsUpdate;
|
|
double ThumbnailsPercentage { get; set; }
|
|
Task SaveLabelsCache();
|
|
LabelInfo AddToCache(string imgPath, List<int> classes);
|
|
ConcurrentDictionary<string, LabelInfo> LabelsCache { get; set; }
|
|
Task<(ThumbnailDto? thumbnailDto, List<int>? classes)> CreateThumbnail(string imgPath, CancellationToken cancellationToken = default);
|
|
Task RefreshThumbnails();
|
|
void ClearThumbnails();
|
|
} |