How to save image file in to 'Streamlit for teams' server while deployment

Hi Guys ,

I was able to deploy my app thru Streamlit app thru single click Streamlit for teams ., thanks to the whole team who are working on making our life easier :hugs: .

Though I was able to deploy , image which was uploaded is not callable in function . below is the code and error message from app

uploaded_file = st.file_uploader(“Choose an Image …”, type=“jpg”)
if uploaded_file is not None:
img = Image.open(uploaded_file)
#img = base64.b64encode(uploaded_file.getvalue())
#uploaded_file = uploaded_file.get_values()
st.image(uploaded_file, caption=‘Uploaded Image.’, use_column_width=True)
st.write("")
st.write(“Classifying…”)
st.write(type(img))
st.write(img)
label = machine_classification(img,‘model1.h5’)

Hi @arun_ramji! Thanks for your feedback - glad to know that the deployment platform worked for you seamlessly!

As for the error - could you please post a link to your app and/or to your repo? Also, just to sanity-check, are you able to run the app locally without this error?

1 Like

Hi @amey-st , I have made some changes now I am getting below error and also given my app link .
same app working fine in local .

https://s4a.streamlit.io/arunramji/skin_cancer/master/app.py/+/

This error means you are trying to pass a BytesIO buffer (which is what file_uploader returns), instead of the actual data.

pic = file_uploader(...)

data = pic.read()

Hi @randyzwitch , But why is it working fine in local . Below is the exact scrip I am using.

I may be wrong , but I think while it running on hosting server file is not being stored temporarily .

uploaded_file = st.file_uploader("Choose an Image ...", type="jpg")
if uploaded_file is not None:
    #uploaded_file = Image.open(uploaded_file)
    st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
    st.write("")
    st.write("Classifying...")
    #st.write(type(uploaded_file))
    label = machine_classification(uploaded_file,'model1.h5')
    my_bar = st.progress(0)
    for percent_complete in range(100):
        time.sleep(0.1)
        my_bar.progress(percent_complete + 1)
    if label == 0:
        st.subheader('RESULT :')
        t = "<div>As per our AI Engine - There is a chance that it is a<span class='highlight'> <span class='bold'> benign</span> </span> melanoma!</div>"
        st.markdown(t, unsafe_allow_html=True)
    else:
        st.subheader('RESULT :')
        t = "<div>As per our AI Engine - There is a chance that it is a<span class='highlight'> <span class='bold'> Malignant</span> </span> melanoma!</div>"
        st.markdown(t, unsafe_allow_html=True) ```

Where does uploaded_file come from in the line if uploaded_file is not None:?

1 Like

@randyzwitch , I missed to add it , Please see the updated one above

When you use file_uploader, it’s always going to return a BytesIO object. It will never save a file locally on a machine, the data will always stay in a buffer in RAM. So you need to use uploaded_file.read() to read the buffer that results from st.file_uploader().

With your commented out line #uploaded_file = Image.open(uploaded_file), that is when the file exists on your local machine. These are two separate scenarios.

1 Like

Thanks @randyzwitch and others who helped me on this . I was finally able to deploy my app in streamline team

It was a terrible mistake , as @randyzwitch mentioned, I was trying to use load_image() function to read image in byte format but that specific function should be used to read the image path from local file .

Here is the code to read image in byte format .

from PIL import Image
import io
from keras.preprocessing import image
img = Image.open(io.BytesIO(img_bytes))
img = img.convert('RGB')
img = img.resize(target_size, Image.NEAREST)
img = image.img_to_array(img)
1 Like