numpy (AttributeError:“NoneType”对象没有属性“__array_interface__”)是什么意思?

trnvg8h3  于 2023-05-13  发布在  其他
关注(0)|答案(2)|浏览(323)

我正在尝试制作一个机器学习程序,它可以将细胞的图像分类为感染或未感染。我在一个单独的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__'

有人知道为什么会发生这种情况吗?

qmb5sa22

qmb5sa221#

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是我们传递给fromarray调用的img1的PIL内部名称。
我们自己的代码中的img1来自上一行img1 = cv2.imread(img)。文档告诉我们(某种程度上-因为它试图同时记录C++和Python版本;你必须在这里填写一些空白),CV 2在the image cannot be read (because of missing file, improper permissions, unsupported or invalid format)时返回None
仔细检查你的文件名(如果文件名来自处理字符串,检查是否有意外的空格,并仔细检查文件名的扩展名);仔细检查文件 path(如果你给出了一个相对路径,仔细检查工作目录是什么-你可能会感到惊讶!);并仔细检查文件本身(Python进程是否有权限打开它?图像是否损坏?).

p4tfgftt

p4tfgftt2#

问题出在你的转换函数上。你尝试将图像转换为数组,然后在代码中尝试将其再次转换为数组。删除其中一个,然后再次测试代码。我修改了你的函数:

def convert(img):
   img1 = cv2.imread(img)
   img = Image.fromarray(img1, 'RGB')
   image = img.resize((50, 50))
   return image

相关问题