Home > Net >  How can I solve "TypeError: expected str, bytes or os.PathLike object, not list"
How can I solve "TypeError: expected str, bytes or os.PathLike object, not list"

Time:05-18

I'm trying to do my person detection project from palm print.

There are folders in the form of 001, 002, 003, 004, ......, 091, 092, with 7 training data in each folder. I want to take all the data one by one and train them.

Example file path:

'Dataset/TrainWithROI/001/001-Train1.JPG', 
'Dataset/TrainWithROI/001/001-Train2.JPG', 
'Dataset/TrainWithROI/001/001-Train3.JPG', 
'Dataset/TrainWithROI/001/001-Train4.JPG', 
'Dataset/TrainWithROI/001/001-Train5.JPG', 
'Dataset/TrainWithROI/001/001-Train6.JPG', 
'Dataset/TrainWithROI/001/001-Train7.JPG',

But before I start training the model I get an error like this.

def open_images(path):
    image = load_img(path, color_mode = 'rgb')
    image = np.array(image)/255.0
    return image

def get_labels(paths):

    label = []
    for path in paths:
        path = path.split('/')[-2]
        label.append(labels.index(path))
    return label

def data_gen(data_paths, batch_size=1):
    img=[]
    lab=[]
    for i in range(0, len(data_paths), batch_size):
        paths = data_paths[i:i batch_size]
        images = open_images(paths)
        img.append(open_images(paths).reshape(224, 224, 3))
        labels = get_labels(paths)
        lab.append(get_labels(paths))
        
        #yield images,np.array(labels)
    return np.array(img) , np.array(lab)

Model:

X_train, y_train = data_gen(train_paths)
X_test, y_test = data_gen(test_paths)

Error:

TypeError: expected str, bytes or os.PathLike object, not list

CodePudding user response:

You are providing a list of paths to the open_images function, but it is not coded to support that. You can modify this function to handle that, try this code:

def open_images(path):
    images = []
    for path in paths:
        image = load_img(path, color_mode = 'rgb')
        image = np.array(image)/255.0
        images.append(image)
    return np.array(images)

CodePudding user response:

You pass a list of images to your function open_images, but this function is written to only open one image, not a list. Try this:

def data_gen(data_paths, batch_size=1):
    img=[]
    lab=[]
    for i in range(0, len(data_paths), batch_size):
        paths = data_paths[i:i batch_size]
        for x in paths:
            images = open_images(x)
            img.append(open_images(x).reshape(224, 224, 3))
            labels = get_labels(x)
            lab.append(get_labels(x))
  • Related