|
1 | 1 | import os |
2 | | -import sys |
3 | 2 | import cv2 |
4 | | -import time |
5 | 3 | import queue |
6 | 4 | import shlex |
7 | 5 | import logging |
|
15 | 13 | from typing import Dict, List, Optional, Tuple |
16 | 14 |
|
17 | 15 | 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 |
19 | 17 |
|
20 | 18 | logger = logging.getLogger(__name__) |
21 | 19 |
|
@@ -257,78 +255,78 @@ def get_labels(labels_path: str) -> list: |
257 | 255 | return class_names |
258 | 256 |
|
259 | 257 |
|
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 |
332 | 330 |
|
333 | 331 |
|
334 | 332 | def convert_box_from_normalized(normalized_box: list, |
|
0 commit comments