Skip to content

Instantly share code, notes, and snippets.

@sukhitashvili
Last active October 10, 2022 14:03
Show Gist options
  • Save sukhitashvili/79d02a039c170f0bc623a6009e2a9ca0 to your computer and use it in GitHub Desktop.
Save sukhitashvili/79d02a039c170f0bc623a6009e2a9ca0 to your computer and use it in GitHub Desktop.

Revisions

  1. sukhitashvili revised this gist Oct 10, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions resize_bbox.py
    Original file line number Diff line number Diff line change
    @@ -19,5 +19,6 @@ def rescale_bounding_boxes(original_shape: tuple, new_shape: tuple,
    bounding_boxes[:, 1] *= scale_h
    bounding_boxes[:, 2] *= scale_w
    bounding_boxes[:, 3] *= scale_h
    bounding_boxes = np.clip(bounding_boxes, a_min=0, a_max=None)
    bounding_boxes = bounding_boxes.astype(np.uint32).tolist()
    return bounding_boxes
  2. sukhitashvili created this gist Jun 13, 2022.
    23 changes: 23 additions & 0 deletions resize_bbox.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import numpy as np


    def rescale_bounding_boxes(original_shape: tuple, new_shape: tuple,
    bounding_boxes: list) -> list:
    """
    Rescales bounding box coords according to new image size
    :param original_shape: (W, H)
    :param new_shape: (W, H)
    :param bounding_boxes: [[x1, y1, x2, y2], ...]
    :return: scaled bbox coords
    """
    original_w, original_h = original_shape
    new_w, new_h = new_shape
    bounding_boxes = np.array(bounding_boxes, dtype=np.float64)
    scale_h, scale_w = new_h / original_h, new_w / original_w
    bounding_boxes[:, 0] *= scale_w
    bounding_boxes[:, 1] *= scale_h
    bounding_boxes[:, 2] *= scale_w
    bounding_boxes[:, 3] *= scale_h
    bounding_boxes = bounding_boxes.astype(np.uint32).tolist()
    return bounding_boxes