gpt4 book ai didi

python - 什么是(属性错误 : 'NoneType' object has no attribute '__array_interface__' ) mean?

转载 作者:行者123 更新时间:2023-12-03 08:52:43 26 4
gpt4 key购买 nike

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

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

最佳答案

arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

AttributeError 表示存在与 Attribute 请求有关的 Error。一般来说,当您编写 x.y 时,y 是 x 的所谓属性

'NoneType' object 表示类型为 NoneType 的对象。确实有一个这样的对象,称为 None (您应该熟悉它)。事实上,它没有名为__array_interface__的属性。

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

我们自己的代码中的 img1 来自上一行 img1 = cv2.imread(img)documentation告诉我们(有点 - 因为它试图同时记录 C++ 和 Python 版本;你必须在这里填写一些空白)当 图像无法读取时,CV2 返回 None (因为文件丢失、权限不正确、格式不受支持或无效)

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

关于python - 什么是(属性错误 : 'NoneType' object has no attribute '__array_interface__' ) mean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com