Last active
November 17, 2022 15:50
-
-
Save shamilnabiyev/7fe0ba208da3add3f86dc54e0990e1b2 to your computer and use it in GitHub Desktop.
Resize an image and bounding boxes
This file contains 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
# Source: https://sheldonsebastian94.medium.com/resizing-image-and-bounding-boxes-for-object-detection-7b9d9463125a | |
import albumentations | |
from PIL import Image | |
import numpy as np | |
sample_img = Image.open("data/img1.jpg") | |
sample_arr = np.asarray(sample_img) | |
def resize_image(img_arr, bboxes, h, w): | |
""" | |
:param img_arr: original image as a numpy array | |
:param bboxes: bboxes as numpy array where each row is 'x_min', 'y_min', 'x_max', 'y_max', "class_id" | |
:param h: resized height dimension of image | |
:param w: resized weight dimension of image | |
:return: dictionary containing {image:transformed, bboxes:['x_min', 'y_min', 'x_max', 'y_max', "class_id"]} | |
""" | |
# create resize transform pipeline | |
transform = albumentations.Compose( | |
[albumentations.Resize(height=h, width=w, always_apply=True)], | |
bbox_params=albumentations.BboxParams(format='pascal_voc')) | |
transformed = transform(image=img_arr, bboxes=bboxes) | |
return transformed | |
transformed_dict = resize_image(sample_arr, bboxes_og, 224, 224) | |
# contains the image as array | |
transformed_arr = transformed_dict["image"] | |
# contains the resized bounding boxes | |
transformed_info = np.array(list(map(list, transformed_dict["bboxes"]))).astype(float) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment