【问题标题】:AttributeError: 'NoneType' object has no attribute 'ReadAsArray' when loading an image, what could be the cause?AttributeError: 'NoneType' 对象在加载图像时没有属性 'ReadAsArray',可能是什么原因?
【发布时间】:2020-03-22 08:42:37
【问题描述】:

我正在尝试在 Python 中构建用于图像分类的卷积神经网络。 我在 CoLab 上运行我的代码并将我的数据加载到了 Google Drive。

我可以从 python 中看到我的谷歌驱动器中的所有文件和文件夹,但是当我尝试实际加载图像时,它给了我标题中的错误。

我正在使用skimage.io 包,实际上我只是在运行我在 kaggle 上找到的笔记本,所以代码应该可以正常运行,我注意到的唯一区别是 kaggle 用户可能没有使用他的数据在 CoLab 上工作在 GoogleDrive 中,所以我认为这可能是问题所在,无论如何这是我的代码:

from skimage.io import imread

img=imread('/content/drive/My Drive/CoLab/Data/chest_xray/train/PNEUMONIA/person53_bacteria_255.jpeg')

这给了我以下错误:

AttributeError: 'NoneType' 对象没有属性 'ReadAsArray'

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-12-4a64aebb8504> in <module>()
----> 1 img=imread('/content/drive/My Drive/CoLab/Data/chest_xray/train/PNEUMONIA/person53_bacteria_255.jpeg')

4 frames

/usr/local/lib/python3.6/dist-packages/skimage/io/_io.py in imread(fname, as_gray, plugin, flatten, **plugin_args)
     59 
     60     with file_or_url_context(fname) as fname:
---> 61         img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
     62 
     63     if not hasattr(img, 'ndim'):

/usr/local/lib/python3.6/dist-packages/skimage/io/manage_plugins.py in call_plugin(kind, *args, **kwargs)
    208                                (plugin, kind))
    209 
--> 210     return func(*args, **kwargs)
    211 
    212 

/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py in imread(uri, format, **kwargs)
    221     reader = read(uri, format, "i", **kwargs)
    222     with reader:
--> 223         return reader.get_data(0)
    224 
    225 

/usr/local/lib/python3.6/dist-packages/imageio/core/format.py in get_data(self, index, **kwargs)
    345             self._checkClosed()
    346             self._BaseReaderWriter_last_index = index
--> 347             im, meta = self._get_data(index, **kwargs)
    348             return Array(im, meta)  # Array tests im and meta
    349 

/usr/local/lib/python3.6/dist-packages/imageio/plugins/gdal.py in _get_data(self, index)
     64             if index != 0:
     65                 raise IndexError("Gdal file contains only one dataset")
---> 66             return self._ds.ReadAsArray(), self._get_meta_data(index)
     67 
     68         def _get_meta_data(self, index):

AttributeError: 'NoneType' object has no attribute 'ReadAsArray'

【问题讨论】:

  • 您能否提供错误的完整回溯(所有行),而不仅仅是最后一行?如果很长,可以使用 gist.github.com 或者 pastebin.com 分享一下
  • 您好,非常感谢您的回复,我按照您的要求上传了完整的回溯。
  • 听起来您的路径错误,请验证您的图像路径确实正确。您可以使用类似import osos.path.isfile('/content/drive/My Drive/CoLab/Data/chest_xray/train/PNEUMONIA/person53_bacteria_255.jpeg')
  • 我意识到我可以在我的谷歌驱动器中查看某些文件的名称,但我无法预览它们,而在某些文件中我可以看到预览并且显然我也可以加载它们。因此,即使我猜图像已损坏,尽管我什至可以从 python 中看到它们在 googledrive 上列出。所以最后我猜我的路径是正确的,但我的文件已损坏。谢谢先生回复我。

标签: scikit-image imread


【解决方案1】:

首先应该是My Drive,而不是MyDrive(没有空格)。

如果还是不行,可以试试以下方法:

%cd /content/drive/MyDrive/CoLab/Data/chest_xray/train/PNEUMONIA
img=imread('person53_bacteria_255.jpeg')```

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    相关资源
    最近更新 更多