Images outside of Streamlit

Dears,

I have an application whose output appears outside of Streamlit (images with the camera on). The last line of the code is

cv2.imshow (‘Display’, img)

I would like to put images from this camera within Streamlit.

Thanks,

Luís Carlos

Hi @LuisCarlosEiras -

You should be able to use st.image to display your images. Do you have a code example you can share?

Best,
Randy

Randy,

Thank you for your attention.

But, the st.image did not work in this case.

The image to be shown is at the same time an image of the camera and a processing of it.

The complete code is in

https://github.com/LuisCarlosEiras/comoficarinvisivel

and waiting for a solution in

https://share.streamlit.io/luiscarloseiras/comoficarinvisivel/main/invisivelst.py

(“Como ficar invisível” is “How to stay invisible”…)

Thanks,

Luís Carlos

2 things.

  1. cv2.VideoCapture(0) uses the web camera on the server, not the client (browser). So this will never work on StreamLit Sharing.

  2. Do not use infinite loops in StreamLit. Please read on how Streamlit works. Basically, each time you interact with something in the UI, the whole code re-runs.

If you really want to do this only using StreamLit for some reason, you can try something like this on your laptop:

import streamlit as st
import cv2 as cv2

st.title('Streamlit OpenCV Camera Test')

MAX_FRAMES = st.slider("Select number of frames to render:", min_value=60, max_value=600, value=300, step=30)
run = st.button("Click to render server camera")

if run:
    capture = cv2.VideoCapture(0)
    img_display = st.empty()
    for i in range(MAX_FRAMES):
        ret, img = capture.read()
        img_display.image(img, channels='BGR')
    capture.release()
    st.markdown("Render complete")