mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 21:06:30 +00:00
add generating result image to Results directory
This commit is contained in:
@@ -63,6 +63,7 @@ public class Constants
|
||||
public const int DEFAULT_THUMBNAIL_BORDER = 10;
|
||||
|
||||
public const string THUMBNAIL_PREFIX = "_thumb";
|
||||
public const string RESULT_PREFIX = "_result";
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -121,7 +121,7 @@ public class DetectionControl : Border
|
||||
var rect = new Rectangle() // small rectangles at the corners and sides
|
||||
{
|
||||
ClipToBounds = false,
|
||||
Margin = new Thickness(-RESIZE_RECT_SIZE),
|
||||
Margin = new Thickness(-RESIZE_RECT_SIZE * 0.7),
|
||||
HorizontalAlignment = ha,
|
||||
VerticalAlignment = va,
|
||||
Width = RESIZE_RECT_SIZE,
|
||||
|
||||
@@ -58,6 +58,7 @@ public class ConfigUpdater : IConfigUpdater
|
||||
{
|
||||
LeftPanelWidth = Constants.DEFAULT_LEFT_PANEL_WIDTH,
|
||||
RightPanelWidth = Constants.DEFAULT_RIGHT_PANEL_WIDTH,
|
||||
GenerateAnnotatedImage = false
|
||||
},
|
||||
|
||||
DirectoriesConfig = new DirectoriesConfig
|
||||
|
||||
@@ -4,4 +4,5 @@ public class UIConfig
|
||||
{
|
||||
public double LeftPanelWidth { get; set; }
|
||||
public double RightPanelWidth { get; set; }
|
||||
}
|
||||
public bool GenerateAnnotatedImage { get; set; }
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
private readonly IAuthProvider _authProvider;
|
||||
private readonly QueueConfig _queueConfig;
|
||||
private Consumer _consumer = null!;
|
||||
private readonly UIConfig _uiConfig;
|
||||
private static readonly Guid SaveTaskId = Guid.NewGuid();
|
||||
|
||||
public AnnotationService(
|
||||
@@ -37,6 +38,7 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
IDbFactory dbFactory,
|
||||
FailsafeAnnotationsProducer producer,
|
||||
IOptions<QueueConfig> queueConfig,
|
||||
IOptions<UIConfig> uiConfig,
|
||||
IGalleryService galleryService,
|
||||
IMediator mediator,
|
||||
IHardwareService hardwareService,
|
||||
@@ -49,6 +51,7 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
_hardwareService = hardwareService;
|
||||
_authProvider = authProvider;
|
||||
_queueConfig = queueConfig.Value;
|
||||
_uiConfig = uiConfig.Value;
|
||||
|
||||
Task.Run(async () => await Init()).Wait();
|
||||
}
|
||||
@@ -184,7 +187,12 @@ public class AnnotationService : INotificationHandler<AnnotationsDeletedEvent>
|
||||
}
|
||||
await YoloLabel.WriteToFile(detections, annotation.LabelPath, token);
|
||||
if (generateThumbnail)
|
||||
{
|
||||
await _galleryService.CreateThumbnail(annotation, token);
|
||||
if (_uiConfig.GenerateAnnotatedImage)
|
||||
await _galleryService.CreateAnnotatedImage(annotation, token);
|
||||
}
|
||||
|
||||
|
||||
if (!fromQueue) //Send to queue only if we're not getting from queue already
|
||||
await _producer.SendToInnerQueue(annotation, token);
|
||||
|
||||
@@ -265,6 +265,24 @@ public class GalleryService(
|
||||
logger.LogError(e, e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateAnnotatedImage(Annotation annotation, CancellationToken token)
|
||||
{
|
||||
var originalImage = Image.FromStream(new MemoryStream(await File.ReadAllBytesAsync(annotation.ImagePath, token)));
|
||||
|
||||
using var g = Graphics.FromImage(originalImage);
|
||||
foreach (var detection in annotation.Detections)
|
||||
{
|
||||
var detClass = _annotationConfig.DetectionClassesDict[detection.ClassNumber];
|
||||
var color = detClass.Color;
|
||||
var brush = new SolidBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
|
||||
var det = new CanvasLabel(detection, new Size(originalImage.Width, originalImage.Height));
|
||||
g.DrawRectangle(new Pen(brush, width: 3), (float)det.X, (float)det.Y, (float)det.Width, (float)det.Height);
|
||||
var label = detection.Confidence >= 0.995 ? detClass.UIName : $"{detClass.UIName}: {detection.Confidence * 100:F0}%";
|
||||
g.DrawString(label, new Font(FontFamily.GenericSerif, 14), brush, new PointF((float)(det.X + det.Width / 2.0), (float)det.Y));
|
||||
}
|
||||
originalImage.Save(Path.Combine(_dirConfig.ResultsDirectory, $"{annotation.Name}{Constants.RESULT_PREFIX}.jpg"), ImageFormat.Jpeg);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IGalleryService
|
||||
@@ -275,4 +293,5 @@ public interface IGalleryService
|
||||
Task RefreshThumbnails();
|
||||
Task ClearThumbnails(CancellationToken cancellationToken = default);
|
||||
|
||||
Task CreateAnnotatedImage(Annotation annotation, CancellationToken token);
|
||||
}
|
||||
Reference in New Issue
Block a user