How to update the output?

def main():
  try:
    st.markdown("<h1 style='text-align: center; color: red;'>App Name</h1>", unsafe_allow_html=True)
    st.markdown("<h2 style='text-align: center; color: black;'>Common Objects Detection</h2>", unsafe_allow_html=True)
    st.write("")
    st.title("Webcam Live Feed")
    run = st.checkbox('Run')
    FRAME_WINDOW = st.image([])
    camera = cv2.VideoCapture(0)
    st.write("")
    #img_uploaded = st.file_uploader("Choose an Image",type=["jpeg","jpg"])
    st.markdown("<h5 style='text-align: center; color: black;'>Attaching a faulty/unclear image may result in wrong prediction or no prediction at all. </h5>", unsafe_allow_html=True)
    st.markdown("<h4 style='text-align: center; color: red;'>Classifying...</h4>", unsafe_allow_html=True)
    st.markdown("<h4 style='text-align: center; color: black;'>The Object is a :</h4>", unsafe_allow_html=True)
    temp=''
    while run:
        caching.clear_cache()
        _, frame = camera.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        FRAME_WINDOW.image(frame)  
        #img_pil = PIL.Image.open(frame)
        #img_tensor = T.ToTensor()(img_pil)
        #img_fastai = Image(img_tensor)
        img_t = pil2tensor(frame, np.float32)
        img_t.div_(255.0)
        image = Image(img_t)
        a,cat_tensor,c = learn.predict(image)
        if temp!=str(a):
            st.write(str(a))
            st.write("")
            st.write("")
            st.write("")
            st.write('')
        temp=str(a)
        
        
    else:
        st.write('Stopped')
  except Exception:
    pass
if __name__ == '__main__':
  main()

Now, everytime it gives predicts on the webcam feed, it prints a new output and the page gets too big. Is there a way, I can keep updating/refreshing the output ?

Hi @Bhuvnesh, welcome to the Streamlit community!

When you want to continually overwrite the same output widget, you can use st.empty:

https://docs.streamlit.io/en/stable/api.html?highlight=st.empty#streamlit.empty

Best,
Randy

3 Likes

(assign widget = st.empty outside the while loop, then replace all your st.write statements with widget.write(str(a)) inside the while loop)

1 Like

Thank you so much !

It works now!