Skip to content

Commit 3f81a7c

Browse files
author
Dan Done
committed
#125 Added background colours to text to make them more visible
1 parent 579f19e commit 3f81a7c

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ An example appsettings.json configuration file can be found [here](#example-apps
9191
* 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)
9292
* DrawExclusions [optional] (Default: ```false```): Whether to draw the exclusion zone boundary boxes on the image. Useful for setting up the initial exclusion zones
9393
* BoxColor [optional] (Default: ```#FF0000```): The colour of the border of the boundary box
94+
* TextBoxColor [opional] (Default: ```#00FFFFFF``` aka ```transparent```): The colour of the box to draw behind the text to aid in making text more visible
9495
* ExclusionBoxColour [optional] (Default: ```#00FF00```): The colour of the border of the exclusion boundary box
9596
* StrokeWidth [optional] (Default: ```2```): The width, in pixels, of the border around the boundary box
9697
* Font [optional] (Default: ```Tahoma```): The font to use when labelling the boundary boxes on the output image

SynoAI/Config.cs

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public static class Config
5555
/// The hex code of the colour to use for the exclusion boxes.
5656
/// </summary>
5757
public static string ExclusionBoxColor { get; private set; }
58+
/// <summary>
59+
/// The hex code of the colour to use behind the text on the image outputs.
60+
/// </summary>
61+
public static string TextBoxColor { get; private set; }
5862

5963
/// <summary>
6064
///The stroke width of the Box drawn around the objects.
@@ -192,6 +196,7 @@ public static void Generate(ILogger logger, IConfiguration configuration)
192196
BoxColor = configuration.GetValue<string>("BoxColor", SKColors.Green.ToString());
193197
FontColor = configuration.GetValue<string>("FontColor", SKColors.Green.ToString());
194198
ExclusionBoxColor = configuration.GetValue<string>("ExclusionBoxColor", SKColors.Red.ToString());
199+
TextBoxColor = configuration.GetValue<string>("TextBoxColor", SKColors.Transparent.ToString());
195200

196201
Font = configuration.GetValue<string>("Font", "Tahoma");
197202
FontSize = configuration.GetValue<int>("FontSize", 12);

SynoAI/Services/SnapshotManager.cs

+39-7
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,52 @@ public static ProcessedImage DressImage(Camera camera, byte[] snapshot, IEnumera
9292

9393
// Label positioning
9494
int x = prediction.MinX + Config.TextOffsetX;
95-
int y = prediction.MinY + Config.FontSize + Config.TextOffsetY;
95+
int y = prediction.MinY + Config.FontSize + Config.TextOffsetY; // FontSize is added as text is drawn above the bottom co-ordinate
9696

9797
// Consider below box placement
9898
if (Config.LabelBelowBox)
9999
{
100100
y += prediction.SizeY;
101101
}
102-
103-
// Draw the text
104-
SKFont font = new SKFont(SKTypeface.FromFamilyName(Config.Font), Config.FontSize);
105-
canvas.DrawText(label, x, y, font, new SKPaint
102+
103+
// Draw background box for the text if required
104+
SKTypeface typeface = SKTypeface.FromFamilyName(Config.Font);
105+
106+
SKPaint paint = new SKPaint
106107
{
107-
Color = GetColour(Config.FontColor)
108-
});
108+
FilterQuality = SKFilterQuality.High,
109+
IsAntialias = true,
110+
Color = GetColour(Config.FontColor),
111+
TextSize = Config.FontSize,
112+
Typeface = typeface
113+
};
114+
115+
string textBoxColor = Config.TextBoxColor;
116+
if (!string.IsNullOrWhiteSpace(textBoxColor) && !textBoxColor.Equals(SKColors.Transparent.ToString(), StringComparison.OrdinalIgnoreCase))
117+
{
118+
float textWidth = paint.MeasureText(label);
119+
float textBoxWidth = textWidth + (Config.TextOffsetX * 2);
120+
float textBoxHeight = Config.FontSize + (Config.TextOffsetY * 2);
121+
122+
float textBoxX = prediction.MinX + Config.StrokeWidth;
123+
float textBoxY = prediction.MinY + Config.TextOffsetY;
124+
if (Config.LabelBelowBox)
125+
{
126+
textBoxY += prediction.SizeY;
127+
}
128+
129+
SKRect textRectangle = SKRect.Create(textBoxX, textBoxY, textBoxWidth, textBoxHeight);
130+
canvas.DrawRect(textRectangle, new SKPaint
131+
{
132+
Style = SKPaintStyle.StrokeAndFill,
133+
Color = GetColour(textBoxColor),
134+
StrokeWidth = Config.StrokeWidth
135+
});
136+
}
137+
138+
// Draw the text
139+
SKFont font = new SKFont(typeface, Config.FontSize);
140+
canvas.DrawText(label, x, y, paint);
109141
}
110142
}
111143
}

0 commit comments

Comments
 (0)