in sagemaker/src/utils.py [0:0]
def resize_image(image, desired_size, allignment="centre"):
''' Helper function to resize an image while keeping the aspect ratio.
Parameter
---------
image: np.array
The image to be resized.
desired_size: (int, int)
The (height, width) of the resized image
Return
------
image: np.array
The image of size = desired_size
border_bb: (int, int, int, int)
(x, y, w, h) in percentages of the borders added into the image to keep the aspect ratio
'''
original_image_size = image.shape[:2]
size = image.shape[:2]
if size[0] > desired_size[0] or size[1] > desired_size[1]:
ratio_w = float(desired_size[0]) / size[0]
ratio_h = float(desired_size[1]) / size[1]
ratio = min(ratio_w, ratio_h)
new_size = tuple([int(x * ratio) for x in size])
image = cv2.resize(image, (new_size[1], new_size[0]))
size = image.shape
delta_w = max(0, desired_size[1] - size[1])
delta_h = max(0, desired_size[0] - size[0])
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
if allignment == "centre":
left, right = delta_w // 2, delta_w - (delta_w // 2)
if allignment == "left":
left = 5
right = delta_w - 5
color = image[0][0]
if color < 230:
color = 230
bordered_image = cv2.copyMakeBorder(image, top, bottom, left, right,
cv2.BORDER_CONSTANT, value=float(color))
border_bb = [left, top,
bordered_image.shape[1] - right - left,
bordered_image.shape[0] - top - bottom]
border_bb = [border_bb[0] / bordered_image.shape[1],
border_bb[1] / bordered_image.shape[0],
border_bb[2] / bordered_image.shape[1],
border_bb[3] / bordered_image.shape[0]]
bordered_image[bordered_image > 230] = 255
return bordered_image, border_bb