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
27 changes: 20 additions & 7 deletions supervision/annotators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,6 @@ def annotate(
detections: Detections,
labels: Optional[List[str]] = None,
custom_color_lookup: Optional[np.ndarray] = None,
custom_text_color_lookup: Optional[np.ndarray] = None,
) -> ImageType:
"""
Annotates the given scene with labels based on the provided detections.
Expand All @@ -1068,9 +1067,7 @@ def annotate(
labels (Optional[List[str]]): Custom labels for each detection.
custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
Allows to override the default color mapping strategy.
custom_text_color_lookup (Optional[np.ndarray]): Custom text color lookup
array. Allows to override the default text color mapping strategy.


Returns:
The annotated image, matching the type of `scene` (`numpy.ndarray`
or `PIL.Image.Image`)
Expand Down Expand Up @@ -1128,8 +1125,8 @@ def annotate(
detection_idx=detection_idx,
color_lookup=(
self.text_color_lookup
if custom_text_color_lookup is None
else custom_text_color_lookup
if custom_color_lookup is None
else custom_color_lookup
),
)

Expand Down Expand Up @@ -1235,6 +1232,7 @@ def __init__(
text_padding: int = 10,
text_position: Position = Position.TOP_LEFT,
color_lookup: ColorLookup = ColorLookup.CLASS,
text_color_lookup: ColorLookup = ColorLookup.CLASS,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's remove this parameter and use only color_lookup.

border_radius: int = 0,
):
"""
Expand All @@ -1250,6 +1248,8 @@ def __init__(
Possible values are defined in the `Position` enum.
color_lookup (ColorLookup): Strategy for mapping colors to annotations.
Options are `INDEX`, `CLASS`, `TRACK`.
text_color_lookup (ColorLookup): Strategy for mapping text colors to
annotations. Options are `INDEX`, `CLASS`, `TRACK`.
border_radius (int): The radius to apply round edges. If the selected
value is higher than the lower dimension, width or height, is clipped.
"""
Expand All @@ -1258,6 +1258,7 @@ def __init__(
self.text_padding = text_padding
self.text_anchor = text_position
self.color_lookup = color_lookup
self.text_color_lookup: ColorLookup = text_color_lookup
self.border_radius = border_radius
if font_path is not None:
try:
Expand Down Expand Up @@ -1336,6 +1337,18 @@ def annotate(
else custom_color_lookup
),
)

text_color = resolve_color(
color=self.text_color,
detections=detections,
detection_idx=detection_idx,
color_lookup=(
self.text_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 @@ -1369,7 +1382,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