I just don't understand what python is doing here:
I want to provide the math of a directory that contains images and store them directly as numpy array in a list. However, when I execute the following code everything works fine because I first extract the filename and then perform imread to create the numpy array:
def get_data(image_dir, annotations_dir):
image_filenames = [ os.path.join(image_dir,i) for i in os.listdir(image_dir)]
return image_filenames, image_filenames
IMAGE_DIR = os.path.join(os.getcwd(), "sample_data/images")
ANNOTATIONS_DIR = os.path.join(os.getcwd(), "sample_data/annotations")
print("IMAGE_DIR: {}\nANNOTATIONS_DIR: {}".format(IMAGE_DIR, ANNOTATIONS_DIR))
pet_images, pet_annotations = get_data(IMAGE_DIR, ANNOTATIONS_DIR)
print(len(pet_images))
my_img = plt.imread(pet_images[0])
plt.imshow(my_img)
Result:
IMAGE_DIR: /content/sample_data/images
ANNOTATIONS_DIR: /content/sample_data/annotations
7393
<matplotlib.image.AxesImage at 0x7feee24fdeb8>
(Image is shown perfectly fine)
BUT when I try to do create the numpy array inside directly it does not work. Here the following code:
def get_data(image_dir, annotations_dir):
image_filenames = [ plt.imread((os.path.join(image_dir,i)) for i in os.listdir(image_dir)]
return image_filenames, image_filenames
IMAGE_DIR = os.path.join(os.getcwd(), "sample_data/images")
ANNOTATIONS_DIR = os.path.join(os.getcwd(), "sample_data/annotations")
print("IMAGE_DIR: {}\nANNOTATIONS_DIR: {}".format(IMAGE_DIR, ANNOTATIONS_DIR))
pet_images, pet_annotations = get_data(IMAGE_DIR, ANNOTATIONS_DIR)
plt.imshow(pet_images[0])
I get:
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file '/content/sample_data/images/Abyssinian_100.mat'
Could someone please explain what is going on and why I cannot create the numpy array using imread inside the list comprehension directly
Best regards
I found out what the problem was: The reason the code crashes is that the dataset contains a filename.mat file which is not supported my imread.