英文:
FileNotFoundError: [Errno 2] No such file or directory for Image Dataset one-hot encoded
问题
我尝试使用to_categorical()
对我的图像数据集进行了一热编码,但由于出现了FileNotFoundError: [Errno 2] No such file or directory
错误,下面是代码:
import os
import numpy as np
from PIL import Image
from keras.utils import to_categorical
# 定义数据集中的类别
classes = ['BL_Healthy']
# 初始化用于存储图像数据和标签的数组
X = []
y = []
# 遍历数据集并加载每个图像
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)
# 将标签转换为一热编码标签
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'
附带的文件中包含了所需的错误截图。希望能帮助有相关经验的人解决这个问题。
英文:
I tried to one-hot encoded using to_categorical() in my image dataset but I failed because of FileNotFoundError: [Errno 2] No such file or directory
error and the code is given below,
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)
Although, there are images but unfortunately, it showed an error the file is not found.
> Error is:
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'
The desired error screenshot is mentioned in the attached file. Please help who have ideas.
答案1
得分: 1
你忘记将/content/imdadulhaque/
与class_name
和image_file
连接到你的变量image_path
中,可以这样做:
image_path = os.path.join('/content/imdadulhaque/', class_name, image_file)
英文:
You forgot to join /content/imdadulhaque/
with class_name
and image_file
for your variable image_path
Do something like:
image_path = os.path.join('/content/imdadulhaque/', class_name, image_file)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论