def crop_image(x, target_height=227, target_width=227, as_float=True):
image = skimage.io.imread(x)
if as_float:
image = skimage.img_as_float(image).astype(np.float32)
print(len(image.shape))
if len(image.shape) == 2:
image = np.tile(image[:, :, None], 3)
elif len(image.shape) == 4:
image = image[:, :, :, 0]
height, width, rgb = image.shape
if width == height:
resized_image = np.resize(image, (target_height,target_width))
elif height < width:
resized_image = np.resize(image, (int(width * float(target_height)/height), target_width))
cropping_length = int((resized_image.shape[1] - target_height) / 2)
resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]
else:
resized_image = np.resize(image, (target_height, int(height * float(target_width) / width)))
cropping_length = int((resized_image.shape[0] - target_width) / 2)
resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]
return np.resize(resized_image, (target_height, target_width))
cnn_util.py 文件源码
python
阅读 28
收藏 0
点赞 0
评论 0
评论列表
文章目录