Skip to content

Commit

Permalink
#125 Added background colours to text to make them more visible
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Done committed Jul 31, 2022
1 parent 579f19e commit 3f81a7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions SynoAI/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static class Config
/// The hex code of the colour to use for the exclusion boxes.
/// </summary>
public static string ExclusionBoxColor { get; private set; }
/// <summary>
/// The hex code of the colour to use behind the text on the image outputs.
/// </summary>
public static string TextBoxColor { get; private set; }

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

Font = configuration.GetValue<string>("Font", "Tahoma");
FontSize = configuration.GetValue<int>("FontSize", 12);
Expand Down
46 changes: 39 additions & 7 deletions SynoAI/Services/SnapshotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 3f81a7c

Please sign in to comment.