I have a text file that contains the paths to jpeg images I want to import into my script. I am using an example code provided by a Udemy course: "Deep Learning with Python - Novice to Pro!" that detects smiles in images. The function I am having trouble with is converting images into matrixes/two-dimensional arrays:
def img2array(f, detection=False, ii_size=(64, 64)):
"""
Convert images into matrixes/two-dimensional arrays.
detection - if True we will resize an image to fit the
shape of a data that our first convolutional
layer is accepting which is 32x32 array,
used only on detection.
ii_size - this is the size that our input images have.
"""
rf=None
if detection:
rf=f.rsplit('.')
rf=rf[0]+'-resampled.'+rf[1]
im = Image.open(f)
# Create a smaller scalled down thumbnail
# of our image.
im.thumbnail(ii_size)
# Our thumbnail might not be of a perfect
# dimensions, so we need to create a new
# image and paste the thumbnail in.
newi = Image.new('L', ii_size)
newi.paste(im, (0,0))
newi.save(rf, "JPEG")
f=rf
# Turn images into an array.
data=imread(f, as_gray=True)
# Downsample it from 64x64 to 32x32
# (that's what we need to feed into our first convolutional layer).
data=block_reduce(data, block_size=(2, 2), func=np.mean)
if rf:
remove(rf)
return data
The function is called in another script:
img_data=prep_array(img2array(filename, detection=True), detection=True)
I am not sure what to name 'filename' in order for this code to run correctly. When I give it the text file path I get an error that says:
UnidentifiedImageError: cannot identify image file 'filepath\imagelist.txt
I am brand new to Python, and I need help importing the correct 'filename' variable to make this function work.