Reputation: 75
i am trying to read multiple images from a folder in a single numpy array to feed them to my deep learning model. when i print the shape i get the error message that none type object has no shape. which means that the images were not read by opencv(for jpg)/tifffile(for .tif)My code is as follows
import numpy as np
import cv2 as cv
import tifffile as tiff
filepath1 = "D:/Programs/transnet/dataset/train"
filepath2 = "D:/Programs/transnet/dataset/train_label"
img = np.ndarray((cv.imread(filepath1 + "*.jpg")))
clt = np.ndarray((tiff.imread(filepath2 + "*.tif")))
print(img.shape)
print(clt.shape)
I have already tried glob.glob but it doesn't work. i am expecting a 4 dimensional array of number of rgb images
Upvotes: 1
Views: 4107
Reputation: 20490
You can use os module to do file traversal operations, especially os.listdir
and os.path.join
as follows
import os
def get_all_images(folder, ext):
all_files = []
#Iterate through all files in folder
for file in os.listdir(folder):
#Get the file extension
_, file_ext = os.path.splitext(file)
#If file is of given extension, get it's full path and append to list
if ext in file_ext:
full_file_path = os.path.join(folder, file)
all_files.append(full_file_path)
#Get list of all files
return all_files
filepath1 = "D:/Programs/transnet/dataset/train"
filepath2 = "D:/Programs/transnet/dataset/train_label"
#List of all jps and tif files
jpg_files = get_all_images(filepath1, 'jpg')
tif_files = get_all_images(filepath2, 'tif')
Now once you have the list of all files, you can iterate through the list and open the images.
Upvotes: 1