Unable to open webcam in Streamlit in cloud envirement

I am developing a Streamlit application that is intended to access and capture video from a webcam. The application works locally but encounters issues when deployed on an Amazon EC2 instance. Below, I’ve detailed the application’s functionality, the issues I’m facing, and the steps I’ve taken so far.

  • Functionality: The application uses streamlit_webrtc, an extension of Streamlit, to access the webcam and capture video frames. The captured frames are processed and displayed within the Streamlit interface.
  • Libraries and Tools: The key Python libraries used in this project include Streamlit, streamlit_webrtc, OpenCV (cv2), and asyncio.

Issues Encountered on EC2:

Exception in callback Transaction.__retry()
handle: <TimerHandle when=4213.466461538 Transaction.__retry()>
Traceback (most recent call last):
File “/usr/lib64/python3.9/asyncio/selector_events.py”, line 1054, in sendto
self._sock.sendto(data, addr)
AttributeError: ‘NoneType’ object has no attribute ‘sendto’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/usr/lib64/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/ec2-user/.local/lib/python3.9/site-packages/aioice/stun.py", line 312, in __retry
    self.__protocol.send_stun(self.__request, self.__addr)
  File "/home/ec2-user/.local/lib/python3.9/site-packages/aioice/ice.py", line 266, in send_stun
    self.transport.sendto(bytes(message), addr)
  File "/usr/lib64/python3.9/asyncio/selector_events.py", line 1064, in sendto
    self._fatal_error(
  File "/usr/lib64/python3.9/asyncio/selector_events.py", line 711, in _fatal_error
    self._loop.call_exception_handler({
AttributeError: 'NoneType' object has no attribute 'call_exception_handler'

And here is my source code:

import streamlit as st
from streamlit_webrtc import webrtc_streamer, RTCConfiguration, VideoProcessorBase
import av

RTC_CONFIGURATION = RTCConfiguration(
    {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
)

class VideoProcessor(VideoProcessorBase):
    def recv(self, frame):
        img = frame.to_ndarray(format="bgr24")
        return av.VideoFrame.from_ndarray(img, format="bgr24")

def main():
    st.title("Webcam Live Feed")

    webrtc_ctx = webrtc_streamer(key="example", 
                                 rtc_configuration=RTC_CONFIGURATION, 
                                 video_processor_factory=VideoProcessor, 
                                 media_stream_constraints={"video": True, "audio": False})

    if st.button("Capture"):
        if webrtc_ctx.video_processor:
            frame = webrtc_ctx.video_frame.to_image()  # Capturing the frame as an image
            st.image(frame, caption="Captured Image", use_column_width=True)

if __name__ == "__main__":
    main()

Hi @yusufsuzyusuf

Have you tried going over the GitHub issues for webrtc Issues · whitphx/streamlit-webrtc · GitHub which is a great resource for troubleshooting issues when using webrtc

Hope this helps!

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