我正在尝试在 google colab 上开发一个 Web 应用程序。我想在这个 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)