From 82ab6c57cb8cb2f863e27c551e31e310e18fdb38 Mon Sep 17 00:00:00 2001 From: LinasKo Date: Mon, 13 May 2024 14:14:42 +0300 Subject: [PATCH] infrenceSlicer: resolution_wh --- .../detection/tools/inference_slicer.py | 19 ++++++++++++------- supervision/detection/utils.py | 19 ++++--------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/supervision/detection/tools/inference_slicer.py b/supervision/detection/tools/inference_slicer.py index a951664c..82551434 100644 --- a/supervision/detection/tools/inference_slicer.py +++ b/supervision/detection/tools/inference_slicer.py @@ -1,5 +1,5 @@ from concurrent.futures import ThreadPoolExecutor, as_completed -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Tuple import numpy as np @@ -11,24 +11,28 @@ from supervision.utils.image import crop_image def move_detections( detections: Detections, offset: np.ndarray, - image_shape: Optional[Union[Tuple[int, int, int], Tuple[int, int]]] = None, + resolution_wh: Optional[Tuple[int, int]] = None, ) -> Detections: """ Args: detections (sv.Detections): Detections object to be moved. offset (np.ndarray): An array of shape `(2,)` containing offset values in format is `[dx, dy]`. - image_size (Tuple, optional): A tuple of image shape. Can be `(2,)` or `(3,)`. - Important when moving for segmentation detections, as it defines mask array - size. + resolution_wh (Tuple[int, int]): The width and height of the desired mask + resolution. Required for segmentation detections. Returns: (sv.Detections) repositioned Detections object. """ detections.xyxy = move_boxes(xyxy=detections.xyxy, offset=offset) if detections.mask is not None: + if resolution_wh is None: + raise ValueError( + "Resolution width and height are required for moving segmentation " + "detections. This should be the same as (width, height) of image shape." + ) detections.mask = move_masks( - masks=detections.mask, offset=offset, desired_shape=image_shape + masks=detections.mask, offset=offset, resolution_wh=resolution_wh ) return detections @@ -138,8 +142,9 @@ class InferenceSlicer: """ image_slice = crop_image(image=image, xyxy=offset) detections = self.callback(image_slice) + resolution_wh = (image.shape[1], image.shape[0]) detections = move_detections( - detections=detections, offset=offset[:2], image_shape=image.shape + detections=detections, offset=offset[:2], resolution_wh=resolution_wh ) return detections diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index d20cadea..49b91795 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -595,24 +595,17 @@ def move_boxes(xyxy: np.ndarray, offset: np.ndarray) -> np.ndarray: def move_masks( masks: np.ndarray, offset: np.ndarray, - desired_shape: Optional[Union[Tuple[int, int, int], Tuple[int, int]]] = None, + resolution_wh: Tuple[int, int] = None, ) -> np.ndarray: """ Offset the masks in an array by the specified (x, y) amount. - Note the axis orders: - - - `masks`: array of shape `(n, y, x)` - - `offset`: array of ints: `(x, y)` - - `desired_shape`: tuple of ints, shaped `(y, x)` or `(y, x, ...)` - Args: masks (np.ndarray): array of bools offset (np.ndarray): An array of shape `(2,)` containing non-negative int values `[dx, dy]`. - desired_shape (Tuple, optional): Final shape of the mask in the format - `(height, width)`, `(height, width, ...)`. The masks will be padded to match - the first 2 shape dimensions. Note the axis order (y,x)! + resolution_wh (Tuple[int, int]): The width and height of the desired mask + resolution. Returns: (np.ndarray) repositioned masks, optionally padded to the specified shape. @@ -621,11 +614,7 @@ def move_masks( if offset[0] < 0 or offset[1] < 0: raise ValueError(f"Offset values must be non-negative integers. Got: {offset}") - size_y, size_x = masks.shape[1:] + offset[::-1] - if desired_shape is not None: - size_y, size_x = desired_shape[:2] - - mask_array = np.full((masks.shape[0], size_y, size_x), False) + mask_array = np.full((masks.shape[0], resolution_wh[1], resolution_wh[0]), False) mask_array[ :, offset[1] : masks.shape[1] + offset[1],