class PreprocessResizeKeepRatio(object):
def __init__(self, width, height):
self.width = width
self.height = height
def do(self, cv2_img):
max_width = self.width
max_height = self.height
cur_height, cur_width = cv2_img.shape[:2]
ratio_w = float(max_width)/float(cur_width)
ratio_h = float(max_height)/float(cur_height)
ratio = min(ratio_w, ratio_h)
new_size = (min(int(cur_width*ratio), max_width),
min(int(cur_height*ratio), max_height))
new_size = (max(new_size[0], 1),
max(new_size[1], 1),)
resized_img = cv2.resize(cv2_img, new_size)
return resized_img