图像预处理如何快速便捷的实现keep ratio操作
def get_input(img, shape_hw, keep_raito=True):
import mmcv
if not isinstance(img, np.ndarray):
img = mmcv.imread(img)
assert shape_hw[0] % 32 == 0 and shape_hw[1] % 32 == 0
if keep_raito:
img, scale_factor = mmcv.imrescale(img, shape_hw, return_scale=True)
img = mmcv.impad(img, shape=shape_hw, pad_val=0)
else:
img, w_scale, h_scale = mmcv.imresize(img, shape_hw, return_scale=True)
scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
return img, scale_factor