我正在尝试在 google colab 上开发一个网络应用程序。我想在这个 Web 应用程序中使用我之前训练过的模型制作一个图像分类器。当我在 Web 应用程序中从浏览器中选择要分类的图像时,出现以下错误:
TypeError: 'Image' object is not subscriptable.
我的代码块:
file = st.file_uploader("Please upload an image(png) file", type=["png"])
def import_and_predict(_image_data, model):
size = (299,299)
_image = ImageOps.fit(_image_data , size , Image.ANTIALIAS)
img = np.asarray(_image)
img_reshape = _image[np.newaxis,...]
prediction = model.predict(img_reshape)
# image = image.convert('RGB')
# st.image(image, channels='RGB')
return prediction
if file is None:
st.text("Please upload an image file !")
else:
_image = Image.open(file)
st.image(_image , use_column_width=True)
prediction = import_and_predict(_image, model)
class_names=['Cat','Dog']
string="predict:" +class_names[np.argmax(predictions)]
st.success(string)
当您应该在图像数组上执行重塑操作时,您正在尝试对原始 Image 对象执行重塑操作。更改这一行:
img_reshape = _image[np.newaxis,...]
to:
img_reshape = img[np.newaxis,...]
你应该表现得很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)