I wanted to convert image to tensors which I'm not able to do

Question:

Here is the code:

from os import listdir
from os.path import isfile, join

vvy_imgs = []
#images to tensors
for img in np.array([f for f in listdir('E:/vachan') if isfile(join('E:/vachan', f))]): # filepaths 
    print(img) # shows it's of jpg extension
    tensor = tf.io.decode_jpeg(img, channels=3)
    vvy_imgs = np.append(vvy_imgs, tensor)

but it is showing error

InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [Op:DecodeJpeg]

though all the images are of ‘.jpg’ extension.

Asked By: VVY

||

Answers:

Seems like img is a path to an image and not an image.
Maybe try:

image = tf.io.read_file(img)
tensor= tf.io.decode_jpeg(image)
...
Answered By: AloneTogether
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.