1- import typing
2- from typing import Optional , Sequence , Tuple , Union
1+ from typing import Optional , Sequence , Tuple
32
43import cv2
54import numpy as np
65
6+ from albumentations .augmentations .utils import (
7+ _maybe_process_in_chunks ,
8+ preserve_channel_dim ,
9+ )
10+
711from ...core .bbox_utils import denormalize_bbox , normalize_bbox
8- from ...core .transforms_interface import BoxType , KeypointType
9- from ..functional import _maybe_process_in_chunks , preserve_channel_dim
12+ from ...core .transforms_interface import BoxInternalType , KeypointInternalType
1013from ..geometric import functional as FGeometric
1114
1215__all__ = [
@@ -55,7 +58,12 @@ def random_crop(img: np.ndarray, crop_height: int, crop_width: int, h_start: flo
5558
5659
5760def crop_bbox_by_coords (
58- bbox : BoxType , crop_coords : Tuple [int , int , int , int ], crop_height : int , crop_width : int , rows : int , cols : int
61+ bbox : BoxInternalType ,
62+ crop_coords : Tuple [int , int , int , int ],
63+ crop_height : int ,
64+ crop_width : int ,
65+ rows : int ,
66+ cols : int ,
5967):
6068 """Crop a bounding box using the provided coordinates of bottom-left and top-right corners in pixels and the
6169 required height and width of the crop.
@@ -80,13 +88,15 @@ def crop_bbox_by_coords(
8088
8189
8290def bbox_random_crop (
83- bbox : BoxType , crop_height : int , crop_width : int , h_start : float , w_start : float , rows : int , cols : int
91+ bbox : BoxInternalType , crop_height : int , crop_width : int , h_start : float , w_start : float , rows : int , cols : int
8492):
8593 crop_coords = get_random_crop_coords (rows , cols , crop_height , crop_width , h_start , w_start )
8694 return crop_bbox_by_coords (bbox , crop_coords , crop_height , crop_width , rows , cols )
8795
8896
89- def crop_keypoint_by_coords (keypoint : KeypointType , crop_coords : Tuple [int , int , int , int ]): # skipcq: PYL-W0613
97+ def crop_keypoint_by_coords (
98+ keypoint : KeypointInternalType , crop_coords : Tuple [int , int , int , int ]
99+ ): # skipcq: PYL-W0613
90100 """Crop a keypoint using the provided coordinates of bottom-left and top-right corners in pixels and the
91101 required height and width of the crop.
92102
@@ -104,7 +114,13 @@ def crop_keypoint_by_coords(keypoint: KeypointType, crop_coords: Tuple[int, int,
104114
105115
106116def keypoint_random_crop (
107- keypoint : KeypointType , crop_height : int , crop_width : int , h_start : float , w_start : float , rows : int , cols : int
117+ keypoint : KeypointInternalType ,
118+ crop_height : int ,
119+ crop_width : int ,
120+ h_start : float ,
121+ w_start : float ,
122+ rows : int ,
123+ cols : int ,
108124):
109125 """Keypoint random crop.
110126
@@ -147,12 +163,12 @@ def center_crop(img: np.ndarray, crop_height: int, crop_width: int):
147163 return img
148164
149165
150- def bbox_center_crop (bbox : BoxType , crop_height : int , crop_width : int , rows : int , cols : int ):
166+ def bbox_center_crop (bbox : BoxInternalType , crop_height : int , crop_width : int , rows : int , cols : int ):
151167 crop_coords = get_center_crop_coords (rows , cols , crop_height , crop_width )
152168 return crop_bbox_by_coords (bbox , crop_coords , crop_height , crop_width , rows , cols )
153169
154170
155- def keypoint_center_crop (keypoint : KeypointType , crop_height : int , crop_width : int , rows : int , cols : int ):
171+ def keypoint_center_crop (keypoint : KeypointInternalType , crop_height : int , crop_width : int , rows : int , cols : int ):
156172 """Keypoint center crop.
157173
158174 Args:
@@ -192,7 +208,7 @@ def crop(img: np.ndarray, x_min: int, y_min: int, x_max: int, y_max: int):
192208 return img [y_min :y_max , x_min :x_max ]
193209
194210
195- def bbox_crop (bbox : BoxType , x_min : int , y_min : int , x_max : int , y_max : int , rows : int , cols : int ):
211+ def bbox_crop (bbox : BoxInternalType , x_min : int , y_min : int , x_max : int , y_max : int , rows : int , cols : int ):
196212 """Crop a bounding box.
197213
198214 Args:
@@ -230,9 +246,9 @@ def clamping_crop(img: np.ndarray, x_min: int, y_min: int, x_max: int, y_max: in
230246@preserve_channel_dim
231247def crop_and_pad (
232248 img : np .ndarray ,
233- crop_params : Sequence [int ],
234- pad_params : Sequence [int ],
235- pad_value : Union [ int , float ],
249+ crop_params : Optional [ Sequence [int ] ],
250+ pad_params : Optional [ Sequence [int ] ],
251+ pad_value : Optional [ float ],
236252 rows : int ,
237253 cols : int ,
238254 interpolation : int ,
@@ -242,7 +258,9 @@ def crop_and_pad(
242258 if crop_params is not None and any (i != 0 for i in crop_params ):
243259 img = crop (img , * crop_params )
244260 if pad_params is not None and any (i != 0 for i in pad_params ):
245- img = FGeometric .pad_with_params (img , * pad_params , border_mode = pad_mode , value = pad_value )
261+ img = FGeometric .pad_with_params (
262+ img , pad_params [0 ], pad_params [1 ], pad_params [2 ], pad_params [3 ], border_mode = pad_mode , value = pad_value
263+ )
246264
247265 if keep_size :
248266 resize_fn = _maybe_process_in_chunks (cv2 .resize , dsize = (cols , rows ), interpolation = interpolation )
@@ -252,15 +270,14 @@ def crop_and_pad(
252270
253271
254272def crop_and_pad_bbox (
255- bbox : BoxType ,
273+ bbox : BoxInternalType ,
256274 crop_params : Optional [Sequence [int ]],
257275 pad_params : Optional [Sequence [int ]],
258276 rows ,
259277 cols ,
260278 result_rows ,
261279 result_cols ,
262- keep_size : bool ,
263- ) -> BoxType :
280+ ) -> BoxInternalType :
264281 x1 , y1 , x2 , y2 = denormalize_bbox (bbox , rows , cols )[:4 ]
265282
266283 if crop_params is not None :
@@ -274,15 +291,15 @@ def crop_and_pad_bbox(
274291
275292
276293def crop_and_pad_keypoint (
277- keypoint : KeypointType ,
294+ keypoint : KeypointInternalType ,
278295 crop_params : Optional [Sequence [int ]],
279296 pad_params : Optional [Sequence [int ]],
280297 rows : int ,
281298 cols : int ,
282299 result_rows : int ,
283300 result_cols : int ,
284301 keep_size : bool ,
285- ) -> KeypointType :
302+ ) -> KeypointInternalType :
286303 x , y , angle , scale = keypoint [:4 ]
287304
288305 if crop_params is not None :
0 commit comments