How to resolve the error in file_uploader in streamlit?

Summary

I am getting this error when trying to predict the image from loading it from streamlit file_uploader. The process is working fine when I try directly loading the image in ide. But the problem is arising with streamlit file_uploader. I can’t figure in which file type the streamlit is uploading the file.

Steps to reproduce

Code snippet:

from tensorflow.keras.models import load_model
model = load_model('../streamlit/pages/sickmushroom_classifier.h5')

IMAGE_SIZE = (150,150)

class_names = ['oyster_blue','oyster_brown',
               'pleurotus_blue','pleurotus_brown','pleurotus_white',
               'portobello_blue', 'portobello_brown', 'portobello_white',
               'shiitake_blue','shiitake_brown','shiitake_white', 
               'winter_black', 'winter_blue', 'winter_white']

class_labels = {class_name:i for i, class_name in enumerate(class_names)}
print(class_labels)

number_classes = len(class_names)


def load_and_prep_image(filename, img_shape=150, scale=True):

    imge = tf.io.read_file(filename)
    imge = tf.io.decode_image(imge, channels=3)
    imge = tf.image.resize(imge, list(IMAGE_SIZE))
    if scale: 
        return imge/255.
    else:
        return imge 

upload= st.file_uploader('Insert image for classification', type=['png','jpg', 'jpeg'])
c1, c2= st.columns(2)
if upload is not None:
  im= Image.open(upload)
  img= np.asarray(im)
  image= cv2.resize(img, (150, 150))
  img= preprocess_input(image)
  img= np.expand_dims(img, 0)
  c1.header('Input Image')
  c1.image(im)
  c1.write(img.shape)

#load weights of the trained model.
  img2 = load_and_prep_image(upload, scale=False)
  img_expanded = tf.expand_dims(img2, axis=0)
  pred_prob = model.predict(img_expanded)
  pred_class = class_names[pred_prob.argmax()]
  c2.header('Output')
  c2.subheader('Predicted class :')
  c2.write(pred_class)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Explain what you expect to happen when you run the code above.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: (get it with $ streamlit version)
    version 1.17.0
  • Python version: (get it with $ python --version)
    Python 3.9.12
  • OS version:
    WIN11
    Using VS code

Link to your GitHub repo: GitHub - CH01-YE/Mushroom-Tycoon
There is a sikkmushroom_classifier.h5 in it.

Thank you for reading it. I hope this problem is solved.

What type of file are you uploading when you see this error?