我正在尝试制作一个机器学习程序,它可以将细胞的图像分类为感染或未感染。我在一个单独的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__'
有人知道为什么会发生这种情况吗?
2条答案
按热度按时间qmb5sa221#
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进程是否有权限打开它?图像是否损坏?).
p4tfgftt2#
问题出在你的转换函数上。你尝试将图像转换为数组,然后在代码中尝试将其再次转换为数组。删除其中一个,然后再次测试代码。我修改了你的函数: