Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Union[Color, ColorPalette] to text_color parameter #1387

Merged
merged 14 commits into from
Jul 26, 2024
Merged
38 changes: 31 additions & 7 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ class LabelAnnotator(BaseAnnotator):
def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.DEFAULT,
text_color: Color = Color.WHITE,
text_color: Union[Color, ColorPalette] = Color.WHITE,
text_scale: float = 0.5,
text_thickness: int = 1,
text_padding: int = 10,
Expand All @@ -1023,7 +1023,8 @@ def __init__(
Args:
color (Union[Color, ColorPalette]): The color or color palette to use for
annotating the text background.
text_color (Color): The color to use for the text.
text_color (Union[Color, ColorPalette]): The color or color palette to use
for the text.
text_scale (float): Font scale for the text.
text_thickness (int): Thickness of the text characters.
text_padding (int): Padding around the text within its background box.
Expand All @@ -1036,7 +1037,7 @@ def __init__(
"""
self.border_radius: int = border_radius
self.color: Union[Color, ColorPalette] = color
self.text_color: Color = text_color
self.text_color: Union[Color, ColorPalette] = text_color
self.text_scale: float = text_scale
self.text_thickness: int = text_thickness
self.text_padding: int = text_padding
Expand Down Expand Up @@ -1114,6 +1115,17 @@ def annotate(
),
)

text_color = resolve_color(
color=self.text_color,
detections=detections,
detection_idx=detection_idx,
color_lookup=(
self.color_lookup
if custom_color_lookup is None
else custom_color_lookup
),
)

if labels is not None:
text = labels[detection_idx]
elif detections[CLASS_NAME_DATA_FIELD] is not None:
Expand Down Expand Up @@ -1152,7 +1164,7 @@ def annotate(
org=(text_x, text_y),
fontFace=font,
fontScale=self.text_scale,
color=self.text_color.as_bgr(),
color=text_color.as_bgr(),
thickness=self.text_thickness,
lineType=cv2.LINE_AA,
)
Expand Down Expand Up @@ -1210,7 +1222,7 @@ class RichLabelAnnotator(BaseAnnotator):
def __init__(
self,
color: Union[Color, ColorPalette] = ColorPalette.DEFAULT,
text_color: Color = Color.WHITE,
text_color: Union[Color, ColorPalette] = Color.WHITE,
font_path: Optional[str] = None,
font_size: int = 10,
text_padding: int = 10,
Expand All @@ -1222,7 +1234,7 @@ def __init__(
Args:
color (Union[Color, ColorPalette]): The color or color palette to use for
annotating the text background.
text_color (Color): The color to use for the text.
text_color (Union[Color, ColorPalette]): The color to use for the text.
font_path (Optional[str]): Path to the font file (e.g., ".ttf" or ".otf")
to use for rendering text. If `None`, the default PIL font will be used.
font_size (int): Font size for the text.
Expand Down Expand Up @@ -1317,6 +1329,18 @@ def annotate(
else custom_color_lookup
),
)

text_color = resolve_color(
color=self.text_color,
detections=detections,
detection_idx=detection_idx,
color_lookup=(
self.color_lookup
if custom_color_lookup is None
else custom_color_lookup
),
)

if labels is not None:
text = labels[detection_idx]
elif detections[CLASS_NAME_DATA_FIELD] is not None:
Expand Down Expand Up @@ -1350,7 +1374,7 @@ def annotate(
xy=(text_x, text_y),
text=text,
font=self.font,
fill=self.text_color.as_rgb(),
fill=text_color.as_rgb(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this, do we need to change as_rgb() to as_bgr() same like we did in LabelAnnotator?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RichLabelAnnotator uses Pillow as a rendering engine. Pillow accepts colors in RGB order, while OpenCV in BGR. So we keep as_rgb() here.

)
return scene

Expand Down