[英]How can I solve "TypeError: expected str, bytes or os.PathLike object, not list"
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.有001、002、003、004、......、091、092形式的文件夹,每个文件夹有7个训练数据。 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
TypeError:预期的 str、字节或 os.PathLike 对象,而不是列表
You are providing a list of paths to the open_images
function, but it is not coded to support that.您正在提供
open_images
函数的路径列表,但未对其进行编码以支持该路径。 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)
You are passing a list of images to your function open_images
, but this function is written to only open one image, not a list.您正在将图像列表传递给函数
open_images
,但此函数仅用于打开一个图像,而不是列表。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.