Last active
October 10, 2022 14:03
-
-
Save sukhitashvili/79d02a039c170f0bc623a6009e2a9ca0 to your computer and use it in GitHub Desktop.
Resize bbox to new image size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = np.clip(bounding_boxes, a_min=0, a_max=None) | |
| bounding_boxes = bounding_boxes.astype(np.uint32).tolist() | |
| return bounding_boxes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment