add db WIP 2, 80%

refactor, renames
This commit is contained in:
Alex Bezdieniezhnykh
2024-12-24 06:07:13 +02:00
parent 5fa18aa514
commit 48c9ccbfda
32 changed files with 499 additions and 459 deletions
+22 -11
View File
@@ -8,8 +8,10 @@ using Azaion.Common.DTO.Queue;
using Azaion.CommonSecurity.DTO;
using Azaion.CommonSecurity.Services;
using LinqToDB;
using MediatR;
using MessagePack;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using RabbitMQ.Stream.Client;
using RabbitMQ.Stream.Client.Reliable;
@@ -20,17 +22,23 @@ public class AnnotationService
private readonly AzaionApiClient _apiClient;
private readonly IDbFactory _dbFactory;
private readonly FailsafeAnnotationsProducer _producer;
private readonly IGalleryService _galleryService;
private readonly IMediator _mediator;
private readonly QueueConfig _queueConfig;
private Consumer _consumer = null!;
public AnnotationService(AzaionApiClient apiClient,
IDbFactory dbFactory,
FailsafeAnnotationsProducer producer,
IOptions<QueueConfig> queueConfig)
IOptions<QueueConfig> queueConfig,
IGalleryService galleryService,
IMediator mediator)
{
_apiClient = apiClient;
_dbFactory = dbFactory;
_producer = producer;
_galleryService = galleryService;
_mediator = mediator;
_queueConfig = queueConfig.Value;
Task.Run(async () => await Init()).Wait();
@@ -53,8 +61,8 @@ public class AnnotationService
}
//AI / Manual
public async Task SaveAnnotation(string fName, List<YoloLabel>? labels, SourceEnum source, MemoryStream? stream = null, CancellationToken token = default) =>
await SaveAnnotationInner(DateTime.UtcNow, fName, labels, source, stream, _apiClient.User.Role, _apiClient.User.Email, token);
public async Task SaveAnnotation(string fName, string imageExtension, List<Detection> detections, SourceEnum source, Stream? stream = null, CancellationToken token = default) =>
await SaveAnnotationInner(DateTime.UtcNow, fName, imageExtension, detections, source, stream, _apiClient.User.Role, _apiClient.User.Email, token);
//Queue (only from operators)
public async Task Consume(AnnotationCreatedMessage message, CancellationToken cancellationToken = default)
@@ -65,7 +73,8 @@ public class AnnotationService
await SaveAnnotationInner(
message.CreatedDate,
message.Name,
YoloLabel.Deserialize(message.Label),
message.ImageExtension,
JsonConvert.DeserializeObject<List<Detection>>(message.Detections) ?? [],
message.Source,
new MemoryStream(message.Image),
message.CreatedRole,
@@ -73,7 +82,7 @@ public class AnnotationService
cancellationToken);
}
private async Task SaveAnnotationInner(DateTime createdDate, string fName, List<YoloLabel>? labels, SourceEnum source, MemoryStream? stream,
private async Task SaveAnnotationInner(DateTime createdDate, string fName, string imageExtension, List<Detection> detections, SourceEnum source, Stream? stream,
RoleEnum createdRole,
string createdEmail,
CancellationToken token = default)
@@ -85,7 +94,7 @@ public class AnnotationService
// sourceEnum: (manual) if was in received.json then <AnnotationValidatedMessage> else <AnnotationCreatedMessage>
// sourceEnum: (queue, AI) if queue CreatedMessage with the same user - do nothing Add to received.json
var classes = labels?.Select(x => x.ClassNumber).Distinct().ToList() ?? [];
var classes = detections.Select(x => x.ClassNumber).Distinct().ToList() ?? [];
AnnotationStatus status;
var annotation = await _dbFactory.Run(async db =>
@@ -108,11 +117,12 @@ public class AnnotationService
{
CreatedDate = createdDate,
Name = fName,
Classes = classes,
ImageExtension = imageExtension,
CreatedEmail = createdEmail,
CreatedRole = createdRole,
AnnotationStatus = status,
Source = source
Source = source,
Detections = detections
};
await db.InsertAsync(ann, token: token);
}
@@ -124,9 +134,10 @@ public class AnnotationService
var img = System.Drawing.Image.FromStream(stream);
img.Save(annotation.ImagePath, ImageFormat.Jpeg); //todo: check png images coming from queue
}
if (labels != null)
await YoloLabel.WriteToFile(labels, annotation.LabelPath, token);
await YoloLabel.WriteToFile(detections, annotation.LabelPath, token);
await _galleryService.CreateThumbnail(annotation, token);
await _producer.SendToQueue(annotation, token);
await _mediator.Publish(new AnnotationCreatedEvent(annotation), token);
}
}
+4 -5
View File
@@ -8,6 +8,7 @@ using LinqToDB;
using MessagePack;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using RabbitMQ.Stream.Client;
using RabbitMQ.Stream.Client.Reliable;
@@ -28,14 +29,13 @@ public class FailsafeAnnotationsProducer
_logger = logger;
_dbFactory = dbFactory;
_queueConfig = queueConfig.Value;
Task.Run(async () => await ProcessQueue()).Wait();
Task.Run(async () => await ProcessQueue());
}
private async Task<StreamSystem> GetProducerQueueConfig()
{
return await StreamSystem.Create(new StreamSystemConfig
{
Endpoints = new List<EndPoint> { new IPEndPoint(IPAddress.Parse(_queueConfig.Host), _queueConfig.Port) },
UserName = _queueConfig.ProducerUsername,
Password = _queueConfig.ProducerPassword
@@ -45,7 +45,7 @@ public class FailsafeAnnotationsProducer
private async Task Init(CancellationToken cancellationToken = default)
{
_annotationProducer = await Producer.Create(new ProducerConfig(await GetProducerQueueConfig(), Constants.MQ_ANNOTATIONS_QUEUE));
//_annotationConfirmProducer = await Producer.Create(new ProducerConfig(await GetProducerQueueConfig(), Constants.MQ_ANNOTATIONS_CONFIRM_QUEUE));
_annotationConfirmProducer = await Producer.Create(new ProducerConfig(await GetProducerQueueConfig(), Constants.MQ_ANNOTATIONS_CONFIRM_QUEUE));
}
private async Task ProcessQueue(CancellationToken cancellationToken = default)
@@ -98,7 +98,6 @@ public class FailsafeAnnotationsProducer
foreach (var annotation in annotations)
{
var image = await File.ReadAllBytesAsync(annotation.ImagePath, cancellationToken);
var label = await File.ReadAllTextAsync(annotation.LabelPath, cancellationToken);
var annCreateMessage = new AnnotationCreatedMessage
{
Name = annotation.Name,
@@ -108,7 +107,7 @@ public class FailsafeAnnotationsProducer
CreatedDate = annotation.CreatedDate,
Image = image,
Label = label,
Detections = JsonConvert.SerializeObject(annotation.Detections),
Source = annotation.Source
};
messages.Add(annCreateMessage);
+251
View File
@@ -0,0 +1,251 @@
using System.Collections.Concurrent;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using Azaion.Annotator.Extensions;
using Azaion.Common.Database;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
using Azaion.Common.DTO.Queue;
using Azaion.CommonSecurity.DTO;
using LinqToDB;
using LinqToDB.Data;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Color = System.Drawing.Color;
using ParallelOptions = Azaion.Annotator.Extensions.ParallelOptions;
using Size = System.Windows.Size;
namespace Azaion.Common.Services;
public delegate void ThumbnailsUpdatedEventHandler(double thumbnailsPercentage);
public class GalleryService(
IOptions<DirectoriesConfig> directoriesConfig,
IOptions<ThumbnailConfig> thumbnailConfig,
IOptions<AnnotationConfig> annotationConfig,
ILogger<GalleryService> logger,
IDbFactory dbFactory) : IGalleryService
{
private readonly DirectoriesConfig _dirConfig = directoriesConfig.Value;
private readonly ThumbnailConfig _thumbnailConfig = thumbnailConfig.Value;
private readonly AnnotationConfig _annotationConfig = annotationConfig.Value;
public event ThumbnailsUpdatedEventHandler? ThumbnailsUpdate;
private readonly SemaphoreSlim _updateLock = new(1);
public double ProcessedThumbnailsPercentage { get; set; }
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 async Task ClearThumbnails(CancellationToken cancellationToken = default)
{
foreach(var file in new DirectoryInfo(_dirConfig.ThumbnailsDirectory).GetFiles())
file.Delete();
await dbFactory.Run(async db =>
{
await db.Detections.DeleteAsync(x => true, token: cancellationToken);
await db.Annotations.DeleteAsync(x => true, token: cancellationToken);
});
}
public async Task RefreshThumbnails()
{
await _updateLock.WaitAsync();
var existingAnnotations = new ConcurrentDictionary<string, Annotation>(await dbFactory.Run(async db =>
await db.Annotations.ToDictionaryAsync(x => x.Name)));
var missedAnnotations = new ConcurrentBag<Annotation>();
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();
var files = new DirectoryInfo(_dirConfig.ImagesDirectory).GetFiles();
var imagesCount = files.Length;
await ParallelExt.ForEachAsync(files, async (file, cancellationToken) =>
{
var fName = Path.GetFileNameWithoutExtension(file.Name);
try
{
var labelName = Path.Combine(_dirConfig.LabelsDirectory, $"{fName}.txt");
if (!File.Exists(labelName))
{
File.Delete(file.FullName);
logger.LogInformation($"No labels found for image {file.FullName}! Image deleted!");
return;
}
//Read labels file only when it needed
if (existingAnnotations.ContainsKey(fName) && thumbnails.Contains(fName))
return;
var detections = (await YoloLabel.ReadFromFile(labelName, cancellationToken)).Select(x => new Detection(fName, x)).ToList();
var annotation = new Annotation
{
Name = fName,
ImageExtension = Path.GetExtension(file.Name),
Detections = detections,
CreatedDate = File.GetCreationTimeUtc(file.FullName),
Source = SourceEnum.Manual,
CreatedRole = RoleEnum.Validator,
CreatedEmail = Constants.ADMIN_EMAIL,
AnnotationStatus = AnnotationStatus.Validated
};
if (!existingAnnotations.ContainsKey(fName))
missedAnnotations.Add(annotation);
if (!thumbnails.Contains(fName))
await CreateThumbnail(annotation, 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}");
ProcessedThumbnailsPercentage = imagesCount == 0 ? 0 : Math.Min(100, num * 100 / (double)imagesCount);
ThumbnailsUpdate?.Invoke(ProcessedThumbnailsPercentage);
await Task.CompletedTask;
},
CpuUtilPercent = 100,
ProgressUpdateInterval = 200
});
}
finally
{
var copyOptions = new BulkCopyOptions
{
MaxBatchSize = 50
};
await dbFactory.Run(async db =>
{
var xx = missedAnnotations.GroupBy(x => x.Name)
.Where(gr => gr.Count() > 1)
.ToList();
foreach (var gr in xx)
Console.WriteLine(gr.Key);
await db.BulkCopyAsync(copyOptions, missedAnnotations);
await db.BulkCopyAsync(copyOptions, missedAnnotations.SelectMany(x => x.Detections));
});
dbFactory.SaveToDisk();
_updateLock.Release();
}
}
public async Task CreateThumbnail(Annotation annotation, CancellationToken cancellationToken = default)
{
try
{
var width = (int)_thumbnailConfig.Size.Width;
var height = (int)_thumbnailConfig.Size.Height;
var originalImage = Image.FromStream(new MemoryStream(await File.ReadAllBytesAsync(annotation.ImagePath, 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);
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;
var labels = annotation.Detections
.Select(x => new CanvasLabel(x, size))
.ToList();
if (annotation.Detections.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.DetectionClassesDict[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);
}
bitmap.Save(annotation.ThumbPath, ImageFormat.Jpeg);
}
catch (Exception e)
{
logger.LogError(e, e.Message);
}
}
}
public interface IGalleryService
{
event ThumbnailsUpdatedEventHandler? ThumbnailsUpdate;
double ProcessedThumbnailsPercentage { get; set; }
Task CreateThumbnail(Annotation annotation, CancellationToken cancellationToken = default);
Task RefreshThumbnails();
Task ClearThumbnails(CancellationToken cancellationToken = default);
}