From c016ea3071a7632d55d815d82e43e125a925262b Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 29 Jan 2024 22:38:40 +0200 Subject: [PATCH 01/17] as --- supervision/annotators/core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 870b26f2..f35157fe 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1749,3 +1749,5 @@ class PercentageBarAnnotator(BaseAnnotator): if not all(0 <= value <= 1 for value in custom_values): raise ValueError("All values in custom_values must be between 0 and 1.") + + From 714937e4bdce49c92065090c36f30be9fc61416d Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 29 Jan 2024 22:40:33 +0200 Subject: [PATCH 02/17] initial class build --- supervision/annotators/core.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index f35157fe..de4d04b9 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1751,3 +1751,18 @@ class PercentageBarAnnotator(BaseAnnotator): raise ValueError("All values in custom_values must be between 0 and 1.") +class CropAnnotator(BaseAnnotator): + + def __init__( + self, + position: Position = Position.TOP_CENTER, + zoom_factor: int = 2 + ): + pass + + def annotate( + self, + scene: np.ndarray, + detections: Detections, + ) -> np.ndarray: + pass \ No newline at end of file From bc24d0ec4419677f2c2ef9637ab394823ac6af8e Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 29 Jan 2024 22:49:51 +0200 Subject: [PATCH 03/17] initial doc strings --- supervision/annotators/core.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index de4d04b9..355ebe08 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1752,12 +1752,20 @@ class PercentageBarAnnotator(BaseAnnotator): class CropAnnotator(BaseAnnotator): + ''' + A class for viewing a cropped part of detections with a selected zoom scale. + ''' def __init__( self, position: Position = Position.TOP_CENTER, zoom_factor: int = 2 ): + ''' + Args: + position (Position): the anchor position to place cropped detections + zoom_factor (int): the percentage factor to zoom cropped image part + ''' pass def annotate( @@ -1765,4 +1773,31 @@ class CropAnnotator(BaseAnnotator): scene: np.ndarray, detections: Detections, ) -> np.ndarray: + ''' + Annotates the given scene with cropped parts from the provided detections. + + Args: + scene (np.ndarray): The image where cropped detection will be placed. + detections (Detections): Object detections to annotate. + + Returns: + The annotated image. + + Example: + ```python + import supervision as sv + + image = ... + detections = sv.Detections(...) + + crop_annotator = sv.CropAnnotator() + annotated_frame = crop_annotator.annotate( + scene=image.copy(), + detections=detections + ) + ``` + + ![crop-annotator-example](https://media.roboflow.com/ #TODO update link + supervision-annotator-examples/______.png) + ''' pass \ No newline at end of file From c132d5fcf22fa060491b32a745e401d82071c90b Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 29 Jan 2024 22:54:51 +0200 Subject: [PATCH 04/17] added CropAnnotator to __init__() --- supervision/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/supervision/__init__.py b/supervision/__init__.py index d44f6723..2b05e977 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -12,6 +12,7 @@ from supervision.annotators.core import ( BoxCornerAnnotator, CircleAnnotator, ColorAnnotator, + CropAnnotator, DotAnnotator, EllipseAnnotator, HaloAnnotator, From 96b52f28fbfe0efae056258ff71a5ae1dd1d251d Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 29 Jan 2024 23:36:20 +0200 Subject: [PATCH 05/17] added attributes and base for annotate() method --- supervision/annotators/core.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 355ebe08..ebfca647 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1766,7 +1766,8 @@ class CropAnnotator(BaseAnnotator): position (Position): the anchor position to place cropped detections zoom_factor (int): the percentage factor to zoom cropped image part ''' - pass + self.position: Position = position + self.zoom_factor: int = zoom_factor def annotate( self, @@ -1800,4 +1801,10 @@ class CropAnnotator(BaseAnnotator): ![crop-annotator-example](https://media.roboflow.com/ #TODO update link supervision-annotator-examples/______.png) ''' - pass \ No newline at end of file + anchors = detections.get_anchors_coordinates(anchor=self.position) + for detection_idx in range(len(detections)): + x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) + anchor = anchors[detection_idx].astype(int) + + + return scene \ No newline at end of file From fbbf147312090427a8e98c939fe77fce49cb873e Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Tue, 30 Jan 2024 02:25:21 +0200 Subject: [PATCH 06/17] added zoom factor, crop of image part and placement on frame --- supervision/annotators/core.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index ebfca647..feac591e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1801,10 +1801,28 @@ class CropAnnotator(BaseAnnotator): ![crop-annotator-example](https://media.roboflow.com/ #TODO update link supervision-annotator-examples/______.png) ''' + base_scene = scene.copy() anchors = detections.get_anchors_coordinates(anchor=self.position) for detection_idx in range(len(detections)): x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) anchor = anchors[detection_idx].astype(int) + crop_part = base_scene[y1:y2, x1:x2] + + crop_part = cv2.resize( + crop_part, + (self.zoom_factor * crop_part.shape[1], + self.zoom_factor * crop_part.shape[0]) + ) + x = anchor[0] + y = anchor[1] + + y_len = crop_part.shape[0] + x_len = crop_part.shape[1] + + try: + scene[y:y + y_len, x:x + x_len] = crop_part + except ValueError: + pass return scene \ No newline at end of file From cf14758891f9c281da236fac4d6b596fe9a0c621 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 5 Feb 2024 23:46:30 +0200 Subject: [PATCH 07/17] as --- supervision/annotators/core.py | 89 ++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index feac591e..683eb1e1 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1806,23 +1806,82 @@ class CropAnnotator(BaseAnnotator): for detection_idx in range(len(detections)): x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) anchor = anchors[detection_idx].astype(int) - crop_part = base_scene[y1:y2, x1:x2] + cropped_scene = base_scene[y1:y2, x1:x2] - crop_part = cv2.resize( - crop_part, - (self.zoom_factor * crop_part.shape[1], - self.zoom_factor * crop_part.shape[0]) + resized_scene = self.apply_zoom( + cropped_scene=cropped_scene, + zoom_factor=self.zoom_factor + ) + + coordinates = self.clip_cropped_scene( + scene=base_scene, + cropped_scene=resized_scene, + paste_point=anchor ) - - x = anchor[0] - y = anchor[1] - - y_len = crop_part.shape[0] - x_len = crop_part.shape[1] - try: - scene[y:y + y_len, x:x + x_len] = crop_part - except ValueError: - pass + scene = self.paste_cropped_scene( + scene=base_scene, + cropped_scene=resized_scene, + clipped_point=coordinates + ) + + return scene + + @staticmethod + def apply_zoom( + cropped_scene: np.ndarray, + zoom_factor: int + ) -> np.ndarray: + ''' + Applies resize (zoom) to the cropped part of detections + + Args: + cropped_scene (np.ndarray): the cropped part of detections + + Returns: + resized_scene (np.ndarray): the resized cropped scene + ''' + resized_scene = cv2.resize( + cropped_scene, + (zoom_factor * cropped_scene.shape[1], + zoom_factor * cropped_scene.shape[0]) + ) + + return resized_scene + + @staticmethod + def clip_cropped_scene( + scene: np.ndarray, + cropped_scene: np.ndarray, + paste_point: np.ndarray + ) -> Tuple[int,int]: + + h, w = scene.shape[:2] + h_cropped, w_cropped = cropped_scene.shape[:2] + + # Clip the coordinates to ensure they are within the bounds + x_clip = max(0, min(paste_point[0], w - w_cropped)) + y_clip = max(0, min(paste_point[1], h - h_cropped)) + + # Calculate the valid region to paste the cropped part + x_end = min(x_clip + w_cropped, w) + y_end = min(y_clip + h_cropped, h) + + return x_clip, y_clip, x_end, y_end + + @staticmethod + def paste_cropped_scene( + scene: np.ndarray, + cropped_scene: np.ndarray, + clipped_point: np.ndarray + ) -> np.ndarray: + + x_clip, y_clip, x_end, y_end = clipped_point + # Adjust the cropped part based on the valid region + cropped_part_clipped = cropped_scene[:y_end - y_clip, :x_end - x_clip] + + # Paste the cropped part onto the image + scene[y_clip:y_clip + cropped_part_clipped.shape[0], + x_clip:x_clip + cropped_part_clipped.shape[1]] = cropped_part_clipped return scene \ No newline at end of file From 2863267f300064fbcd4f698ca357cd1ae9a673c1 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 12 Feb 2024 18:33:47 +0200 Subject: [PATCH 08/17] asdf --- supervision/annotators/core.py | 113 ++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 16 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 683eb1e1..d8edf510 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1813,16 +1813,16 @@ class CropAnnotator(BaseAnnotator): zoom_factor=self.zoom_factor ) - coordinates = self.clip_cropped_scene( + coordinates_and_images = self.calculate_place_coordinates( scene=base_scene, cropped_scene=resized_scene, - paste_point=anchor + position=self.position, + anchor_xy=anchor ) - scene = self.paste_cropped_scene( + scene = self.place_cropped_scene( scene=base_scene, - cropped_scene=resized_scene, - clipped_point=coordinates + coordinates_and_image=coordinates_and_images ) return scene @@ -1850,24 +1850,105 @@ class CropAnnotator(BaseAnnotator): return resized_scene @staticmethod - def clip_cropped_scene( + def calculate_place_coordinates( scene: np.ndarray, cropped_scene: np.ndarray, - paste_point: np.ndarray - ) -> Tuple[int,int]: + position: Position, + anchor_xy: np.ndarray + ) -> Tuple[Tuple[int,int],np.ndarray]: h, w = scene.shape[:2] - h_cropped, w_cropped = cropped_scene.shape[:2] + crop_h, crop_w = cropped_scene.shape[:2] + cx, cy = anchor_xy - # Clip the coordinates to ensure they are within the bounds - x_clip = max(0, min(paste_point[0], w - w_cropped)) - y_clip = max(0, min(paste_point[1], h - h_cropped)) + if position == Position.TOP_LEFT: + x = max(0, (cx - crop_w)) + y = max(0, (cy - crop_h)) + + if x == 0 and (cx-crop_w < 0): + cropped_scene = cropped_scene[:,abs(cx-crop_w):] + if y == 0 and (cy-crop_h < 0): + cropped_scene = cropped_scene[abs(cy-crop_h):,:] + return x, y, cropped_scene + + elif position == Position.TOP_CENTER: + return x, y, cropped_scene + elif position == Position.TOP_RIGHT: + x = cx - # Calculate the valid region to paste the cropped part - x_end = min(x_clip + w_cropped, w) - y_end = min(y_clip + h_cropped, h) + x_max = min(w, (cx + crop_w)) + y = max(0, (cy - crop_h)) - return x_clip, y_clip, x_end, y_end + if x_max == w and (cx + crop_w > w): + cropped_scene = cropped_scene[:,:(w - cx)] + if y == 0 and (cy-crop_h < 0): + cropped_scene = cropped_scene[abs(cy-crop_h):,:] + return x, y, cropped_scene + + elif position == Position.CENTER_LEFT: + return x, y, cropped_scene + elif position == (Position.CENTER or Position.CENTER_OF_MASS): + return x, y, cropped_scene + elif position == Position.CENTER_RIGHT: + return x, y, cropped_scene + elif position == Position.BOTTOM_LEFT: + x = max(0, (cx - crop_w)) + y = cy + y_max = min(h, (cy + crop_h)) + + if x == 0 and (cx - crop_w < 0): + cropped_scene = cropped_scene[:,abs(cx-crop_w):] + if y_max == h and (cy + crop_h > h): + cropped_scene = cropped_scene[:h-cy,:] + + return x, y, cropped_scene + + elif position == Position.BOTTOM_CENTER: + return x, y, cropped_scene + elif position == Position.BOTTOM_RIGHT: + x = cx + y = cy + x_max = min(w, (cx + crop_w)) + y_max = min(h, (cy + crop_h)) + + if x_max == w and (cx + crop_w > w): + cropped_scene = cropped_scene[:, :(w - cx)] + if y_max == h and (cy + crop_h > h): + cropped_scene = cropped_scene[:h-cy,:] + + return x, y, cropped_scene + + @staticmethod + def place_cropped_scene( + scene: np.ndarray, + coordinates_and_image:Tuple[Tuple[int,int], np.ndarray] + ) -> np.ndarray: + x,y = coordinates_and_image[0:2] + cropped_scene = coordinates_and_image[2] + scene[y:y+cropped_scene.shape[0], x:x+cropped_scene.shape[1]] = cropped_scene + + return scene + + + # @staticmethod + # def clip_cropped_scene( + # scene: np.ndarray, + # cropped_scene: np.ndarray, + # paste_point: np.ndarray + # ) -> Tuple[int,int]: + + # h, w = scene.shape[:2] + # h_cropped, w_cropped = cropped_scene.shape[:2] + + # # Clip the coordinates to ensure they are within the bounds + # x_clip = max(0, min(paste_point[0], w - w_cropped)) + # y_clip = max(0, min(paste_point[1], h - h_cropped)) + + # # Calculate the valid region to paste the cropped part + # x_end = min(x_clip + w_cropped, w) + # y_end = min(y_clip + h_cropped, h) + + # return x_clip, y_clip, x_end, y_end @staticmethod def paste_cropped_scene( From d228224f065a4821800379223b9d933da6c658d4 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 12 Feb 2024 21:47:29 +0200 Subject: [PATCH 09/17] completed if else statements for positioning --- supervision/annotators/core.py | 123 +++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 38 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index d8edf510..65d8d5e2 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1869,10 +1869,27 @@ class CropAnnotator(BaseAnnotator): cropped_scene = cropped_scene[:,abs(cx-crop_w):] if y == 0 and (cy-crop_h < 0): cropped_scene = cropped_scene[abs(cy-crop_h):,:] + return x, y, cropped_scene elif position == Position.TOP_CENTER: + + if (cx - crop_w//2) < 0: + x = 0 + cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + else: + x = cx - crop_w//2 + + if (cx + crop_w//2) >= w: + cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] + + y = max(0, (cy - crop_h)) + + if y == 0 and (cy-crop_h < 0): + cropped_scene = cropped_scene[abs(cy-crop_h):,:] + return x, y, cropped_scene + elif position == Position.TOP_RIGHT: x = cx @@ -1883,14 +1900,65 @@ class CropAnnotator(BaseAnnotator): cropped_scene = cropped_scene[:,:(w - cx)] if y == 0 and (cy-crop_h < 0): cropped_scene = cropped_scene[abs(cy-crop_h):,:] + return x, y, cropped_scene elif position == Position.CENTER_LEFT: + x = max(0, (cx - crop_w)) + + if x == 0 and (cx-crop_w < 0): + cropped_scene = cropped_scene[:,abs(cx-crop_w):] + if (cy - crop_h//2) < 0: + y = 0 + cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + else: + y = cy - crop_h//2 + + if (cy + crop_h//2) >= h: + cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] + return x, y, cropped_scene + elif position == (Position.CENTER or Position.CENTER_OF_MASS): + + if (cx - crop_w//2) < 0: + x = 0 + cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + else: + x = cx - crop_w//2 + + if (cx + crop_w//2) >= w: + cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] + + if (cy - crop_h//2) < 0: + y = 0 + cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + else: + y = cy - crop_h//2 + + if (cy + crop_h//2) >= h: + cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] + return x, y, cropped_scene + elif position == Position.CENTER_RIGHT: + x = cx + x_max = min(w, (cx + crop_w)) + + if x_max == w and (cx + crop_w > w): + cropped_scene = cropped_scene[:,:(w - cx)] + + if (cy - crop_h//2) < 0: + y = 0 + cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + else: + y = cy - crop_h//2 + + if (cy + crop_h//2) >= h: + cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] + return x, y, cropped_scene + elif position == Position.BOTTOM_LEFT: x = max(0, (cx - crop_w)) y = cy @@ -1904,7 +1972,24 @@ class CropAnnotator(BaseAnnotator): return x, y, cropped_scene elif position == Position.BOTTOM_CENTER: + + if (cx - crop_w//2) < 0: + x = 0 + cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + else: + x = cx - crop_w//2 + + if (cx + crop_w//2) >= w: + cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] + + y = cy + y_max = min(h, (cy + crop_h)) + + if y_max == h and (cy + crop_h > h): + cropped_scene = cropped_scene[:h-cy,:] + return x, y, cropped_scene + elif position == Position.BOTTOM_RIGHT: x = cx y = cy @@ -1927,42 +2012,4 @@ class CropAnnotator(BaseAnnotator): cropped_scene = coordinates_and_image[2] scene[y:y+cropped_scene.shape[0], x:x+cropped_scene.shape[1]] = cropped_scene - return scene - - - # @staticmethod - # def clip_cropped_scene( - # scene: np.ndarray, - # cropped_scene: np.ndarray, - # paste_point: np.ndarray - # ) -> Tuple[int,int]: - - # h, w = scene.shape[:2] - # h_cropped, w_cropped = cropped_scene.shape[:2] - - # # Clip the coordinates to ensure they are within the bounds - # x_clip = max(0, min(paste_point[0], w - w_cropped)) - # y_clip = max(0, min(paste_point[1], h - h_cropped)) - - # # Calculate the valid region to paste the cropped part - # x_end = min(x_clip + w_cropped, w) - # y_end = min(y_clip + h_cropped, h) - - # return x_clip, y_clip, x_end, y_end - - @staticmethod - def paste_cropped_scene( - scene: np.ndarray, - cropped_scene: np.ndarray, - clipped_point: np.ndarray - ) -> np.ndarray: - - x_clip, y_clip, x_end, y_end = clipped_point - # Adjust the cropped part based on the valid region - cropped_part_clipped = cropped_scene[:y_end - y_clip, :x_end - x_clip] - - # Paste the cropped part onto the image - scene[y_clip:y_clip + cropped_part_clipped.shape[0], - x_clip:x_clip + cropped_part_clipped.shape[1]] = cropped_part_clipped - return scene \ No newline at end of file From 379d755a528d9f7a78dc0dc8715741cd2fb5758d Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 12 Feb 2024 22:03:57 +0200 Subject: [PATCH 10/17] description of x,y pixel and trim method --- supervision/annotators/core.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 65d8d5e2..b8130cef 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1813,7 +1813,7 @@ class CropAnnotator(BaseAnnotator): zoom_factor=self.zoom_factor ) - coordinates_and_images = self.calculate_place_coordinates( + coordinates_and_images = self.calculate_place_coordinates_and_crop( scene=base_scene, cropped_scene=resized_scene, position=self.position, @@ -1850,12 +1850,26 @@ class CropAnnotator(BaseAnnotator): return resized_scene @staticmethod - def calculate_place_coordinates( + def calculate_place_coordinates_and_crop( scene: np.ndarray, cropped_scene: np.ndarray, position: Position, anchor_xy: np.ndarray ) -> Tuple[Tuple[int,int],np.ndarray]: + ''' + This method is responsible to calculate the x,y coordinates that the cropped + part of the detections will be placed and crop any exceeding part. + The returned x,y in all position cases is the final top left pixel. + + -Args: + scene (np.ndarray): The image where cropped detections will be placed. + cropped_scene (np.ndarray): The cropped part of the detections. + + -Returns: + x (int): position in horizontal axis to use for placement of cropped part. + y (int): position in vertical axis to use for placement of cropped part. + cropped_scene (np.ndarray): The cropped part after any applied trim. + ''' h, w = scene.shape[:2] crop_h, crop_w = cropped_scene.shape[:2] From 9ca3c9319d56a9724d89e66e00e4a7f8d0cb9883 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Mon, 12 Feb 2024 22:08:58 +0200 Subject: [PATCH 11/17] pre-commit spacing correction --- supervision/annotators/core.py | 191 ++++++++++++++++----------------- 1 file changed, 91 insertions(+), 100 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index b8130cef..b5084c4b 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1752,20 +1752,16 @@ class PercentageBarAnnotator(BaseAnnotator): class CropAnnotator(BaseAnnotator): - ''' + """ A class for viewing a cropped part of detections with a selected zoom scale. - ''' + """ - def __init__( - self, - position: Position = Position.TOP_CENTER, - zoom_factor: int = 2 - ): - ''' + def __init__(self, position: Position = Position.TOP_CENTER, zoom_factor: int = 2): + """ Args: position (Position): the anchor position to place cropped detections zoom_factor (int): the percentage factor to zoom cropped image part - ''' + """ self.position: Position = position self.zoom_factor: int = zoom_factor @@ -1774,7 +1770,7 @@ class CropAnnotator(BaseAnnotator): scene: np.ndarray, detections: Detections, ) -> np.ndarray: - ''' + """ Annotates the given scene with cropped parts from the provided detections. Args: @@ -1800,7 +1796,7 @@ class CropAnnotator(BaseAnnotator): ![crop-annotator-example](https://media.roboflow.com/ #TODO update link supervision-annotator-examples/______.png) - ''' + """ base_scene = scene.copy() anchors = detections.get_anchors_coordinates(anchor=self.position) for detection_idx in range(len(detections)): @@ -1809,30 +1805,25 @@ class CropAnnotator(BaseAnnotator): cropped_scene = base_scene[y1:y2, x1:x2] resized_scene = self.apply_zoom( - cropped_scene=cropped_scene, - zoom_factor=self.zoom_factor + cropped_scene=cropped_scene, zoom_factor=self.zoom_factor ) - + coordinates_and_images = self.calculate_place_coordinates_and_crop( scene=base_scene, cropped_scene=resized_scene, position=self.position, - anchor_xy=anchor + anchor_xy=anchor, ) scene = self.place_cropped_scene( - scene=base_scene, - coordinates_and_image=coordinates_and_images + scene=base_scene, coordinates_and_image=coordinates_and_images ) return scene - @staticmethod - def apply_zoom( - cropped_scene: np.ndarray, - zoom_factor: int - ) -> np.ndarray: - ''' + @staticmethod + def apply_zoom(cropped_scene: np.ndarray, zoom_factor: int) -> np.ndarray: + """ Applies resize (zoom) to the cropped part of detections Args: @@ -1840,23 +1831,25 @@ class CropAnnotator(BaseAnnotator): Returns: resized_scene (np.ndarray): the resized cropped scene - ''' + """ resized_scene = cv2.resize( cropped_scene, - (zoom_factor * cropped_scene.shape[1], - zoom_factor * cropped_scene.shape[0]) + ( + zoom_factor * cropped_scene.shape[1], + zoom_factor * cropped_scene.shape[0], + ), ) - + return resized_scene - + @staticmethod def calculate_place_coordinates_and_crop( scene: np.ndarray, cropped_scene: np.ndarray, position: Position, - anchor_xy: np.ndarray - ) -> Tuple[Tuple[int,int],np.ndarray]: - ''' + anchor_xy: np.ndarray, + ) -> Tuple[Tuple[int, int], np.ndarray]: + """ This method is responsible to calculate the x,y coordinates that the cropped part of the detections will be placed and crop any exceeding part. The returned x,y in all position cases is the final top left pixel. @@ -1869,8 +1862,8 @@ class CropAnnotator(BaseAnnotator): x (int): position in horizontal axis to use for placement of cropped part. y (int): position in vertical axis to use for placement of cropped part. cropped_scene (np.ndarray): The cropped part after any applied trim. - ''' - + """ + h, w = scene.shape[:2] crop_h, crop_w = cropped_scene.shape[:2] cx, cy = anchor_xy @@ -1878,32 +1871,31 @@ class CropAnnotator(BaseAnnotator): if position == Position.TOP_LEFT: x = max(0, (cx - crop_w)) y = max(0, (cy - crop_h)) - - if x == 0 and (cx-crop_w < 0): - cropped_scene = cropped_scene[:,abs(cx-crop_w):] - if y == 0 and (cy-crop_h < 0): - cropped_scene = cropped_scene[abs(cy-crop_h):,:] + + if x == 0 and (cx - crop_w < 0): + cropped_scene = cropped_scene[:, abs(cx - crop_w) :] + if y == 0 and (cy - crop_h < 0): + cropped_scene = cropped_scene[abs(cy - crop_h) :, :] return x, y, cropped_scene - + elif position == Position.TOP_CENTER: - - if (cx - crop_w//2) < 0: + if (cx - crop_w // 2) < 0: x = 0 - cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] else: - x = cx - crop_w//2 + x = cx - crop_w // 2 - if (cx + crop_w//2) >= w: - cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] + if (cx + crop_w // 2) >= w: + cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] y = max(0, (cy - crop_h)) - if y == 0 and (cy-crop_h < 0): - cropped_scene = cropped_scene[abs(cy-crop_h):,:] + if y == 0 and (cy - crop_h < 0): + cropped_scene = cropped_scene[abs(cy - crop_h) :, :] return x, y, cropped_scene - + elif position == Position.TOP_RIGHT: x = cx @@ -1911,96 +1903,94 @@ class CropAnnotator(BaseAnnotator): y = max(0, (cy - crop_h)) if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:,:(w - cx)] - if y == 0 and (cy-crop_h < 0): - cropped_scene = cropped_scene[abs(cy-crop_h):,:] + cropped_scene = cropped_scene[:, : (w - cx)] + if y == 0 and (cy - crop_h < 0): + cropped_scene = cropped_scene[abs(cy - crop_h) :, :] return x, y, cropped_scene - + elif position == Position.CENTER_LEFT: x = max(0, (cx - crop_w)) - if x == 0 and (cx-crop_w < 0): - cropped_scene = cropped_scene[:,abs(cx-crop_w):] - if (cy - crop_h//2) < 0: + if x == 0 and (cx - crop_w < 0): + cropped_scene = cropped_scene[:, abs(cx - crop_w) :] + if (cy - crop_h // 2) < 0: y = 0 - cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] else: - y = cy - crop_h//2 + y = cy - crop_h // 2 - if (cy + crop_h//2) >= h: - cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] + if (cy + crop_h // 2) >= h: + cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] return x, y, cropped_scene - + elif position == (Position.CENTER or Position.CENTER_OF_MASS): - - if (cx - crop_w//2) < 0: + if (cx - crop_w // 2) < 0: x = 0 - cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] else: - x = cx - crop_w//2 + x = cx - crop_w // 2 - if (cx + crop_w//2) >= w: - cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] - - if (cy - crop_h//2) < 0: + if (cx + crop_w // 2) >= w: + cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] + + if (cy - crop_h // 2) < 0: y = 0 - cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] else: - y = cy - crop_h//2 + y = cy - crop_h // 2 + + if (cy + crop_h // 2) >= h: + cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] - if (cy + crop_h//2) >= h: - cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] - return x, y, cropped_scene - + elif position == Position.CENTER_RIGHT: x = cx x_max = min(w, (cx + crop_w)) - + if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:,:(w - cx)] + cropped_scene = cropped_scene[:, : (w - cx)] - if (cy - crop_h//2) < 0: + if (cy - crop_h // 2) < 0: y = 0 - cropped_scene = cropped_scene[abs(cy-crop_h//2):,:] + cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] else: - y = cy - crop_h//2 + y = cy - crop_h // 2 + + if (cy + crop_h // 2) >= h: + cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] - if (cy + crop_h//2) >= h: - cropped_scene = cropped_scene[:-((cy + crop_h//2) - h + 1),:] - return x, y, cropped_scene - + elif position == Position.BOTTOM_LEFT: x = max(0, (cx - crop_w)) y = cy y_max = min(h, (cy + crop_h)) if x == 0 and (cx - crop_w < 0): - cropped_scene = cropped_scene[:,abs(cx-crop_w):] + cropped_scene = cropped_scene[:, abs(cx - crop_w) :] if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[:h-cy,:] + cropped_scene = cropped_scene[: h - cy, :] return x, y, cropped_scene - + elif position == Position.BOTTOM_CENTER: - - if (cx - crop_w//2) < 0: + if (cx - crop_w // 2) < 0: x = 0 - cropped_scene = cropped_scene[:,abs(cx-crop_w//2):] + cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] else: - x = cx - crop_w//2 + x = cx - crop_w // 2 - if (cx + crop_w//2) >= w: - cropped_scene = cropped_scene[:,:-((cx + crop_w//2) - w + 1)] + if (cx + crop_w // 2) >= w: + cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] y = cy y_max = min(h, (cy + crop_h)) if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[:h-cy,:] + cropped_scene = cropped_scene[: h - cy, :] return x, y, cropped_scene @@ -2011,19 +2001,20 @@ class CropAnnotator(BaseAnnotator): y_max = min(h, (cy + crop_h)) if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:, :(w - cx)] + cropped_scene = cropped_scene[:, : (w - cx)] if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[:h-cy,:] + cropped_scene = cropped_scene[: h - cy, :] return x, y, cropped_scene @staticmethod def place_cropped_scene( - scene: np.ndarray, - coordinates_and_image:Tuple[Tuple[int,int], np.ndarray] + scene: np.ndarray, coordinates_and_image: Tuple[Tuple[int, int], np.ndarray] ) -> np.ndarray: - x,y = coordinates_and_image[0:2] + x, y = coordinates_and_image[0:2] cropped_scene = coordinates_and_image[2] - scene[y:y+cropped_scene.shape[0], x:x+cropped_scene.shape[1]] = cropped_scene + scene[ + y : y + cropped_scene.shape[0], x : x + cropped_scene.shape[1] + ] = cropped_scene - return scene \ No newline at end of file + return scene From c7a6b38275e58b018df651964e4ecbd1518eaa7f Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 14 Mar 2024 13:32:52 +0100 Subject: [PATCH 12/17] initial version of refactored CropAnnotator --- docs/utils/file.md | 2 +- docs/utils/image.md | 19 ++- docs/utils/notebook.md | 2 +- docs/utils/video.md | 2 +- supervision/__init__.py | 2 +- supervision/annotators/core.py | 246 +++++---------------------------- supervision/utils/image.py | 76 +++++++++- 7 files changed, 132 insertions(+), 217 deletions(-) diff --git a/docs/utils/file.md b/docs/utils/file.md index aa086d32..2ba5a015 100644 --- a/docs/utils/file.md +++ b/docs/utils/file.md @@ -2,7 +2,7 @@ comments: true --- -# File +# File Utils

list_files_with_extensions

diff --git a/docs/utils/image.md b/docs/utils/image.md index 2bc53786..8a6768d7 100644 --- a/docs/utils/image.md +++ b/docs/utils/image.md @@ -1,8 +1,13 @@ --- comments: true +status: new --- -# ImageSink +# Image Utils + +
+

ImageSink

+
:::supervision.utils.image.ImageSink @@ -11,3 +16,15 @@ comments: true
:::supervision.utils.image.crop_image + +
+

resize_image

+
+ +:::supervision.utils.image.resize_image + +
+

place_image

+
+ +:::supervision.utils.image.place_image diff --git a/docs/utils/notebook.md b/docs/utils/notebook.md index 7ab233bf..d58bbcfc 100644 --- a/docs/utils/notebook.md +++ b/docs/utils/notebook.md @@ -2,7 +2,7 @@ comments: true --- -# Notebooks +# Notebooks Utils

plot_image

diff --git a/docs/utils/video.md b/docs/utils/video.md index 7fa7ed68..1f58450f 100644 --- a/docs/utils/video.md +++ b/docs/utils/video.md @@ -2,7 +2,7 @@ comments: true --- -# Video +# Video Utils

VideoInfo

diff --git a/supervision/__init__.py b/supervision/__init__.py index 0d048d68..d2636c1d 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -71,7 +71,7 @@ from supervision.geometry.utils import get_polygon_center from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision from supervision.tracker.byte_tracker.core import ByteTrack from supervision.utils.file import list_files_with_extensions -from supervision.utils.image import ImageSink, crop_image +from supervision.utils.image import ImageSink, crop_image, resize_image, place_image from supervision.utils.notebook import plot_image, plot_images_grid from supervision.utils.video import ( FPSMonitor, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index e9a3b674..c4d7b535 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -12,6 +12,7 @@ from supervision.detection.utils import clip_boxes, mask_to_polygons from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import draw_polygon from supervision.geometry.core import Position +from supervision.utils.image import crop_image, resize_image, place_image class BoundingBoxAnnotator(BaseAnnotator): @@ -1756,14 +1757,14 @@ class CropAnnotator(BaseAnnotator): A class for viewing a cropped part of detections with a selected zoom scale. """ - def __init__(self, position: Position = Position.TOP_CENTER, zoom_factor: int = 2): + def __init__(self, position: Position = Position.TOP_CENTER, scale_factor: int = 2): """ Args: position (Position): the anchor position to place cropped detections - zoom_factor (int): the percentage factor to zoom cropped image part + scale_factor (int): the percentage factor to zoom cropped image part """ self.position: Position = position - self.zoom_factor: int = zoom_factor + self.scale_factor: int = scale_factor def annotate( self, @@ -1793,228 +1794,55 @@ class CropAnnotator(BaseAnnotator): detections=detections ) ``` - - ![crop-annotator-example](https://media.roboflow.com/ #TODO update link - supervision-annotator-examples/______.png) """ - base_scene = scene.copy() + crops = [ + crop_image(image=scene, xyxy=xyxy) + for xyxy + in detections.xyxy.astype(int) + ] + resized_crops = [ + resize_image(image=crop, scale_factor=self.scale_factor) + for crop + in crops + ] anchors = detections.get_anchors_coordinates(anchor=self.position) - for detection_idx in range(len(detections)): - x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) - anchor = anchors[detection_idx].astype(int) - cropped_scene = base_scene[y1:y2, x1:x2] - resized_scene = self.apply_zoom( - cropped_scene=cropped_scene, zoom_factor=self.zoom_factor - ) - - coordinates_and_images = self.calculate_place_coordinates_and_crop( - scene=base_scene, - cropped_scene=resized_scene, - position=self.position, + for resized_crop, anchor in zip(resized_crops, anchors): + crop_wh = resized_crop.shape[1], resized_crop.shape[0] + xy, _ = self.calculate_border_coordinates( anchor_xy=anchor, + border_wh=crop_wh, + position=self.position ) - - scene = self.place_cropped_scene( - scene=base_scene, coordinates_and_image=coordinates_and_images - ) + scene = place_image(scene=scene, image=resized_crop, anchor=xy) return scene @staticmethod - def apply_zoom(cropped_scene: np.ndarray, zoom_factor: int) -> np.ndarray: - """ - Applies resize (zoom) to the cropped part of detections - - Args: - cropped_scene (np.ndarray): the cropped part of detections - - Returns: - resized_scene (np.ndarray): the resized cropped scene - """ - resized_scene = cv2.resize( - cropped_scene, - ( - zoom_factor * cropped_scene.shape[1], - zoom_factor * cropped_scene.shape[0], - ), - ) - - return resized_scene - - @staticmethod - def calculate_place_coordinates_and_crop( - scene: np.ndarray, - cropped_scene: np.ndarray, - position: Position, - anchor_xy: np.ndarray, - ) -> Tuple[Tuple[int, int], np.ndarray]: - """ - This method is responsible to calculate the x,y coordinates that the cropped - part of the detections will be placed and crop any exceeding part. - The returned x,y in all position cases is the final top left pixel. - - -Args: - scene (np.ndarray): The image where cropped detections will be placed. - cropped_scene (np.ndarray): The cropped part of the detections. - - -Returns: - x (int): position in horizontal axis to use for placement of cropped part. - y (int): position in vertical axis to use for placement of cropped part. - cropped_scene (np.ndarray): The cropped part after any applied trim. - """ - - h, w = scene.shape[:2] - crop_h, crop_w = cropped_scene.shape[:2] + def calculate_border_coordinates( + anchor_xy: Tuple[int, int], border_wh: Tuple[int, int], position: Position + ) -> Tuple[Tuple[int, int], Tuple[int, int]]: cx, cy = anchor_xy + width, height = border_wh if position == Position.TOP_LEFT: - x = max(0, (cx - crop_w)) - y = max(0, (cy - crop_h)) - - if x == 0 and (cx - crop_w < 0): - cropped_scene = cropped_scene[:, abs(cx - crop_w) :] - if y == 0 and (cy - crop_h < 0): - cropped_scene = cropped_scene[abs(cy - crop_h) :, :] - - return x, y, cropped_scene - + return (cx - width, cy - height), (cx, cy) elif position == Position.TOP_CENTER: - if (cx - crop_w // 2) < 0: - x = 0 - cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] - else: - x = cx - crop_w // 2 - - if (cx + crop_w // 2) >= w: - cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] - - y = max(0, (cy - crop_h)) - - if y == 0 and (cy - crop_h < 0): - cropped_scene = cropped_scene[abs(cy - crop_h) :, :] - - return x, y, cropped_scene - + return (cx - width // 2, cy), (cx + width // 2, cy - height) elif position == Position.TOP_RIGHT: - x = cx - - x_max = min(w, (cx + crop_w)) - y = max(0, (cy - crop_h)) - - if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:, : (w - cx)] - if y == 0 and (cy - crop_h < 0): - cropped_scene = cropped_scene[abs(cy - crop_h) :, :] - - return x, y, cropped_scene - + return (cx, cy), (cx + width, cy - height) elif position == Position.CENTER_LEFT: - x = max(0, (cx - crop_w)) - - if x == 0 and (cx - crop_w < 0): - cropped_scene = cropped_scene[:, abs(cx - crop_w) :] - if (cy - crop_h // 2) < 0: - y = 0 - cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] - else: - y = cy - crop_h // 2 - - if (cy + crop_h // 2) >= h: - cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] - - return x, y, cropped_scene - - elif position == (Position.CENTER or Position.CENTER_OF_MASS): - if (cx - crop_w // 2) < 0: - x = 0 - cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] - else: - x = cx - crop_w // 2 - - if (cx + crop_w // 2) >= w: - cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] - - if (cy - crop_h // 2) < 0: - y = 0 - cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] - else: - y = cy - crop_h // 2 - - if (cy + crop_h // 2) >= h: - cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] - - return x, y, cropped_scene - + return (cx - width, cy - height // 2), (cx, cy + height // 2) + elif position == Position.CENTER or position == Position.CENTER_OF_MASS: + return ( + (cx - width // 2, cy - height // 2), + (cx + width // 2, cy + height // 2), + ) elif position == Position.CENTER_RIGHT: - x = cx - x_max = min(w, (cx + crop_w)) - - if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:, : (w - cx)] - - if (cy - crop_h // 2) < 0: - y = 0 - cropped_scene = cropped_scene[abs(cy - crop_h // 2) :, :] - else: - y = cy - crop_h // 2 - - if (cy + crop_h // 2) >= h: - cropped_scene = cropped_scene[: -((cy + crop_h // 2) - h + 1), :] - - return x, y, cropped_scene - + return (cx, cy - height // 2), (cx + width, cy + height // 2) elif position == Position.BOTTOM_LEFT: - x = max(0, (cx - crop_w)) - y = cy - y_max = min(h, (cy + crop_h)) - - if x == 0 and (cx - crop_w < 0): - cropped_scene = cropped_scene[:, abs(cx - crop_w) :] - if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[: h - cy, :] - - return x, y, cropped_scene - + return (cx - width, cy), (cx, cy + height) elif position == Position.BOTTOM_CENTER: - if (cx - crop_w // 2) < 0: - x = 0 - cropped_scene = cropped_scene[:, abs(cx - crop_w // 2) :] - else: - x = cx - crop_w // 2 - - if (cx + crop_w // 2) >= w: - cropped_scene = cropped_scene[:, : -((cx + crop_w // 2) - w + 1)] - - y = cy - y_max = min(h, (cy + crop_h)) - - if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[: h - cy, :] - - return x, y, cropped_scene - + return (cx - width // 2, cy), (cx + width // 2, cy + height) elif position == Position.BOTTOM_RIGHT: - x = cx - y = cy - x_max = min(w, (cx + crop_w)) - y_max = min(h, (cy + crop_h)) - - if x_max == w and (cx + crop_w > w): - cropped_scene = cropped_scene[:, : (w - cx)] - if y_max == h and (cy + crop_h > h): - cropped_scene = cropped_scene[: h - cy, :] - - return x, y, cropped_scene - - @staticmethod - def place_cropped_scene( - scene: np.ndarray, coordinates_and_image: Tuple[Tuple[int, int], np.ndarray] - ) -> np.ndarray: - x, y = coordinates_and_image[0:2] - cropped_scene = coordinates_and_image[2] - scene[ - y : y + cropped_scene.shape[0], x : x + cropped_scene.shape[1] - ] = cropped_scene - - return scene + return (cx, cy), (cx + width, cy + height) diff --git a/supervision/utils/image.py b/supervision/utils/image.py index 66401886..684a3b86 100644 --- a/supervision/utils/image.py +++ b/supervision/utils/image.py @@ -1,6 +1,6 @@ import os import shutil -from typing import Optional +from typing import Optional, Tuple import cv2 import numpy as np @@ -32,8 +32,78 @@ def crop_image(image: np.ndarray, xyxy: np.ndarray) -> np.ndarray: xyxy = np.round(xyxy).astype(int) x1, y1, x2, y2 = xyxy - cropped_img = image[y1:y2, x1:x2] - return cropped_img + return image[y1:y2, x1:x2] + + +def resize_image(image: np.ndarray, scale_factor: float) -> np.ndarray: + """ + Resizes an image by a given scale factor using cv2.INTER_LINEAR interpolation. + + Args: + image (np.ndarray): The input image to be resized. + scale_factor (float): The factor by which the image will be scaled. Scale factor + > 1.0 zooms in, < 1.0 zooms out. + + Returns: + np.ndarray: The resized image. + + Raises: + ValueError: If the scale factor is non-positive. + """ + if scale_factor <= 0: + raise ValueError("Scale factor must be positive.") + + old_width, old_height = image.shape[1], image.shape[0] + nwe_width = int(old_width * scale_factor) + new_height = int(old_height * scale_factor) + + return cv2.resize(image, (nwe_width, new_height), interpolation=cv2.INTER_LINEAR) + + +def place_image( + scene: np.ndarray, + image: np.ndarray, + anchor: Tuple[int, int] +) -> np.ndarray: + """ + Places an image onto a scene at a given anchor point, handling cases where + the image's position is partially or completely outside the scene's bounds. + + Args: + scene (np.ndarray): The background scene onto which the image is placed. + image (np.ndarray): The image to be placed onto the scene. + anchor (Tuple[int, int]): The (x, y) coordinates in the scene where the + top-left corner of the image will be placed. + + Returns: + np.ndarray: The modified scene with the image placed at the anchor point, + or unchanged if the image placement is completely outside the scene. + """ + scene_height, scene_width = scene.shape[:2] + image_height, image_width = image.shape[:2] + anchor_x, anchor_y = anchor + + is_out_horizontally = anchor_x + image_width <= 0 or anchor_x >= scene_width + is_out_vertically = anchor_y + image_height <= 0 or anchor_y >= scene_height + + if is_out_horizontally or is_out_vertically: + return scene + + start_y = max(anchor_y, 0) + start_x = max(anchor_x, 0) + end_y = min(scene_height, anchor_y + image_height) + end_x = min(scene_width, anchor_x + image_width) + + crop_start_y = max(-anchor_y, 0) + crop_start_x = max(-anchor_x, 0) + crop_end_y = image_height - max((anchor_y + image_height) - scene_height, 0) + crop_end_x = image_width - max((anchor_x + image_width) - scene_width, 0) + + scene[start_y:end_y, start_x:end_x] = image[ + crop_start_y:crop_end_y, crop_start_x:crop_end_x + ] + + return scene class ImageSink: From d1003d420cd4047509ceb3ac2506c0e448ecce51 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:33:17 +0000 Subject: [PATCH 13/17] =?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 | 14 ++++---------- supervision/utils/image.py | 4 +--- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/supervision/__init__.py b/supervision/__init__.py index d2636c1d..8f757c80 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -71,7 +71,7 @@ from supervision.geometry.utils import get_polygon_center from supervision.metrics.detection import ConfusionMatrix, MeanAveragePrecision from supervision.tracker.byte_tracker.core import ByteTrack from supervision.utils.file import list_files_with_extensions -from supervision.utils.image import ImageSink, crop_image, resize_image, place_image +from supervision.utils.image import ImageSink, crop_image, place_image, resize_image from supervision.utils.notebook import plot_image, plot_images_grid from supervision.utils.video import ( FPSMonitor, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index c4d7b535..6f747b9a 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -12,7 +12,7 @@ from supervision.detection.utils import clip_boxes, mask_to_polygons from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import draw_polygon from supervision.geometry.core import Position -from supervision.utils.image import crop_image, resize_image, place_image +from supervision.utils.image import crop_image, place_image, resize_image class BoundingBoxAnnotator(BaseAnnotator): @@ -1796,23 +1796,17 @@ class CropAnnotator(BaseAnnotator): ``` """ crops = [ - crop_image(image=scene, xyxy=xyxy) - for xyxy - in detections.xyxy.astype(int) + crop_image(image=scene, xyxy=xyxy) for xyxy in detections.xyxy.astype(int) ] resized_crops = [ - resize_image(image=crop, scale_factor=self.scale_factor) - for crop - in crops + resize_image(image=crop, scale_factor=self.scale_factor) for crop in crops ] anchors = detections.get_anchors_coordinates(anchor=self.position) for resized_crop, anchor in zip(resized_crops, anchors): crop_wh = resized_crop.shape[1], resized_crop.shape[0] xy, _ = self.calculate_border_coordinates( - anchor_xy=anchor, - border_wh=crop_wh, - position=self.position + anchor_xy=anchor, border_wh=crop_wh, position=self.position ) scene = place_image(scene=scene, image=resized_crop, anchor=xy) diff --git a/supervision/utils/image.py b/supervision/utils/image.py index 684a3b86..4b9372ee 100644 --- a/supervision/utils/image.py +++ b/supervision/utils/image.py @@ -61,9 +61,7 @@ def resize_image(image: np.ndarray, scale_factor: float) -> np.ndarray: def place_image( - scene: np.ndarray, - image: np.ndarray, - anchor: Tuple[int, int] + scene: np.ndarray, image: np.ndarray, anchor: Tuple[int, int] ) -> np.ndarray: """ Places an image onto a scene at a given anchor point, handling cases where From 4ba3780d86def05a4505e04872a1c32ffad1d2b5 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 14 Mar 2024 14:12:50 +0100 Subject: [PATCH 14/17] CropAnnotator updates and fixes --- supervision/annotators/core.py | 51 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 6f747b9a..8e889cd3 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -12,7 +12,7 @@ from supervision.detection.utils import clip_boxes, mask_to_polygons from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import draw_polygon from supervision.geometry.core import Position -from supervision.utils.image import crop_image, place_image, resize_image +from supervision.utils.image import crop_image, resize_image, place_image class BoundingBoxAnnotator(BaseAnnotator): @@ -1796,47 +1796,50 @@ class CropAnnotator(BaseAnnotator): ``` """ crops = [ - crop_image(image=scene, xyxy=xyxy) for xyxy in detections.xyxy.astype(int) + crop_image(image=scene, xyxy=xyxy) + for xyxy + in detections.xyxy.astype(int) ] resized_crops = [ - resize_image(image=crop, scale_factor=self.scale_factor) for crop in crops + resize_image(image=crop, scale_factor=self.scale_factor) + for crop + in crops ] - anchors = detections.get_anchors_coordinates(anchor=self.position) + anchors = detections.get_anchors_coordinates(anchor=self.position).astype(int) for resized_crop, anchor in zip(resized_crops, anchors): crop_wh = resized_crop.shape[1], resized_crop.shape[0] - xy, _ = self.calculate_border_coordinates( - anchor_xy=anchor, border_wh=crop_wh, position=self.position + crop_anchor = self.calculate_crop_coordinates( + anchor=anchor, + crop_wh=crop_wh, + position=self.position ) - scene = place_image(scene=scene, image=resized_crop, anchor=xy) + scene = place_image(scene=scene, image=resized_crop, anchor=crop_anchor) return scene @staticmethod - def calculate_border_coordinates( - anchor_xy: Tuple[int, int], border_wh: Tuple[int, int], position: Position - ) -> Tuple[Tuple[int, int], Tuple[int, int]]: - cx, cy = anchor_xy - width, height = border_wh + def calculate_crop_coordinates( + anchor: Tuple[int, int], crop_wh: Tuple[int, int], position: Position + ) -> Tuple[int, int]: + anchor_x, anchor_y = anchor + width, height = crop_wh if position == Position.TOP_LEFT: - return (cx - width, cy - height), (cx, cy) + return anchor_x - width, anchor_y - height elif position == Position.TOP_CENTER: - return (cx - width // 2, cy), (cx + width // 2, cy - height) + return anchor_x - width // 2, anchor_y - height elif position == Position.TOP_RIGHT: - return (cx, cy), (cx + width, cy - height) + return anchor_x, anchor_y - height elif position == Position.CENTER_LEFT: - return (cx - width, cy - height // 2), (cx, cy + height // 2) + return anchor_x - width, anchor_y - height // 2 elif position == Position.CENTER or position == Position.CENTER_OF_MASS: - return ( - (cx - width // 2, cy - height // 2), - (cx + width // 2, cy + height // 2), - ) + return anchor_x - width // 2, anchor_y - height // 2 elif position == Position.CENTER_RIGHT: - return (cx, cy - height // 2), (cx + width, cy + height // 2) + return anchor_x, anchor_y - height // 2 elif position == Position.BOTTOM_LEFT: - return (cx - width, cy), (cx, cy + height) + return anchor_x - width, anchor_y elif position == Position.BOTTOM_CENTER: - return (cx - width // 2, cy), (cx + width // 2, cy + height) + return anchor_x - width // 2, anchor_y elif position == Position.BOTTOM_RIGHT: - return (cx, cy), (cx + width, cy + height) + return anchor_x, anchor_y From d6589443d72124fb835853ebbcf1e7fa4d8091c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:13:08 +0000 Subject: [PATCH 15/17] =?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 | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 8e889cd3..17d1595c 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -12,7 +12,7 @@ from supervision.detection.utils import clip_boxes, mask_to_polygons from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import draw_polygon from supervision.geometry.core import Position -from supervision.utils.image import crop_image, resize_image, place_image +from supervision.utils.image import crop_image, place_image, resize_image class BoundingBoxAnnotator(BaseAnnotator): @@ -1796,23 +1796,17 @@ class CropAnnotator(BaseAnnotator): ``` """ crops = [ - crop_image(image=scene, xyxy=xyxy) - for xyxy - in detections.xyxy.astype(int) + crop_image(image=scene, xyxy=xyxy) for xyxy in detections.xyxy.astype(int) ] resized_crops = [ - resize_image(image=crop, scale_factor=self.scale_factor) - for crop - in crops + resize_image(image=crop, scale_factor=self.scale_factor) for crop in crops ] anchors = detections.get_anchors_coordinates(anchor=self.position).astype(int) for resized_crop, anchor in zip(resized_crops, anchors): crop_wh = resized_crop.shape[1], resized_crop.shape[0] crop_anchor = self.calculate_crop_coordinates( - anchor=anchor, - crop_wh=crop_wh, - position=self.position + anchor=anchor, crop_wh=crop_wh, position=self.position ) scene = place_image(scene=scene, image=resized_crop, anchor=crop_anchor) From 0c04647d004315b2d8698197d740019d23dc0fc6 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 14 Mar 2024 14:37:26 +0100 Subject: [PATCH 16/17] Improved docstrings --- supervision/annotators/core.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 17d1595c..a94f7e12 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1754,14 +1754,17 @@ class PercentageBarAnnotator(BaseAnnotator): class CropAnnotator(BaseAnnotator): """ - A class for viewing a cropped part of detections with a selected zoom scale. + A class for drawing scaled up crops of detections on the scene. """ def __init__(self, position: Position = Position.TOP_CENTER, scale_factor: int = 2): """ Args: - position (Position): the anchor position to place cropped detections - scale_factor (int): the percentage factor to zoom cropped image part + position (Position): The anchor position for placing the cropped and scaled + part of the detection in the scene. + 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. """ self.position: Position = position self.scale_factor: int = scale_factor @@ -1772,7 +1775,11 @@ class CropAnnotator(BaseAnnotator): detections: Detections, ) -> np.ndarray: """ - Annotates the given scene with cropped parts from the provided detections. + 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 + and scaled according to the annotator's scale factor before being placed back + onto the scene at the specified position. + Args: scene (np.ndarray): The image where cropped detection will be placed. From 22d97b8a4610d586f902f899ebffbec159e9cf4e Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Thu, 14 Mar 2024 14:43:19 +0100 Subject: [PATCH 17/17] add CropAnnotator docs --- docs/annotators.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/annotators.md b/docs/annotators.md index 5dfcda08..168028ee 100644 --- a/docs/annotators.md +++ b/docs/annotators.md @@ -278,6 +278,27 @@ status: new
+=== "Crop" + + ```python + import supervision as sv + + image = ... + detections = sv.Detections(...) + + crop_annotator = sv.CropAnnotator() + annotated_frame = crop_annotator.annotate( + scene=image.copy(), + detections=detections + ) + ``` + +
+ + ![label-annotator-example](https://media.roboflow.com/supervision-annotator-examples/label-annotator-example-purple.png){ align=center width="800" } + +
+ === "Blur" ```python @@ -488,6 +509,12 @@ status: new :::supervision.annotators.core.TraceAnnotator + + +:::supervision.annotators.core.CropAnnotator +