I have many images which rotate 90 degrees when I use Tensorflow tf.image.decode_jpeg() to read in images in my data pipeline. When I look at those image on my computer they look correctly oriented and when I read the image with cv2 they also looks correctly oriented. However, when I read them in with Tensorflow tf.image.decode_jpeg(), they end up rotated 90 degrees. Anybody know why or how I can fix this so they read in the correct orientation (it doesn't happen to all images but many)?
To show what I mean, here's what a sample image looks like on my computer or when I read it with open-cv.
However, when I read it with Tensorflow (tensorflow 2.4.0) in my pipeline, many images rotate 90 degrees and, for example, the same image ends up looking like this.
To read and see an image with cv2 I do
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
%matplotlib inline
plt.imshow(cv2.imread(image_file_path)[:, :, ::-1]))
To read and see many rotated image with Tensorflow
def decode_images(dataset_file_paths):
'''
tf_read_converted_file_path = tf.io.read_file(dataset_file_paths)
return tf.image.decode_jpeg(tf_read_converted_file_path)
def check_processed_images(files_path_dataset, processing_function):
'''Plots images to see what the processing looks like when it is mapped in from a dataset.
This can be used to, for example, look see how the augmentation of images look.
Arguments:
files_path_dataset: TF TensorSliceDataset of filenames.
processing_function: function which is used to map each element in the dataset.
Return:
plot of images'''
augmentation_dataset = files_path_dataset.map(processing_function).shuffle(50).batch(16)
plt.figure(figsize=(13, 13))
for images, labels in augmentation_dataset.take(1):
for i in range(16):
ax = plt.subplot(4, 4, i + 1)
plt.imshow(images[i].numpy())
####################
####################
# Look at a sample of images
# Take a directory of jpg images and create a TensorFlow dataset of filepaths
dirPath = type_in_a_directory_here
fileNames = tf.io.gfile.glob(f'{dirPath}/**/*.jpg')
fileNames = tf.random.shuffle(fileNames)
filenamesDs = tf.data.Dataset.from_tensor_slices(fileNames)
check_processed_images(filenamesDs, decode_images)
The code above will produce a grid of images so you can see. For example, all these images on my computer have the fishing lure so that they are horizontal but when I use TensorFlow to process my images many are rotated 90 degrees for some reason.
Am I doing something wrong that Tensorflow is rotating many of my images?