infrenceSlicer: resolution_wh

This commit is contained in:
LinasKo
2024-05-13 14:14:42 +03:00
parent 816aecb51b
commit 82ab6c57cb
2 changed files with 16 additions and 22 deletions
@@ -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
+4 -15
View File
@@ -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],