From a5b1079e39bad052e6da3c5d3fba44d7a000993a Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Fri, 15 Mar 2024 11:57:53 +0100 Subject: [PATCH 1/3] work in progress --- supervision/annotators/core.py | 91 +++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 0c834fa8..1c63b232 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1847,7 +1847,14 @@ class CropAnnotator(BaseAnnotator): A class for drawing scaled up crops of detections on the scene. """ - def __init__(self, position: Position = Position.TOP_CENTER, scale_factor: int = 2): + def __init__( + self, + position: Position = Position.TOP_CENTER, + scale_factor: int = 2, + color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, + thickness: int = 2, + color_lookup: ColorLookup = ColorLookup.CLASS + ): """ Args: position (Position): The anchor position for placing the cropped and scaled @@ -1858,13 +1865,17 @@ class CropAnnotator(BaseAnnotator): """ self.position: Position = position self.scale_factor: int = scale_factor + self.color: Union[Color, ColorPalette] = color + self.thickness: int = thickness + self.color_lookup: ColorLookup = color_lookup @scene_to_annotator_img_type def annotate( self, - scene: np.ndarray, + scene: ImageType, detections: Detections, - ) -> np.ndarray: + custom_color_lookup: Optional[np.ndarray] = None, + ) -> ImageType: """ Annotates the provided scene with scaled and cropped parts of the image based on the provided detections. Each detection is cropped from the original scene @@ -1873,8 +1884,12 @@ class CropAnnotator(BaseAnnotator): Args: - scene (np.ndarray): The image where cropped detection will be placed. + scene (ImageType): The image where cropped detection will be placed. + `ImageType` is a flexible type, accepting either `numpy.ndarray` + or `PIL.Image.Image`. detections (Detections): Object detections to annotate. + custom_color_lookup (Optional[np.ndarray]): Custom color lookup array. + Allows to override the default color mapping strategy. Returns: The annotated image. @@ -1901,37 +1916,79 @@ class CropAnnotator(BaseAnnotator): ] anchors = detections.get_anchors_coordinates(anchor=self.position).astype(int) - for resized_crop, anchor in zip(resized_crops, anchors): + for idx, (resized_crop, anchor) in enumerate(zip(resized_crops, anchors)): crop_wh = resized_crop.shape[1], resized_crop.shape[0] - crop_anchor = self.calculate_crop_coordinates( + (x1, y1), (x2, y2) = self.calculate_crop_coordinates( anchor=anchor, crop_wh=crop_wh, position=self.position ) - scene = place_image(scene=scene, image=resized_crop, anchor=crop_anchor) + scene = place_image(scene=scene, image=resized_crop, anchor=(x1, y1)) + color = resolve_color( + color=self.color, + detections=detections, + detection_idx=idx, + color_lookup=self.color_lookup + if custom_color_lookup is None + else custom_color_lookup, + ) + cv2.rectangle( + img=scene, + pt1=(x1, y1), + pt2=(x2, y2), + color=color.as_bgr(), + thickness=self.thickness, + ) return scene @staticmethod def calculate_crop_coordinates( anchor: Tuple[int, int], crop_wh: Tuple[int, int], position: Position - ) -> Tuple[int, int]: + ) -> Tuple[Tuple[int, int], Tuple[int, int]]: anchor_x, anchor_y = anchor width, height = crop_wh if position == Position.TOP_LEFT: - return anchor_x - width, anchor_y - height + return ( + (anchor_x - width, anchor_y - height), + (anchor_x, anchor_y) + ) elif position == Position.TOP_CENTER: - return anchor_x - width // 2, anchor_y - height + return ( + (anchor_x - width // 2, anchor_y - height), + (anchor_x + width // 2, anchor_y) + ) elif position == Position.TOP_RIGHT: - return anchor_x, anchor_y - height + return ( + (anchor_x, anchor_y - height), + (anchor_x + width, anchor_y) + ) elif position == Position.CENTER_LEFT: - return anchor_x - width, anchor_y - height // 2 + return ( + (anchor_x - width, anchor_y - height // 2), + (anchor_x, anchor_y + height // 2) + ) elif position == Position.CENTER or position == Position.CENTER_OF_MASS: - return anchor_x - width // 2, anchor_y - height // 2 + return ( + (anchor_x - width // 2, anchor_y - height // 2), + (anchor_x + width // 2, anchor_y + height // 2) + ) elif position == Position.CENTER_RIGHT: - return anchor_x, anchor_y - height // 2 + return ( + (anchor_x, anchor_y - height // 2), + (anchor_x + width, anchor_y + height // 2) + ) elif position == Position.BOTTOM_LEFT: - return anchor_x - width, anchor_y + return ( + (anchor_x - width, anchor_y), + (anchor_x, anchor_y + height) + ) elif position == Position.BOTTOM_CENTER: - return anchor_x - width // 2, anchor_y + return ( + (anchor_x - width // 2, anchor_y), + (anchor_x + width // 2, anchor_y + height) + ) elif position == Position.BOTTOM_RIGHT: - return anchor_x, anchor_y + return ( + (anchor_x, anchor_y), + (anchor_x + width, anchor_y + height) + ) From 9f5c63b32d88d5eed7dada3cebd37dce5623a7d7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 15 Mar 2024 11:02:24 +0000 Subject: [PATCH 2/3] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/annotators/core.py | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 1c63b232..a96d91e1 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1853,7 +1853,7 @@ class CropAnnotator(BaseAnnotator): scale_factor: int = 2, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, thickness: int = 2, - color_lookup: ColorLookup = ColorLookup.CLASS + color_lookup: ColorLookup = ColorLookup.CLASS, ): """ Args: @@ -1948,47 +1948,35 @@ class CropAnnotator(BaseAnnotator): width, height = crop_wh if position == Position.TOP_LEFT: - return ( - (anchor_x - width, anchor_y - height), - (anchor_x, anchor_y) - ) + return ((anchor_x - width, anchor_y - height), (anchor_x, anchor_y)) elif position == Position.TOP_CENTER: return ( (anchor_x - width // 2, anchor_y - height), - (anchor_x + width // 2, anchor_y) + (anchor_x + width // 2, anchor_y), ) elif position == Position.TOP_RIGHT: - return ( - (anchor_x, anchor_y - height), - (anchor_x + width, anchor_y) - ) + return ((anchor_x, anchor_y - height), (anchor_x + width, anchor_y)) elif position == Position.CENTER_LEFT: return ( (anchor_x - width, anchor_y - height // 2), - (anchor_x, anchor_y + height // 2) + (anchor_x, anchor_y + height // 2), ) elif position == Position.CENTER or position == Position.CENTER_OF_MASS: return ( (anchor_x - width // 2, anchor_y - height // 2), - (anchor_x + width // 2, anchor_y + height // 2) + (anchor_x + width // 2, anchor_y + height // 2), ) elif position == Position.CENTER_RIGHT: return ( (anchor_x, anchor_y - height // 2), - (anchor_x + width, anchor_y + height // 2) + (anchor_x + width, anchor_y + height // 2), ) elif position == Position.BOTTOM_LEFT: - return ( - (anchor_x - width, anchor_y), - (anchor_x, anchor_y + height) - ) + return ((anchor_x - width, anchor_y), (anchor_x, anchor_y + height)) elif position == Position.BOTTOM_CENTER: return ( (anchor_x - width // 2, anchor_y), - (anchor_x + width // 2, anchor_y + height) + (anchor_x + width // 2, anchor_y + height), ) elif position == Position.BOTTOM_RIGHT: - return ( - (anchor_x, anchor_y), - (anchor_x + width, anchor_y + height) - ) + return ((anchor_x, anchor_y), (anchor_x + width, anchor_y + height)) From 8a2abe72aead34443e16a8591102bf78b4987466 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 20 Mar 2024 19:37:03 +0100 Subject: [PATCH 3/3] utils refactor --- docs/draw/utils.md | 8 ++--- .../count_people_in_zone/requirements.txt | 2 +- examples/heatmap_and_track/requirements.txt | 2 +- examples/speed_estimation/requirements.txt | 2 +- examples/tracking/requirements.txt | 2 +- examples/traffic_analysis/requirements.txt | 2 +- supervision/__init__.py | 4 +-- supervision/annotators/core.py | 31 +++++++++++-------- supervision/draw/utils.py | 24 +++++--------- 9 files changed, 37 insertions(+), 40 deletions(-) diff --git a/docs/draw/utils.md b/docs/draw/utils.md index ad3e1d6b..7975952c 100644 --- a/docs/draw/utils.md +++ b/docs/draw/utils.md @@ -41,13 +41,13 @@ comments: true :::supervision.draw.utils.draw_image
-

calculate_dynamic_font_scale

+

calculate_optimal_font_scale

-:::supervision.draw.utils.calculate_dynamic_text_scale +:::supervision.draw.utils.calculate_optimal_text_scale
-

calculate_dynamic_line_thickness

+

calculate_optimal_line_thickness

-:::supervision.draw.utils.calculate_dynamic_line_thickness +:::supervision.draw.utils.calculate_optimal_line_thickness diff --git a/examples/count_people_in_zone/requirements.txt b/examples/count_people_in_zone/requirements.txt index a8f583e8..d9e27264 100644 --- a/examples/count_people_in_zone/requirements.txt +++ b/examples/count_people_in_zone/requirements.txt @@ -1,5 +1,5 @@ gdown inference -supervision +supervision==0.19.0 tqdm ultralytics diff --git a/examples/heatmap_and_track/requirements.txt b/examples/heatmap_and_track/requirements.txt index ffc73e1f..27e5e57a 100644 --- a/examples/heatmap_and_track/requirements.txt +++ b/examples/heatmap_and_track/requirements.txt @@ -1,2 +1,2 @@ -supervision[assets] +supervision[assets]==0.19.0 ultralytics diff --git a/examples/speed_estimation/requirements.txt b/examples/speed_estimation/requirements.txt index 59ca45cb..36de970d 100644 --- a/examples/speed_estimation/requirements.txt +++ b/examples/speed_estimation/requirements.txt @@ -1,4 +1,4 @@ -supervision==0.18.0rc1 +supervision==0.19.0 tqdm==4.66.1 requests ultralytics==8.0.237 diff --git a/examples/tracking/requirements.txt b/examples/tracking/requirements.txt index 80c28300..8d5a9233 100644 --- a/examples/tracking/requirements.txt +++ b/examples/tracking/requirements.txt @@ -1,4 +1,4 @@ inference -supervision +supervision==0.19.0 tqdm ultralytics diff --git a/examples/traffic_analysis/requirements.txt b/examples/traffic_analysis/requirements.txt index c56009a1..6e72dd55 100644 --- a/examples/traffic_analysis/requirements.txt +++ b/examples/traffic_analysis/requirements.txt @@ -1,5 +1,5 @@ gdown inference -supervision>=0.19.0rc5 +supervision>=0.19.0 tqdm ultralytics diff --git a/supervision/__init__.py b/supervision/__init__.py index 4a102af5..35f525b0 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -58,8 +58,8 @@ from supervision.detection.utils import ( ) from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import ( - calculate_dynamic_line_thickness, - calculate_dynamic_text_scale, + calculate_optimal_line_thickness, + calculate_optimal_text_scale, draw_filled_rectangle, draw_image, draw_line, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index a96d91e1..2be2562e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1851,9 +1851,9 @@ class CropAnnotator(BaseAnnotator): self, position: Position = Position.TOP_CENTER, scale_factor: int = 2, - color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, - thickness: int = 2, - color_lookup: ColorLookup = ColorLookup.CLASS, + border_color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, + border_thickness: int = 2, + border_color_lookup: ColorLookup = ColorLookup.CLASS, ): """ Args: @@ -1862,12 +1862,17 @@ class CropAnnotator(BaseAnnotator): scale_factor (int): The factor by which to scale the cropped image part. A factor of 2, for example, would double the size of the cropped area, allowing for a closer view of the detection. + border_color (Union[Color, ColorPalette]): The color or color palette to + use for annotating border around the cropped area. + border_thickness (int): The thickness of the border around the cropped area. + border_color_lookup (ColorLookup): Strategy for mapping colors to + annotations. Options are `INDEX`, `CLASS`, `TRACK`. """ self.position: Position = position self.scale_factor: int = scale_factor - self.color: Union[Color, ColorPalette] = color - self.thickness: int = thickness - self.color_lookup: ColorLookup = color_lookup + self.border_color: Union[Color, ColorPalette] = border_color + self.border_thickness: int = border_thickness + self.border_color_lookup: ColorLookup = border_color_lookup @scene_to_annotator_img_type def annotate( @@ -1923,10 +1928,10 @@ class CropAnnotator(BaseAnnotator): ) scene = place_image(scene=scene, image=resized_crop, anchor=(x1, y1)) color = resolve_color( - color=self.color, + color=self.border_color, detections=detections, detection_idx=idx, - color_lookup=self.color_lookup + color_lookup=self.border_color_lookup if custom_color_lookup is None else custom_color_lookup, ) @@ -1935,7 +1940,7 @@ class CropAnnotator(BaseAnnotator): pt1=(x1, y1), pt2=(x2, y2), color=color.as_bgr(), - thickness=self.thickness, + thickness=self.border_thickness, ) return scene @@ -1948,14 +1953,14 @@ class CropAnnotator(BaseAnnotator): width, height = crop_wh if position == Position.TOP_LEFT: - return ((anchor_x - width, anchor_y - height), (anchor_x, anchor_y)) + return (anchor_x - width, anchor_y - height), (anchor_x, anchor_y) elif position == Position.TOP_CENTER: return ( (anchor_x - width // 2, anchor_y - height), (anchor_x + width // 2, anchor_y), ) elif position == Position.TOP_RIGHT: - return ((anchor_x, anchor_y - height), (anchor_x + width, anchor_y)) + return (anchor_x, anchor_y - height), (anchor_x + width, anchor_y) elif position == Position.CENTER_LEFT: return ( (anchor_x - width, anchor_y - height // 2), @@ -1972,11 +1977,11 @@ class CropAnnotator(BaseAnnotator): (anchor_x + width, anchor_y + height // 2), ) elif position == Position.BOTTOM_LEFT: - return ((anchor_x - width, anchor_y), (anchor_x, anchor_y + height)) + return (anchor_x - width, anchor_y), (anchor_x, anchor_y + height) elif position == Position.BOTTOM_CENTER: return ( (anchor_x - width // 2, anchor_y), (anchor_x + width // 2, anchor_y + height), ) elif position == Position.BOTTOM_RIGHT: - return ((anchor_x, anchor_y), (anchor_x + width, anchor_y + height)) + return (anchor_x, anchor_y), (anchor_x + width, anchor_y + height) diff --git a/supervision/draw/utils.py b/supervision/draw/utils.py index 7f82b008..638e6b75 100644 --- a/supervision/draw/utils.py +++ b/supervision/draw/utils.py @@ -238,13 +238,13 @@ def draw_image( return scene -def calculate_dynamic_text_scale(resolution_wh: Tuple[int, int]) -> float: +def calculate_optimal_text_scale(resolution_wh: Tuple[int, int]) -> float: """ - Calculate a dynamic font scale based on the resolution of an image. + Calculate font scale based on the resolution of an image. Parameters: resolution_wh (Tuple[int, int]): A tuple representing the width and height - of the image. + of the image. Returns: float: The calculated font scale factor. @@ -252,25 +252,17 @@ def calculate_dynamic_text_scale(resolution_wh: Tuple[int, int]) -> float: return min(resolution_wh) * 1e-3 -def calculate_dynamic_line_thickness(resolution_wh: Tuple[int, int]) -> int: +def calculate_optimal_line_thickness(resolution_wh: Tuple[int, int]) -> int: """ - Calculate a dynamic line thickness based on the resolution of an image. + Calculate line thickness based on the resolution of an image. Parameters: resolution_wh (Tuple[int, int]): A tuple representing the width and height - of the image. + of the image. Returns: int: The calculated line thickness in pixels. """ - min_dimension = min(resolution_wh) - if min_dimension < 480: + if min(resolution_wh) < 1080: return 2 - if min_dimension < 720: - return 2 - if min_dimension < 1080: - return 2 - if min_dimension < 2160: - return 4 - else: - return 4 + return 4