From 1a7309e7c9f7d0d9c699e1c29c7adb77fa3c5b7e Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Tue, 9 Apr 2024 16:51:41 +0530 Subject: [PATCH 01/10] RichLabelAnnotator class added --- supervision/annotators/core.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 9f6cdb36..d6ae599e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -3,6 +3,7 @@ from typing import List, Optional, Tuple, Union import cv2 import numpy as np +from PIL import ImageFont from supervision.annotators.base import BaseAnnotator, ImageType from supervision.annotators.utils import ColorLookup, Trace, resolve_color @@ -1147,6 +1148,27 @@ class LabelAnnotator: ) return scene +class RichLabelAnnotator: + + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, + text_color: Color = Color.WHITE, + font_path: str = "/content/Arial Unicode Font.ttf", + font_size: int = 14, + text_padding: int = 10, + text_position: Position = Position.TOP_LEFT, + color_lookup: ColorLookup = ColorLookup.CLASS, + border_radius: int = 0, + ): + self.color = color + self.text_color = text_color + self.font = ImageFont.truetype(font_path, font_size) + self.text_padding = text_padding + self.text_anchor = text_position + self.color_lookup = color_lookup + self.border_radius = border_radius + class BlurAnnotator(BaseAnnotator): """ From 2417b36006c48583a48883ba22d495f77b09273b Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Tue, 9 Apr 2024 20:39:53 +0530 Subject: [PATCH 02/10] resolve_text_background_xyxy() function added to RichLabelAnnotator --- supervision/annotators/core.py | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index d6ae599e..a86d9f9e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1168,6 +1168,59 @@ class RichLabelAnnotator: self.text_anchor = text_position self.color_lookup = color_lookup self.border_radius = border_radius + + @staticmethod + def resolve_text_background_xyxy( + center_coordinates: Tuple[int, int], + text_wh: Tuple[int, int], + position: Position, + ) -> Tuple[int, int, int, int]: + center_x, center_y = center_coordinates + text_w, text_h = text_wh + + if position == Position.TOP_LEFT: + return center_x, center_y - text_h, center_x + text_w, center_y + elif position == Position.TOP_RIGHT: + return center_x - text_w, center_y - text_h, center_x, center_y + elif position == Position.TOP_CENTER: + return ( + center_x - text_w // 2, + center_y - text_h, + center_x + text_w // 2, + center_y, + ) + elif position == Position.CENTER or position == Position.CENTER_OF_MASS: + return ( + center_x - text_w // 2, + center_y - text_h // 2, + center_x + text_w // 2, + center_y + text_h // 2, + ) + elif position == Position.BOTTOM_LEFT: + return center_x, center_y, center_x + text_w, center_y + text_h + elif position == Position.BOTTOM_RIGHT: + return center_x - text_w, center_y, center_x, center_y + text_h + elif position == Position.BOTTOM_CENTER: + return ( + center_x - text_w // 2, + center_y, + center_x + text_w // 2, + center_y + text_h, + ) + elif position == Position.CENTER_LEFT: + return ( + center_x - text_w, + center_y - text_h // 2, + center_x, + center_y + text_h // 2, + ) + elif position == Position.CENTER_RIGHT: + return ( + center_x, + center_y - text_h // 2, + center_x + text_w, + center_y + text_h // 2, + ) class BlurAnnotator(BaseAnnotator): From 9e906c78f5f1566e62c79a940c9d16e0c7cd6b3e Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Tue, 9 Apr 2024 22:14:36 +0530 Subject: [PATCH 03/10] annotate function added in RichLabelAnnotator --- supervision/annotators/core.py | 72 +++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index a86d9f9e..f69421cc 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -3,7 +3,7 @@ from typing import List, Optional, Tuple, Union import cv2 import numpy as np -from PIL import ImageFont +from PIL import ImageFont, ImageDraw from supervision.annotators.base import BaseAnnotator, ImageType from supervision.annotators.utils import ColorLookup, Trace, resolve_color @@ -1221,6 +1221,76 @@ class RichLabelAnnotator: center_x + text_w, center_y + text_h // 2, ) + + + def annotate( + self, + scene: ImageType, + detections: Detections, + labels: List[str] = None, + custom_color_lookup: Optional[np.ndarray] = None, + ) -> ImageType: + draw = ImageDraw.Draw(scene) + anchors_coordinates = detections.get_anchors_coordinates( + anchor=self.text_anchor + ).astype(int) + if labels is not None and len(labels) != len(detections): + raise ValueError( + f"The number of labels provided ({len(labels)}) does not match the " + f"number of detections ({len(detections)}). Each detection should have " + f"a corresponding label. This discrepancy can occur if the labels and " + f"detections are not aligned or if an incorrect number of labels has " + f"been provided. Please ensure that the labels array has the same " + f"length as the Detections object." + ) + for detection_idx, center_coordinates in enumerate(anchors_coordinates): + color = resolve_color( + color=self.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: + text = detections[CLASS_NAME_DATA_FIELD][detection_idx] + elif detections.class_id is not None: + text = str(detections.class_id[detection_idx]) + else: + text = str(detection_idx) + + left, top, right, bottom = draw.textbbox((0, 0), text, font=self.font) + text_width = right - left + text_height = bottom - top + text_w_padded = text_width + 2 * self.text_padding + text_h_padded = text_height + 2 * self.text_padding + text_background_xyxy = self.resolve_text_background_xyxy( + center_coordinates=tuple(center_coordinates), + text_wh=(text_w_padded, text_h_padded), + position=self.text_anchor, + ) + + text_x = text_background_xyxy[0] + self.text_padding - left + text_y = text_background_xyxy[1] + self.text_padding - top + + draw.rounded_rectangle( + text_background_xyxy, + radius=self.border_radius, + fill=color.as_rgb(), + outline=None, + ) + draw.text( + xy=(text_x, text_y), + text=text, + font=self.font, + fill=self.text_color.as_rgb(), + ) + + return scene class BlurAnnotator(BaseAnnotator): From 8329d155e4dc62f5da2a4b7b3df1936694765a91 Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Sat, 13 Apr 2024 17:27:27 +0530 Subject: [PATCH 04/10] Use default font when font_path isnt specified --- supervision/annotators/core.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index dfdf9990..a10335a7 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -3,7 +3,7 @@ from typing import List, Optional, Tuple, Union import cv2 import numpy as np -from PIL import ImageFont, ImageDraw +from PIL import ImageFont, ImageDraw, Image from supervision.annotators.base import BaseAnnotator, ImageType from supervision.annotators.utils import ColorLookup, Trace, resolve_color @@ -1154,7 +1154,7 @@ class RichLabelAnnotator: self, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, text_color: Color = Color.WHITE, - font_path: str = "/content/Arial Unicode Font.ttf", + font_path: str = None, font_size: int = 14, text_padding: int = 10, text_position: Position = Position.TOP_LEFT, @@ -1163,12 +1163,19 @@ class RichLabelAnnotator: ): self.color = color self.text_color = text_color - self.font = ImageFont.truetype(font_path, font_size) self.text_padding = text_padding self.text_anchor = text_position self.color_lookup = color_lookup self.border_radius = border_radius - + if font_path is not None: + try: + self.font = ImageFont.truetype(font_path, font_size) + except OSError: + print(f"Font path '{font_path}' not found. Using a system font.") + self.font = ImageFont.load_default(size=font_size) + else: + self.font = ImageFont.load_default(size=font_size) + @staticmethod def resolve_text_background_xyxy( center_coordinates: Tuple[int, int], @@ -1221,8 +1228,7 @@ class RichLabelAnnotator: center_x + text_w, center_y + text_h // 2, ) - - + def annotate( self, scene: ImageType, @@ -1230,6 +1236,8 @@ class RichLabelAnnotator: labels: List[str] = None, custom_color_lookup: Optional[np.ndarray] = None, ) -> ImageType: + if isinstance(scene, np.ndarray): + scene = Image.fromarray(cv2.cvtColor(scene, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(scene) anchors_coordinates = detections.get_anchors_coordinates( anchor=self.text_anchor @@ -1292,7 +1300,6 @@ class RichLabelAnnotator: return scene - class BlurAnnotator(BaseAnnotator): """ A class for blurring regions in an image using provided detections. From 8255bcb1cab76923300dfff429dfa300754db97e Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Sat, 13 Apr 2024 17:43:52 +0530 Subject: [PATCH 05/10] Moved resolve_text_background_xyxy() function into utils.py --- supervision/annotators/core.py | 112 +------------------------------- supervision/annotators/utils.py | 54 ++++++++++++++- 2 files changed, 56 insertions(+), 110 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index a10335a7..61eb9fd3 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -6,7 +6,7 @@ import numpy as np from PIL import ImageFont, ImageDraw, Image from supervision.annotators.base import BaseAnnotator, ImageType -from supervision.annotators.utils import ColorLookup, Trace, resolve_color +from supervision.annotators.utils import ColorLookup, Trace, resolve_color, resolve_text_background_xyxy from supervision.config import CLASS_NAME_DATA_FIELD, ORIENTED_BOX_COORDINATES from supervision.detection.core import Detections from supervision.detection.utils import clip_boxes, mask_to_polygons @@ -937,59 +937,6 @@ class LabelAnnotator: self.text_anchor: Position = text_position self.color_lookup: ColorLookup = color_lookup - @staticmethod - def resolve_text_background_xyxy( - center_coordinates: Tuple[int, int], - text_wh: Tuple[int, int], - position: Position, - ) -> Tuple[int, int, int, int]: - center_x, center_y = center_coordinates - text_w, text_h = text_wh - - if position == Position.TOP_LEFT: - return center_x, center_y - text_h, center_x + text_w, center_y - elif position == Position.TOP_RIGHT: - return center_x - text_w, center_y - text_h, center_x, center_y - elif position == Position.TOP_CENTER: - return ( - center_x - text_w // 2, - center_y - text_h, - center_x + text_w // 2, - center_y, - ) - elif position == Position.CENTER or position == Position.CENTER_OF_MASS: - return ( - center_x - text_w // 2, - center_y - text_h // 2, - center_x + text_w // 2, - center_y + text_h // 2, - ) - elif position == Position.BOTTOM_LEFT: - return center_x, center_y, center_x + text_w, center_y + text_h - elif position == Position.BOTTOM_RIGHT: - return center_x - text_w, center_y, center_x, center_y + text_h - elif position == Position.BOTTOM_CENTER: - return ( - center_x - text_w // 2, - center_y, - center_x + text_w // 2, - center_y + text_h, - ) - elif position == Position.CENTER_LEFT: - return ( - center_x - text_w, - center_y - text_h // 2, - center_x, - center_y + text_h // 2, - ) - elif position == Position.CENTER_RIGHT: - return ( - center_x, - center_y - text_h // 2, - center_x + text_w, - center_y + text_h // 2, - ) - @convert_for_annotation_method def annotate( self, @@ -1079,7 +1026,7 @@ class LabelAnnotator: )[0] text_w_padded = text_w + 2 * self.text_padding text_h_padded = text_h + 2 * self.text_padding - text_background_xyxy = self.resolve_text_background_xyxy( + text_background_xyxy = resolve_text_background_xyxy( center_coordinates=tuple(center_coordinates), text_wh=(text_w_padded, text_h_padded), position=self.text_anchor, @@ -1176,59 +1123,6 @@ class RichLabelAnnotator: else: self.font = ImageFont.load_default(size=font_size) - @staticmethod - def resolve_text_background_xyxy( - center_coordinates: Tuple[int, int], - text_wh: Tuple[int, int], - position: Position, - ) -> Tuple[int, int, int, int]: - center_x, center_y = center_coordinates - text_w, text_h = text_wh - - if position == Position.TOP_LEFT: - return center_x, center_y - text_h, center_x + text_w, center_y - elif position == Position.TOP_RIGHT: - return center_x - text_w, center_y - text_h, center_x, center_y - elif position == Position.TOP_CENTER: - return ( - center_x - text_w // 2, - center_y - text_h, - center_x + text_w // 2, - center_y, - ) - elif position == Position.CENTER or position == Position.CENTER_OF_MASS: - return ( - center_x - text_w // 2, - center_y - text_h // 2, - center_x + text_w // 2, - center_y + text_h // 2, - ) - elif position == Position.BOTTOM_LEFT: - return center_x, center_y, center_x + text_w, center_y + text_h - elif position == Position.BOTTOM_RIGHT: - return center_x - text_w, center_y, center_x, center_y + text_h - elif position == Position.BOTTOM_CENTER: - return ( - center_x - text_w // 2, - center_y, - center_x + text_w // 2, - center_y + text_h, - ) - elif position == Position.CENTER_LEFT: - return ( - center_x - text_w, - center_y - text_h // 2, - center_x, - center_y + text_h // 2, - ) - elif position == Position.CENTER_RIGHT: - return ( - center_x, - center_y - text_h // 2, - center_x + text_w, - center_y + text_h // 2, - ) - def annotate( self, scene: ImageType, @@ -1276,7 +1170,7 @@ class RichLabelAnnotator: text_height = bottom - top text_w_padded = text_width + 2 * self.text_padding text_h_padded = text_height + 2 * self.text_padding - text_background_xyxy = self.resolve_text_background_xyxy( + text_background_xyxy = resolve_text_background_xyxy( center_coordinates=tuple(center_coordinates), text_wh=(text_w_padded, text_h_padded), position=self.text_anchor, diff --git a/supervision/annotators/utils.py b/supervision/annotators/utils.py index e206c8cb..bdc17975 100644 --- a/supervision/annotators/utils.py +++ b/supervision/annotators/utils.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Optional, Union +from typing import Optional, Union, Tuple import numpy as np @@ -62,6 +62,58 @@ def resolve_color_idx( ) return detections.tracker_id[detection_idx] +def resolve_text_background_xyxy( + center_coordinates: Tuple[int, int], + text_wh: Tuple[int, int], + position: Position, +) -> Tuple[int, int, int, int]: + center_x, center_y = center_coordinates + text_w, text_h = text_wh + + if position == Position.TOP_LEFT: + return center_x, center_y - text_h, center_x + text_w, center_y + elif position == Position.TOP_RIGHT: + return center_x - text_w, center_y - text_h, center_x, center_y + elif position == Position.TOP_CENTER: + return ( + center_x - text_w // 2, + center_y - text_h, + center_x + text_w // 2, + center_y, + ) + elif position == Position.CENTER or position == Position.CENTER_OF_MASS: + return ( + center_x - text_w // 2, + center_y - text_h // 2, + center_x + text_w // 2, + center_y + text_h // 2, + ) + elif position == Position.BOTTOM_LEFT: + return center_x, center_y, center_x + text_w, center_y + text_h + elif position == Position.BOTTOM_RIGHT: + return center_x - text_w, center_y, center_x, center_y + text_h + elif position == Position.BOTTOM_CENTER: + return ( + center_x - text_w // 2, + center_y, + center_x + text_w // 2, + center_y + text_h, + ) + elif position == Position.CENTER_LEFT: + return ( + center_x - text_w, + center_y - text_h // 2, + center_x, + center_y + text_h // 2, + ) + elif position == Position.CENTER_RIGHT: + return ( + center_x, + center_y - text_h // 2, + center_x + text_w, + center_y + text_h // 2, + ) + def get_color_by_index(color: Union[Color, ColorPalette], idx: int) -> Color: if isinstance(color, ColorPalette): From 8bb8ce03167a68f0cda1a9089026306719a2d0aa Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Sat, 13 Apr 2024 18:03:08 +0530 Subject: [PATCH 06/10] changed error message and formatted code --- supervision/annotators/core.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 61eb9fd3..baf04574 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -6,7 +6,12 @@ import numpy as np from PIL import ImageFont, ImageDraw, Image from supervision.annotators.base import BaseAnnotator, ImageType -from supervision.annotators.utils import ColorLookup, Trace, resolve_color, resolve_text_background_xyxy +from supervision.annotators.utils import ( + ColorLookup, + Trace, + resolve_color, + resolve_text_background_xyxy, +) from supervision.config import CLASS_NAME_DATA_FIELD, ORIENTED_BOX_COORDINATES from supervision.detection.core import Detections from supervision.detection.utils import clip_boxes, mask_to_polygons @@ -1004,9 +1009,11 @@ class LabelAnnotator: color=self.color, detections=detections, detection_idx=detection_idx, - color_lookup=self.color_lookup - if custom_color_lookup is None - else custom_color_lookup, + color_lookup=( + self.color_lookup + if custom_color_lookup is None + else custom_color_lookup + ), ) if labels is not None: @@ -1095,6 +1102,7 @@ class LabelAnnotator: ) return scene + class RichLabelAnnotator: def __init__( @@ -1102,7 +1110,7 @@ class RichLabelAnnotator: color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, text_color: Color = Color.WHITE, font_path: str = None, - font_size: int = 14, + font_size: int = 10, text_padding: int = 10, text_position: Position = Position.TOP_LEFT, color_lookup: ColorLookup = ColorLookup.CLASS, @@ -1118,7 +1126,7 @@ class RichLabelAnnotator: try: self.font = ImageFont.truetype(font_path, font_size) except OSError: - print(f"Font path '{font_path}' not found. Using a system font.") + print(f"Font path '{font_path}' not found. Using PIL's default font.") self.font = ImageFont.load_default(size=font_size) else: self.font = ImageFont.load_default(size=font_size) From cb220479ebaf63464f758550b58e7eb321ed0f87 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:16:13 +0000 Subject: [PATCH 07/10] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/annotators/core.py | 4 ++-- supervision/annotators/utils.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index baf04574..4a8d0b66 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -3,7 +3,7 @@ from typing import List, Optional, Tuple, Union import cv2 import numpy as np -from PIL import ImageFont, ImageDraw, Image +from PIL import Image, ImageDraw, ImageFont from supervision.annotators.base import BaseAnnotator, ImageType from supervision.annotators.utils import ( @@ -1104,7 +1104,6 @@ class LabelAnnotator: class RichLabelAnnotator: - def __init__( self, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, @@ -1202,6 +1201,7 @@ class RichLabelAnnotator: return scene + class BlurAnnotator(BaseAnnotator): """ A class for blurring regions in an image using provided detections. diff --git a/supervision/annotators/utils.py b/supervision/annotators/utils.py index bdc17975..f0de9fa5 100644 --- a/supervision/annotators/utils.py +++ b/supervision/annotators/utils.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Optional, Union, Tuple +from typing import Optional, Tuple, Union import numpy as np @@ -62,6 +62,7 @@ def resolve_color_idx( ) return detections.tracker_id[detection_idx] + def resolve_text_background_xyxy( center_coordinates: Tuple[int, int], text_wh: Tuple[int, int], From df4a6f857cc138e5e9b680f223be0ef6527a40d6 Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Sat, 13 Apr 2024 18:58:18 +0530 Subject: [PATCH 08/10] added docs for RichLabelAnnotator --- supervision/annotators/core.py | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 4a8d0b66..f2b552cd 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1104,6 +1104,11 @@ class LabelAnnotator: class RichLabelAnnotator: + """ + A class for annotating labels on an image using provided detections, + with support for Unicode characters by using a custom font. + """ + def __init__( self, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, @@ -1115,6 +1120,22 @@ class RichLabelAnnotator: color_lookup: ColorLookup = ColorLookup.CLASS, border_radius: int = 0, ): + """ + 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. + font_path (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. + text_padding (int): Padding around the text within its background box. + text_position (Position): Position of the text relative to the detection. + Possible values are defined in the `Position` enum. + color_lookup (ColorLookup): Strategy for mapping 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. + """ self.color = color self.text_color = text_color self.text_padding = text_padding @@ -1137,6 +1158,45 @@ class RichLabelAnnotator: labels: List[str] = None, custom_color_lookup: Optional[np.ndarray] = None, ) -> ImageType: + """ + Annotates the given scene with labels based on the provided + detections, with support for Unicode characters. + + Args: + scene (ImageType): The image where labels will be drawn. + `ImageType` is a flexible type, accepting either `numpy.ndarray` + or `PIL.Image.Image`. + detections (Detections): Object detections to annotate. + labels (List[str]): Optional. Custom labels for each detection. + custom_color_lookup (Optional[np.ndarray]): Custom color lookup array. + Allows to override the default color mapping strategy. + + Returns: + The annotated image, matching the type of `scene` (`numpy.ndarray` + or `PIL.Image.Image`) + + Example: + ```python + import supervision as sv + + image = ... + detections = sv.Detections(...) + + labels = [ + f"{class_name} {confidence:.2f}" + for class_name, confidence + in zip(detections['class_name'], detections.confidence) + ] + + label_annotator = sv.RichLabelAnnotator(font_path="path/to/font.ttf") + annotated_frame = label_annotator.annotate( + scene=image.copy(), + detections=detections, + labels=labels + ) + ``` + + """ if isinstance(scene, np.ndarray): scene = Image.fromarray(cv2.cvtColor(scene, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(scene) From 24d1840ea1c8b6dab615b6d8c1b4913b131ddff8 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Wed, 15 May 2024 18:35:36 +0300 Subject: [PATCH 09/10] =?UTF-8?q?docs:=20=F0=9F=93=9D=20RichLabelAnnotator?= =?UTF-8?q?=20documentation=20added=20into=20docs=20and=20minor=20var=20fi?= =?UTF-8?q?xes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- docs/annotators.md | 37 ++++++++++++++++++++++++++++++++++ supervision/__init__.py | 1 + supervision/annotators/core.py | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/docs/annotators.md b/docs/annotators.md index e1f4b115..958f2a74 100644 --- a/docs/annotators.md +++ b/docs/annotators.md @@ -285,6 +285,37 @@ status: new +=== "RichLabel" + + ```python + import supervision as sv + + image = ... + detections = sv.Detections(...) + + labels = [ + f"{class_name} {confidence:.2f}" + for class_name, confidence + in zip(detections['class_name'], detections.confidence) + ] + + rich_label_annotator = sv.RichLabelAnnotator( + font_path=".../font.ttf", + text_position=sv.Position.CENTER + ) + annotated_frame = label_annotator.annotate( + scene=image.copy(), + detections=detections, + labels=labels + ) + ``` + +
+ + ![label-annotator-example](https://media.roboflow.com/supervision-annotator-examples/label-annotator-example-purple.png){ align=center width="800" } + +
+ === "Crop" ```python @@ -492,6 +523,12 @@ status: new :::supervision.annotators.core.LabelAnnotator + + +:::supervision.annotators.core.RichLabelAnnotator + diff --git a/supervision/__init__.py b/supervision/__init__.py index f8e7a832..d2b502b9 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -18,6 +18,7 @@ from supervision.annotators.core import ( HaloAnnotator, HeatMapAnnotator, LabelAnnotator, + RichLabelAnnotator, MaskAnnotator, OrientedBoxAnnotator, PercentageBarAnnotator, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index f2b552cd..75373e33 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1177,7 +1177,7 @@ class RichLabelAnnotator: Example: ```python - import supervision as sv + import supervision as sv image = ... detections = sv.Detections(...) @@ -1188,7 +1188,7 @@ class RichLabelAnnotator: in zip(detections['class_name'], detections.confidence) ] - label_annotator = sv.RichLabelAnnotator(font_path="path/to/font.ttf") + rich_label_annotator = sv.RichLabelAnnotator(font_path="path/to/font.ttf") annotated_frame = label_annotator.annotate( scene=image.copy(), detections=detections, From d983177c3a3af42d88facff59a755f924c8933d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 15:54:51 +0000 Subject: [PATCH 10/10] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto?= =?UTF-8?q?=20format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/__init__.py | 2 +- supervision/annotators/core.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/supervision/__init__.py b/supervision/__init__.py index da754928..64680012 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -18,12 +18,12 @@ from supervision.annotators.core import ( HaloAnnotator, HeatMapAnnotator, LabelAnnotator, - RichLabelAnnotator, MaskAnnotator, OrientedBoxAnnotator, PercentageBarAnnotator, PixelateAnnotator, PolygonAnnotator, + RichLabelAnnotator, RoundBoxAnnotator, TraceAnnotator, TriangleAnnotator, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 75373e33..1c0ad8af 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1125,8 +1125,8 @@ class RichLabelAnnotator: 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. - font_path (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_path (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. text_padding (int): Padding around the text within its background box. text_position (Position): Position of the text relative to the detection.