Model Deploy TypeError: expected str, bytes or os.PathLike object, not UploadedFile

I’m trying to upload an image for my binary classification model,
but constantly getting this error.
I’ve used the other solutions mentioned in the forum, but still its not resolved.
Please help.

Here’s the code snippet:-

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
import streamlit as st
from PIL import Image

st.title("Covid19 Detection")

def prediction(model):
    image_file = st.file_uploader("Choose an image...", type="jpg")
    if (image_file is not None):

        img = load_img(image_file, target_size=(256,256), color_mode = "grayscale")
        img = img_to_array(img)
        img = Image.open(img)
        st.image(img,width=40, caption='Uploaded Image', use_column_width=True)
        st.write("")
        st.write("Detecting......")
        img = img/255
        classes = model.predict(img.reshape(1,256,256,1))
        if classes[0][0] == 1:
            prediction = 'Not Detected'
        else:
            prediction = 'Covid Detected!'
        print(prediction)
    return

def loading_model():
    model = load_model('Covid_model.hdf5')
    print('Model Loaded')
    return model

model = loading_model()

prediction(model)

Even if I’m reading it using BytesIO as suggested in the community forum then still it’s giving the error -

def prediction(model):
    image_file = st.file_uploader("Choose an image...", type="jpg")
    if (image_file is not None):
        image_file = BytesIO(image_file.getvalue().decode("utf-8"))
        img = load_img(image_file, target_size=(256,256), color_mode = "grayscale")
        img = img_to_array(img)
        img = Image.open(img)
        st.image(img,width=40, caption='Uploaded Image', use_column_width=True)
        st.write("")
        st.write("Detecting......")
        img = img/255
        classes = model.predict(img.reshape(1,256,256,1))
        if classes[0][0] == 1:
            prediction = 'Not Detected'
        else:
            prediction = 'Covid Detected!'
        print(prediction)
    return

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Image uploading :-

import streamlit as st 
from PIL import Image
import classify 
import numpy as np

sign_names = {
        0: 'Speed limit (20km/h)',
        1: 'Speed limit (30km/h)',
        2: 'Speed limit (50km/h)',
        3: 'Speed limit (60km/h)' 
}
st.title("Traffic Sign Classifier")

uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:

        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image', use_column_width=True)

        st.write("")

        if st.button('predict'):
                st.write("Result...")
                label = classify.predict(uploaded_file)
                label = label.item()

                res = sign_names.get(label)
                st.markdown(res)

Model Prediction

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
import streamlit as st

@st.cache(allow_output_mutation=True)
def get_model():
        model = load_model('Model/Traffic_Sign_Classifier_CNN.hdf5')
        print('Model Loaded')
        return model 

        
def predict(image):
        loaded_model = get_model()
        image = load_img(image, target_size=(32, 32), color_mode = "grayscale")
        image = img_to_array(image)
        image = image/255.0
        image = np.reshape(image,[1,32,32,1])

        classes = loaded_model.predict_classes(image)

        return classes

Compare your code with these two files and you’re error will be resolved. Try to separate your image uploading and model prediction file.

1 Like

Still getting the same error -
TypeError: expected str, bytes or os.PathLike object, not UploadedFile

Here’s the code you suggested to try -

  1. model
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
import streamlit as st

@st.cache(allow_output_mutation=True)
def get_model():
    model = load_model('Covid_model.hdf5')
    print('Model Loaded')
    return model

def predict(image):
    loaded_model = get_model()
    image = load_img(image, target_size=(256, 256), color_mode = "grayscale")
    image = img_to_array(image)
    image = image/255.0
    image = np.reshape(image,[1,256,256,1])

    classes = loaded_model.predict_classes(image)

    return classes

  1. Image Upload
import streamlit as st
from PIL import Image
import classify
import numpy as np

sign_names = {
        0: 'Covid Detected!',
        1: 'Not Detected',
}

st.title("Covid19 Detection")

uploaded_file = st.file_uploader("Choose the x-ray image...", type="jpg")
if uploaded_file is not None:

    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image', use_column_width=True)

    st.write("")

    if st.button('predict'):
        st.write("Result...")
        label = classify.predict(uploaded_file)
        label = label.item()

        res = sign_names.get(label)
        st.markdown(res)

Any solution to this @snehankekre @randyzwitch