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