def preprocess(image, size):
shape = tf.shape(image)
size_t = tf.constant(size, tf.float64)
height = tf.cast(shape[0], tf.float64)
width = tf.cast(shape[1], tf.float64)
cond_op = tf.less(height, width)
# ?????
new_height, new_width = tf.cond(
cond_op,
lambda: (size_t, (width * size_t) / height),
lambda: ((height * size_t) / width, size_t))
resized_image = tf.image.resize_images(
image,
[tf.to_int32(new_height), tf.to_int32(new_width)],
method=tf.image.ResizeMethod.BICUBIC)
cropped = tf.image.resize_image_with_crop_or_pad(resized_image, size, size)
return cropped
评论列表
文章目录