diff --git a/README.md b/README.md index 6b9721d..6b2bc9c 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ An example appsettings.json configuration file can be found [here](#example-apps * Off: Will not draw boundary boxes (note - this will speed up time between detection and notification as SynoAI will not have to manipulate the image) * DrawExclusions [optional] (Default: ```false```): Whether to draw the exclusion zone boundary boxes on the image. Useful for setting up the initial exclusion zones * BoxColor [optional] (Default: ```#FF0000```): The colour of the border of the boundary box +* TextBoxColor [opional] (Default: ```#00FFFFFF``` aka ```transparent```): The colour of the box to draw behind the text to aid in making text more visible * ExclusionBoxColour [optional] (Default: ```#00FF00```): The colour of the border of the exclusion boundary box * StrokeWidth [optional] (Default: ```2```): The width, in pixels, of the border around the boundary box * Font [optional] (Default: ```Tahoma```): The font to use when labelling the boundary boxes on the output image diff --git a/SynoAI/Config.cs b/SynoAI/Config.cs index a5b49e8..9232c1f 100644 --- a/SynoAI/Config.cs +++ b/SynoAI/Config.cs @@ -55,6 +55,10 @@ public static class Config /// The hex code of the colour to use for the exclusion boxes. /// public static string ExclusionBoxColor { get; private set; } + /// + /// The hex code of the colour to use behind the text on the image outputs. + /// + public static string TextBoxColor { get; private set; } /// ///The stroke width of the Box drawn around the objects. @@ -192,6 +196,7 @@ public static void Generate(ILogger logger, IConfiguration configuration) BoxColor = configuration.GetValue("BoxColor", SKColors.Green.ToString()); FontColor = configuration.GetValue("FontColor", SKColors.Green.ToString()); ExclusionBoxColor = configuration.GetValue("ExclusionBoxColor", SKColors.Red.ToString()); + TextBoxColor = configuration.GetValue("TextBoxColor", SKColors.Transparent.ToString()); Font = configuration.GetValue("Font", "Tahoma"); FontSize = configuration.GetValue("FontSize", 12); diff --git a/SynoAI/Services/SnapshotManager.cs b/SynoAI/Services/SnapshotManager.cs index c8af550..6751fa1 100644 --- a/SynoAI/Services/SnapshotManager.cs +++ b/SynoAI/Services/SnapshotManager.cs @@ -92,20 +92,52 @@ public static ProcessedImage DressImage(Camera camera, byte[] snapshot, IEnumera // Label positioning int x = prediction.MinX + Config.TextOffsetX; - int y = prediction.MinY + Config.FontSize + Config.TextOffsetY; + int y = prediction.MinY + Config.FontSize + Config.TextOffsetY; // FontSize is added as text is drawn above the bottom co-ordinate // Consider below box placement if (Config.LabelBelowBox) { y += prediction.SizeY; } - - // Draw the text - SKFont font = new SKFont(SKTypeface.FromFamilyName(Config.Font), Config.FontSize); - canvas.DrawText(label, x, y, font, new SKPaint + + // Draw background box for the text if required + SKTypeface typeface = SKTypeface.FromFamilyName(Config.Font); + + SKPaint paint = new SKPaint { - Color = GetColour(Config.FontColor) - }); + FilterQuality = SKFilterQuality.High, + IsAntialias = true, + Color = GetColour(Config.FontColor), + TextSize = Config.FontSize, + Typeface = typeface + }; + + string textBoxColor = Config.TextBoxColor; + if (!string.IsNullOrWhiteSpace(textBoxColor) && !textBoxColor.Equals(SKColors.Transparent.ToString(), StringComparison.OrdinalIgnoreCase)) + { + float textWidth = paint.MeasureText(label); + float textBoxWidth = textWidth + (Config.TextOffsetX * 2); + float textBoxHeight = Config.FontSize + (Config.TextOffsetY * 2); + + float textBoxX = prediction.MinX + Config.StrokeWidth; + float textBoxY = prediction.MinY + Config.TextOffsetY; + if (Config.LabelBelowBox) + { + textBoxY += prediction.SizeY; + } + + SKRect textRectangle = SKRect.Create(textBoxX, textBoxY, textBoxWidth, textBoxHeight); + canvas.DrawRect(textRectangle, new SKPaint + { + Style = SKPaintStyle.StrokeAndFill, + Color = GetColour(textBoxColor), + StrokeWidth = Config.StrokeWidth + }); + } + + // Draw the text + SKFont font = new SKFont(typeface, Config.FontSize); + canvas.DrawText(label, x, y, paint); } } }