Key Error for st.session_state variable when stopping the streamlit webrtc live camera analysis


Hey, all I’m creating a web app that recognizes emotion from real-time video using Microsoft’s DeepFace library. I am able to get the webcam activated using the streamlit webrct_streamer library and have real-time analysis on my local computer. This works perfectly when the camera is running but when I hit the Stop button, I receive an error regarding setting the detected dominant emotion to st.session_state["user_emotion"]. I am able to accurately set the “user_emotion” session_state variable to the emotion detected until I hit the Stop button My error is given below:

Steps to reproduce

Code snippet:

lock = threading.Lock()
img_container = {"img": None}


def video_frame_callback(frame):
    img = frame.to_ndarray(format="bgr24")
    with lock:
        img_container["img"] = img
    return frame

frame_rate = 1
ctx = webrtc_streamer(key="example", video_frame_callback=video_frame_callback,
        "video": {"frameRate": {"ideal": frame_rate}},
        "style": {"width": "50%", "margin": "0 auto", "border": "5px purple solid"},
        "controls": False,
        "autoPlay": True,

if "user_emotion" not in st.session_state:
    st.session_state["user_emotion"] =""

main_emotion = st.empty()
while ctx.state.playing:
    with lock:
        img = img_container["img"]
    if img is None:
    emotion_data = DeepFace.analyze(img_path=img,actions=['emotion'],enforce_detection=False)
    if emotion_data != []:
        st.session_state["user_emotion"] = emotion_data[0]["dominant_emotion"]
        curr_emotion = st.session_state["user_emotion"]

Expected behavior:

I would expect when i hit the Stop button of the streamlit webrtc module the last detected emotion would be saved in st.session_state["user_emotion] like “angry” “sad” “happy” without erroring.

Actual behavior:


Traceback (most recent call last):
  File "/Users/v.esau.hutcherson/.local/share/virtualenvs/StreamLit-ohTsyygW/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/", line 565, in _run_script
    exec(code, module.__dict__)
  File "/Users/v.esau.hutcherson/StreamLit/pages/", line 146, in <module>
    if st.session_state["user_emotion"] == "neutral" or "suprised" or "happy":
  File "/Users/v.esau.hutcherson/.local/share/virtualenvs/StreamLit-ohTsyygW/lib/python3.10/site-packages/streamlit/runtime/state/", line 90, in __getitem__
    return get_session_state()[key]
  File "/Users/v.esau.hutcherson/.local/share/virtualenvs/StreamLit-ohTsyygW/lib/python3.10/site-packages/streamlit/runtime/state/", line 111, in __getitem__
    raise KeyError(key)
KeyError: 'user_emotion'

Additional Info:

The data received from DeepFace.analyze is a dictionary formatted like this for a frame instance analyzed:


I would assume I can access the dominant emotion key value and assignment it toa st.session_state variable by doing st.session_state["user_emotion] = emotion_data[0]["dominant emotion] and still works perfectly until i stop the live webcam stream. I am assuming it has something to do with this code inside

    def __getitem__(self, key: str) -> Any:
        with self._lock:
            if self._disconnected:
                raise KeyError(key)

            return self._state[key]

I assumed when webcam is stopped and disconnects that the last dominant emotion would be saved as the st.session_state variable but for some reason the self._disconnected condition is reached when i stop the camera can i receive some assistance with this?

Debug info

  • Streamlit, version 1.22.0
  • Python version: 3.10
  • Using PipEnv
  • OS version: Ventura 13.3.1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.