TypeError: cannot unpack non-iterable NoneType object during image dataset loading

125 Views Asked by At

I am trying to load an image dataset in Google Colab using the os.listdir() function but I am getting this error "TypeError: cannot unpack non-iterable NoneType object". Can you please help me?

I couldn't find any valid reason behind this

def load_data(dir_list, image_size):
    """
    Read images, resize and normalize them. 
    Arguments:
        dir_list: list of strings representing file directories.
    Returns:
        X: A numpy array with shape = (#_examples, image_width, image_height, #_channels)
        y: A numpy array with shape = (#_examples, 1)
    """

    # load all images in a directory
    X = []
    y = []
    image_width, image_height = image_size
    
    for directory in dir_list:
        for filename in listdir(directory):
            # load the image
            image = cv2.imread(directory + '\\' + filename)
            # crop the brain and ignore the unnecessary rest part of the image
            image = crop_brain_contour(image, plot=False)
            # resize image
            image = cv2.resize(image, dsize=(image_width, image_height), interpolation=cv2.INTER_CUBIC)
            # normalize values
            image = image / 255.
            # convert image to numpy array and append it to X
            X.append(image)
            # append a value of 1 to the target array if the image
            # is in the folder named 'yes', otherwise append 0.
            if directory[-3:] == 'yes':
                y.append([1])
            else:
                y.append([0])
                
    X = np.array(X)
    y = np.array(y)
    
    # Shuffle the data
    X, y = shuffle(X, y)
    
    print(f'Number of examples is: {len(X)}')
    print(f'X shape is: {X.shape}')
    print(f'y shape is: {y.shape}')
    
    return X, y
augmented_path = 'augmented data/'

augmented_yes = augmented_path + 'yes' 
augmented_no = augmented_path + 'no'

IMG_WIDTH, IMG_HEIGHT = (240, 240)

X, y = load_data([augmented_yes, augmented_no], (IMG_WIDTH, IMG_HEIGHT))

and the error message is

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-69-8f592084b88a> in <cell line: 8>()
      6 IMG_WIDTH, IMG_HEIGHT = (240, 240)
      7 
----> 8 X, y = load_data([augmented_yes, augmented_no], (IMG_WIDTH, IMG_HEIGHT))

TypeError: cannot unpack non-iterable NoneType object
0

There are 0 best solutions below