whitphx / streamlit-webrtc

Real-time video and audio processing on Streamlit
https://discuss.streamlit.io/t/new-component-streamlit-webrtc-a-new-way-to-deal-with-real-time-media-streams/8669
MIT License
1.36k stars 182 forks source link

New callback design without class #712

Closed whitphx closed 2 years ago

whitphx commented 2 years ago

Something like the following.

some_param = st.slider()

def video_callback(in_frame):
    out_frame = some_filter(in_frame, some_param)
    return out_frame

webrtc_streamer(video_callback=video_callback)

The callback definition is refreshed on every runs, so the callback attached to the worker is replaced on each run.

The class-based callback was designed to solve these 3 problems through instance attributes:

  1. to pass values from outside to inside the callback.
  2. to pass values from inside to outside the callback.
  3. to hold (cache) computationally expensive objects over multiple runs.

This design also simply solves 1 and 3. For 1., variables outside the callback can be referred to from inside as the example code above. For 3., such objects should be cached through Streamlit's built-in cache mechanism and passed to the callback in the same way as 1.

2 is the concern. The shared state object (#478) is one option.

whitphx commented 2 years ago

Through development of #747 , we found that