我尝试在图像数据集中使用to_categorical()进行one-hot编码,但由于FileNotFoundError: [Errno 2] No such file or directory
错误而失败,代码如下所示,
import os
import numpy as np
from PIL import Image
from keras.utils import to_categorical
# Define the classes in the dataset
classes = ['BL_Healthy']
# Initialize the arrays to store the image data and labels
X = []
y = []
# Loop through the dataset and load each image
for class_id, class_name in enumerate(classes):
for image_file in os.listdir('/content/imdadulhaque/' + class_name):
image_path = os.path.join(class_name, image_file)
image = Image.open(image_path)
X.append(np.array(image))
y.append(class_id)
# Convert the labels to one-hot encoded labels
num_classes = len(classes)
y = to_categorical(y, num_classes)
虽然,有图像,但不幸的是,它显示了一个错误的文件找不到.
错误为:
FileNotFoundError Traceback (most recent call last)
<ipython-input-34-9d42022783bc> in <module>
14 for image_file in os.listdir('/content/imdadulhaque/' + class_name):
15 image_path = os.path.join(class_name, image_file)
---> 16 image = Image.open(image_path)
17 X.append(np.array(image))
18 y.append(class_id)
/usr/local/lib/python3.8/dist-packages/PIL/Image.py in open(fp, mode)
2841
2842 if filename:
-> 2843 fp = builtins.open(filename, "rb")
2844 exclusive_fp = True
2845
FileNotFoundError: [Errno 2] No such file or directory: 'BL_Healthy/BL_Healthy_0_451.jpg'
所需的错误截图在附件中提到。请帮助谁有想法。
1条答案
按热度按时间jm2pwxwz1#
您忘记了将
/content/imdadulhaque/
与class_name
和image_file
连接起来作为变量image_path
。image_path = os.path.join('/content/imdadulhaque/', class_name, image_file)