FileNotFoundError: [Errno 2] No such file or directory for Image Dataset one-hot encoded

huangapple go评论67阅读模式
英文:

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 = [&#39;BL_Healthy&#39;]

# 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(&#39;/content/imdadulhaque/&#39; + 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)
&lt;ipython-input-34-9d42022783bc&gt; in &lt;module&gt;
     14     for image_file in os.listdir(&#39;/content/imdadulhaque/&#39; + class_name):
     15         image_path = os.path.join(class_name, image_file)
---&gt; 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:
-&gt; 2843         fp = builtins.open(filename, &quot;rb&quot;)
   2844         exclusive_fp = True
   2845 

FileNotFoundError: [Errno 2] No such file or directory: &#39;BL_Healthy/BL_Healthy_0_451.jpg&#39;

The desired error screenshot is mentioned in the attached file. Please help who have ideas.

FileNotFoundError: [Errno 2] No such file or directory for Image Dataset one-hot encoded

答案1

得分: 1

你忘记将/content/imdadulhaque/class_nameimage_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(&#39;/content/imdadulhaque/&#39;, class_name, image_file)

huangapple
  • 本文由 发表于 2023年2月18日 17:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定