File Uploader File to OpenCV imread

How can I pass image file from file_uploader() to opencv’s imread()?

file_uploader does not store the uploaded file on the webserver but keeps it in memory. It returns a ‘stream’ class variable to access it. I came across this situation as well but honestly I dont have THE answer yet. Fact seems to be that some python modules dont care whether you throw a pathname or a stream at them , but many other do. To me the python ‘stream’ classes appear as a relatively relatively complex matter. You may try to give imread the stream but if this doesnt work you may save the stream to a file and then read like usual. However it might be difficult with streamlit to find the right point in time to delete the quasi temporary file. I dont know if the webserver does this by it itself, does it? Same issue with files you create for downloading. AT the moment I am completely ignoring this issue on my streamlit apps because there are only very few users that work with them.
code fragment to save the file:

fs = st.file_uploader('upload a file')
if fs is not None:
    with open(fs.name,'wb') as f:
        f.write(fs.read())
1 Like

Thanks a lot.