Skip to content

Commit 1ff0f51

Browse files
committed
Move resize_mask_to_unpadded_box to inference.ops
1 parent aa686b5 commit 1ff0f51

2 files changed

Lines changed: 99 additions & 101 deletions

File tree

abraia/hailo/toolbox.py

Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import sys
32
import cv2
4-
import time
53
import queue
64
import shlex
75
import logging
@@ -15,7 +13,7 @@
1513
from typing import Dict, List, Optional, Tuple
1614

1715
from ..utils import download_url, get_remote_file_size
18-
from ..inference.ops import sigmoid, softmax
16+
from ..inference.ops import sigmoid, softmax, resize_mask_to_unpadded_box
1917

2018
logger = logging.getLogger(__name__)
2119

@@ -257,78 +255,78 @@ def get_labels(labels_path: str) -> list:
257255
return class_names
258256

259257

260-
def find_shape_closest_to_target(mask_size, target_height, target_width):
261-
"""
262-
Find the (height, width) pair whose product equals ``mask_size`` and whose
263-
Manhattan distance to (target_height, target_width) is minimal.
264-
265-
Manhattan distance used:
266-
|h − target_height| + |w − target_width|
267-
268-
Args:
269-
mask_size (int): Total number of pixels in the flattened mask.
270-
target_height (int): Desired height.
271-
target_width (int): Desired width.
272-
273-
Returns:
274-
tuple[int, int] | None: Best-matching (height, width), or None if none found.
275-
"""
276-
best_shape = None
277-
min_diff = float("inf")
278-
279-
for h in range(1, mask_size + 1):
280-
if mask_size % h:
281-
continue
282-
w = mask_size // h
283-
diff = abs(h - target_height) + abs(w - target_width)
284-
if diff < min_diff:
285-
min_diff = diff
286-
best_shape = (h, w)
287-
288-
return best_shape
289-
290-
291-
def resize_mask_to_unpadded_box(mask_1d, box_on_input_image, box_on_padded_image):
292-
"""
293-
Resize the mask from the padded box to match the unpadded box size.
294-
295-
Args:
296-
mask_1d (np.ndarray): 1D binary mask.
297-
padded_box (list): [ymin, xmin, ymax, xmax] in 640x640 padded image.
298-
unpadded_box (list): [ymin, xmin, ymax, xmax] after unpadding.
299-
300-
Returns:
301-
np.ndarray: Resized 2D mask for the unpadded box size.
302-
"""
303-
try:
304-
# Step 1: Get the shape of the padded box
305-
x1_p, y1_p, x2_p, y2_p = box_on_padded_image
306-
h_p = y2_p - y1_p
307-
w_p = x2_p - x1_p
308-
309-
# Step 2: Reshape the mask to original (padded) box shape
310-
try:
311-
mask_2d = mask_1d.reshape((h_p, w_p))
312-
except ValueError:
313-
closest_shape = find_shape_closest_to_target(mask_1d.size, h_p, w_p)
314-
if not closest_shape:
315-
return None
316-
317-
h, w = closest_shape
318-
mask_2d = mask_1d.reshape((h, w))
319-
320-
# Step 3: Get new shape after unpadding
321-
x1_u, y1_u, x2_u, y2_u = box_on_input_image
322-
h_u = y2_u - y1_u
323-
w_u = x2_u - x1_u
324-
325-
# Step 4: Resize the mask to the unpadded box shape
326-
resized_mask = cv2.resize(mask_2d.astype(np.uint8), (w_u, h_u), interpolation=cv2.INTER_NEAREST)
327-
328-
except Exception:
329-
return None
330-
331-
return resized_mask
258+
# def find_shape_closest_to_target(mask_size, target_height, target_width):
259+
# """
260+
# Find the (height, width) pair whose product equals ``mask_size`` and whose
261+
# Manhattan distance to (target_height, target_width) is minimal.
262+
263+
# Manhattan distance used:
264+
# |h − target_height| + |w − target_width|
265+
266+
# Args:
267+
# mask_size (int): Total number of pixels in the flattened mask.
268+
# target_height (int): Desired height.
269+
# target_width (int): Desired width.
270+
271+
# Returns:
272+
# tuple[int, int] | None: Best-matching (height, width), or None if none found.
273+
# """
274+
# best_shape = None
275+
# min_diff = float("inf")
276+
277+
# for h in range(1, mask_size + 1):
278+
# if mask_size % h:
279+
# continue
280+
# w = mask_size // h
281+
# diff = abs(h - target_height) + abs(w - target_width)
282+
# if diff < min_diff:
283+
# min_diff = diff
284+
# best_shape = (h, w)
285+
286+
# return best_shape
287+
288+
289+
# def resize_mask_to_unpadded_box(mask_1d, box_on_input_image, box_on_padded_image):
290+
# """
291+
# Resize the mask from the padded box to match the unpadded box size.
292+
293+
# Args:
294+
# mask_1d (np.ndarray): 1D binary mask.
295+
# padded_box (list): [ymin, xmin, ymax, xmax] in 640x640 padded image.
296+
# unpadded_box (list): [ymin, xmin, ymax, xmax] after unpadding.
297+
298+
# Returns:
299+
# np.ndarray: Resized 2D mask for the unpadded box size.
300+
# """
301+
# try:
302+
# # Step 1: Get the shape of the padded box
303+
# x1_p, y1_p, x2_p, y2_p = box_on_padded_image
304+
# h_p = y2_p - y1_p
305+
# w_p = x2_p - x1_p
306+
307+
# # Step 2: Reshape the mask to original (padded) box shape
308+
# try:
309+
# mask_2d = mask_1d.reshape((h_p, w_p))
310+
# except ValueError:
311+
# closest_shape = find_shape_closest_to_target(mask_1d.size, h_p, w_p)
312+
# if not closest_shape:
313+
# return None
314+
315+
# h, w = closest_shape
316+
# mask_2d = mask_1d.reshape((h, w))
317+
318+
# # Step 3: Get new shape after unpadding
319+
# x1_u, y1_u, x2_u, y2_u = box_on_input_image
320+
# h_u = y2_u - y1_u
321+
# w_u = x2_u - x1_u
322+
323+
# # Step 4: Resize the mask to the unpadded box shape
324+
# resized_mask = cv2.resize(mask_2d.astype(np.uint8), (w_u, h_u), interpolation=cv2.INTER_NEAREST)
325+
326+
# except Exception:
327+
# return None
328+
329+
# return resized_mask
332330

333331

334332
def convert_box_from_normalized(normalized_box: list,

abraia/inference/ops.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -294,35 +294,35 @@ def normalize_vector(vector):
294294
return vector / np.linalg.norm(vector)
295295

296296

297-
def find_shape_closest_to_target(mask_size, target_height, target_width):
298-
"""
299-
Find the (height, width) pair whose product equals ``mask_size`` and whose
300-
Manhattan distance to (target_height, target_width) is minimal.
301-
302-
Manhattan distance used:
303-
|h − target_height| + |w − target_width|
297+
# def find_shape_closest_to_target(mask_size, target_height, target_width):
298+
# """
299+
# Find the (height, width) pair whose product equals ``mask_size`` and whose
300+
# Manhattan distance to (target_height, target_width) is minimal.
304301

305-
Args:
306-
mask_size (int): Total number of pixels in the flattened mask.
307-
target_height (int): Desired height.
308-
target_width (int): Desired width.
302+
# Manhattan distance used:
303+
# |h − target_height| + |w − target_width|
309304

310-
Returns:
311-
tuple[int, int] | None: Best-matching (height, width), or None if none found.
312-
"""
313-
best_shape = None
314-
min_diff = float("inf")
315-
316-
for h in range(1, mask_size + 1):
317-
if mask_size % h:
318-
continue
319-
w = mask_size // h
320-
diff = abs(h - target_height) + abs(w - target_width)
321-
if diff < min_diff:
322-
min_diff = diff
323-
best_shape = (h, w)
305+
# Args:
306+
# mask_size (int): Total number of pixels in the flattened mask.
307+
# target_height (int): Desired height.
308+
# target_width (int): Desired width.
324309

325-
return best_shape
310+
# Returns:
311+
# tuple[int, int] | None: Best-matching (height, width), or None if none found.
312+
# """
313+
# best_shape = None
314+
# min_diff = float("inf")
315+
316+
# for h in range(1, mask_size + 1):
317+
# if mask_size % h:
318+
# continue
319+
# w = mask_size // h
320+
# diff = abs(h - target_height) + abs(w - target_width)
321+
# if diff < min_diff:
322+
# min_diff = diff
323+
# best_shape = (h, w)
324+
325+
# return best_shape
326326

327327

328328
def resize_mask_to_unpadded_box(mask_1d, box_on_input_image, box_on_padded_image):

0 commit comments

Comments
 (0)