whitphx / streamlit-webrtc

Real-time video and audio streams over the network, with Streamlit.
https://discuss.streamlit.io/t/new-component-streamlit-webrtc-a-new-way-to-deal-with-real-time-media-streams/8669
MIT License
1.34k stars 181 forks source link

ReferenceError: weakly-referenced object no longer exists #1040

Closed whitphx closed 1 year ago

whitphx commented 1 year ago
2022-08-31 21:30:45.453 Uncaught app exception
Traceback (most recent call last):
  File "/path/to/streamlit-webrtc-article-tutorial-sample/.venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
  File "/path/to/streamlit-webrtc-article-tutorial-sample/app.py", line 21, in <module>
    webrtc_streamer(
  File "/path/to/streamlit-webrtc-article-tutorial-sample/.venv/lib/python3.9/site-packages/streamlit_webrtc/component.py", line 514, in webrtc_streamer
    session_info = get_this_session_info()
  File "/path/to/streamlit-webrtc-article-tutorial-sample/.venv/lib/python3.9/site-packages/streamlit_webrtc/session_info.py", line 43, in get_this_session_info
    current_server = get_current_server()
  File "/path/to/streamlit-webrtc-article-tutorial-sample/.venv/lib/python3.9/site-packages/streamlit_webrtc/server.py", line 52, in get_current_server
    servers = [obj for obj in gc.get_objects() if isinstance(obj, Server)]
  File "/path/to/streamlit-webrtc-article-tutorial-sample/.venv/lib/python3.9/site-packages/streamlit_webrtc/server.py", line 52, in <listcomp>
    servers = [obj for obj in gc.get_objects() if isinstance(obj, Server)]
ReferenceError: weakly-referenced object no longer exists

requirements.txt

opencv-python-headless==4.5.4.60
streamlit==1.11.1
streamlit-webrtc==0.43.2
import streamlit as st
from streamlit_webrtc import webrtc_streamer
import av
import cv2

st.title("My first Streamlit app")
st.write("Hello, world")

threshold1 = st.slider("Threshold1", min_value=0, max_value=1000, step=1, value=100)
threshold2 = st.slider("Threshold2", min_value=0, max_value=1000, step=1, value=200)

def callback(frame):
    img = frame.to_ndarray(format="bgr24")

    img = cv2.cvtColor(cv2.Canny(img, threshold1, threshold2), cv2.COLOR_GRAY2BGR)

    return av.VideoFrame.from_ndarray(img, format="bgr24")

webrtc_streamer(
    key="example",
    video_frame_callback=callback,
    rtc_configuration={  # Add this line
        "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
    }
)

Python 3.9.13

creeksflowing commented 1 year ago

I tried the same and I get the same error

whitphx commented 1 year ago

Thank you for the report!

whitphx commented 1 year ago

Hypothesis: This problem occurs on M1 mac with Python built for arm arch.

creeksflowing commented 1 year ago

Hypothesis: This problem occurs on M1 mac with Python built for arm arch.

Most likely, currently working on M1. As soon as I can I'll try on a Intel Mac and report back.

whitphx commented 1 year ago

Thank you!

Apart from that, I will release a new version which will suppress this error ASAP.

whitphx commented 1 year ago

I released v0.43.3 with the fix for this.

Please let me know if there are still problems!

kargarisaac commented 1 year ago

Hello,

What was the problem? I face the same problem on m1 with deepcopy. Looking for a solution.

Thank you

whitphx commented 1 year ago

I haven't investigated it so well and don't know the actual reason. I just added try-except to catch the error (see the PR linked above) because it was ok to ignore the error in my case.

My hypothesis is that there are something around the garbage collector in the ARM-build Python, but I don't know the details and I don't have the fundamental solution.