I try to setup an images pipeline that builds an images dataset for Tensorflow that crops the images. I followed this tutorial but I want to crop the file to a square and not resize it without preserving the aspect ratio. I can't figure out how to get their dimensions.
#
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
#
import glob
AUTOTUNE = tf.data.experimental.AUTOTUNE
IMAGE_SIZE = 192
def preprocess_image(path):
img_raw = tf.io.read_file(path)
img_tensor = tf.image.decode_jpeg(img_raw, channels=3)
print("img_tensor")
print(img_tensor)
height = img_tensor.shape[0]
print("height")
print(height)
return img_tensor
files_path = glob.glob('./images/*.jpeg')
image_count = len(files_path)
path_ds = tf.data.Dataset.from_tensor_slices(files_path)
path_ds.map(preprocess_image, num_parallel_calls=AUTOTUNE)
The tensor shape returned by tf.image.decode_jpeg is :
Tensor("DecodeJpeg:0", shape=(None, None, 3), dtype=uint8)
How can I access the size of the jpg image ?
When I access it this way it works :
#
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
#
image = tf.io.read_file('./images/4c34476047bcbbfd10b1fd3342605659.jpeg/')
image = tf.image.decode_jpeg(image, channels=3)
print("image.shape")
print(image.shape)
It prints :
image.shape
(700, 498, 3)