I'm trying to develop a web application on google colab. I want to make a image classifier using my model, which I previously trained, in this web application. When I select an image to be classified from browser in the web application, I get the following error:
TypeError: 'Image' object is not subscriptable.
my code block:
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)
You're trying to do the reshape operation on the original Image object, when you should be doing it on the image array. Change this line:
img_reshape = _image[np.newaxis,...]
to:
img_reshape = img[np.newaxis,...]
and you should be good.