【问题标题】:What does (AttributeError: 'NoneType' object has no attribute '__array_interface__') mean?(AttributeError: 'NoneType' object has no attribute '__array_interface__') 是什么意思?
【发布时间】:2019-09-14 21:43:32
【问题描述】:

我正在尝试制作一个机器学习程序,将细胞图像分类为受感染或未感染。我在一个单独的 python 文件上创建了模型,并正在创建一个脚本,该脚本将在图像上调用它来对其进行分类。完整代码如下:

def convert(img):
    img1 = cv2.imread(img)
    img = Image.fromarray(img1, 'RGB')
    image = img.resize((50, 50))
    return (np.array(image))
def cell_name(label):
    if label == 0:
        return ("Paracitized")
    if label == 1:
        return ("Uninfected")
def predict(file):
    print ("Predicting...please wait")
    ar = convert(file)
    ar = ar/255
    label = 1
    a = []
    ar.append(ar)
    a = np.array(a)
    score = loaded_model.predict(a, verbose=1)
    print (score)
    label_index=np.argmax(score)
    print(label_index)
    acc=np.max(score)
    Cell=cell_name(label_index)
    return (Cell,"The predicted Cell is a "+Cell+" with accuracy =    "+str(acc))

print(predict("test/paracitized.png"))

这是完整的错误信息:

File "malaria_img.py", line 15, in convert
    img = Image.fromarray(img1, 'RGB')
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2526, in fromarray
    arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

有人知道为什么会这样吗?

【问题讨论】:

  • 顺便说一句,Python 2.7 非常过时了,到今年年底将不再支持。您应该尽快更新到 3.x。
  • 我在我的系统中尝试了转换函数,它执行得很好。如@KarlKnechtel 所述,您的文件名可能有问题

标签: python arrays numpy model nonetype


【解决方案1】:
arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

AttributeError 表示有一个Error 与Attribute 请求有关。一般来说,当你写x.y时,y是x的声称attribute。

'NoneType' object 表示以NoneType 为类型的对象。确实有一个这样的对象,称为None(您应该熟悉它)。确实,has no attribute 被命名为 __array_interface__。

问题是我们从obj 请求这个属性,但是obj 的值是None。它应该是某种类似数组的对象。这不在我们自己的代码中;相反,obj 是 PIL 对 img1 的内部名称,我们将其传递给 fromarray 调用。

我们自己代码中的img1 来自上一行img1 = cv2.imread(img)。 documentation 告诉我们(有点 - 因为它试图同时记录 C++ 和 Python 版本;你必须在这里填写一些空白)CV2 在 the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) 时返回 None。

仔细检查您的文件名(如果文件名来自处理字符串,请检查意外空格,并仔细检查文件名的扩展名);仔细检查文件 path (如果你给出了相对路径,请仔细检查工作目录是什么——你可能会感到惊讶!);并仔细检查文件本身(Python进程是否有权打开它?图像是否损坏?)。

【讨论】:

  • 虽然这次的解决方案很简单,但希望以后给大家一些调试方面的想法:)
猜你喜欢
  • 2021-03-13
  • 2019-12-15
  • 1970-01-01
  • 2022-12-15
  • 2017-10-20
  • 2023-01-16
  • 2022-01-10
相关资源
最近更新 更多