From 36b3bf1712e2ad54ae47478f5d1d16237a9e8c17 Mon Sep 17 00:00:00 2001 From: Alex Bezdieniezhnykh Date: Thu, 3 Apr 2025 11:56:01 +0300 Subject: [PATCH] put better caption in result image --- .../Extensions/GraphicsExtensions.cs | 28 +++++++++++++++++++ Azaion.Common/Services/GalleryService.cs | 3 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Azaion.Common/Extensions/GraphicsExtensions.cs diff --git a/Azaion.Common/Extensions/GraphicsExtensions.cs b/Azaion.Common/Extensions/GraphicsExtensions.cs new file mode 100644 index 0000000..9a39672 --- /dev/null +++ b/Azaion.Common/Extensions/GraphicsExtensions.cs @@ -0,0 +1,28 @@ +using System.Drawing; + +namespace Azaion.Common.Extensions; + +public static class GraphicsExtensions +{ + public static void DrawTextBox(this Graphics g, string text, PointF position, Brush background, Brush foreground) + { + using var textFont = new Font(FontFamily.GenericSerif, 14); + using var stringFormat = new StringFormat(); + stringFormat.LineAlignment = StringAlignment.Near; + stringFormat.Alignment = StringAlignment.Center; + + var padding = 1.0f; + var textSize = g.MeasureString(text, textFont); + + var backgroundRect = new RectangleF( + position.X - textSize.Width / 2.0f - padding, + position.Y - padding, + textSize.Width + 2 * padding, + textSize.Height + 2 * padding + ); + + g.FillRectangle(background, backgroundRect); + g.DrawString(text, textFont, foreground, position, stringFormat); + } + +} \ No newline at end of file diff --git a/Azaion.Common/Services/GalleryService.cs b/Azaion.Common/Services/GalleryService.cs index 65d8526..5331e6b 100644 --- a/Azaion.Common/Services/GalleryService.cs +++ b/Azaion.Common/Services/GalleryService.cs @@ -278,8 +278,9 @@ public class GalleryService( 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)); + g.DrawTextBox(label, new PointF((float)(det.X + det.Width / 2.0), (float)(det.Y - 24)), brush, Brushes.Black); } originalImage.Save(Path.Combine(_dirConfig.ResultsDirectory, $"{annotation.Name}{Constants.RESULT_PREFIX}.jpg"), ImageFormat.Jpeg); }