St.file_uploader giving nonetype output when image is uploaded

I’m using st.file_uploader widget to upload an image but it always outputs nonetype
here’s my code:

 st.write("Upload the Image of Person to Register them in DataBase")

            uploaded_file = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg', 'webp'])
            
            if uploaded_file is not None:
                file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
                image = cv2.imdecode(file_bytes, 1)

The program exits as the output is nonetype.
How can i fix this issue.

*one more point,in the same app, i’m using another instance of st.file_uploader and it’s working fine but the second instance is outputting nonetype

Hi @Adarsh_Goswami ,
I just ran your code and it’s working.

import streamlit as st
import cv2
import numpy as np

st.write("Upload the Image of Person to Register them in DataBase")

uploaded_file = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg', 'webp'])

if uploaded_file is not None:
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    image = cv2.imdecode(file_bytes, 1)
    st.image(image, channels="BGR")

Here’s the output from the app

Could you provide more details, of the code ?

That’s the exact code, I’ve applied the same code two times,it works the first time. It’s doesn’t work second time. The page just reloads

The indentation before uploaded_file and if statement isn’t necessary, try removing that.

I’m sorry that indentation occurred when I wrote the code here and if I remove the if statement then code runs but the output is still a nonetype and my further code breaks

Try to give a unique key to every st.file_uploader like this:

uploaded_file1 = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg', 'webp'], key="1")
uploaded_file2 = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg', 'webp'], key="2")

I fixed it but had to change layout of my code,it seems that nested buttons don’t work. I had to remove nested buttons to make it work

Hey @Adarsh_Goswami ,
Let us know if the solution above helped you to solve your problem, if not we hope to see your solution.