OpenCV error

i was trying to create a simple opencv application. where it just open the camera feed and give output. iw works fine while running on the localhost but unable to run this while i am trying to deploy it. please help me solve this issue. i tried changing the value in cv2.VideoCapture(0) to 1, 2 etc still i am facing the same problem.

link to git/: affanmohammed/mlproject (github.com)

[ERROR:2@96.403] global obsensor_uvc_stream_channel.cpp:159 getStreamChannelGroup Camera index out of range
2024-02-26 02:36:56.533 Uncaught app exception
Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/mlproject/cvstream.py", line 20, in <module>
    cv2.destroyAllWindows()
cv2.error: OpenCV(4.9.0) /io/opencv/modules/highgui/src/window.cpp:1266: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvDestroyAllWindows'

this is my code :

import cv2
import streamlit as st

run = st.checkbox(‘Run’)
if run:
cap = cv2.VideoCapture(0)
st.title("test2 ")
frame_placeholder = st.empty()
stop_button_pressed = st.button(“Stop”)

while cap.isOpened():
    ret, frame = cap.read()
    cv2.rectangle(frame, (0, 0), (640, 40), (115,115,115), -1)
    frame_placeholder.image(frame, channels="BGR")

    if stop_button_pressed:
        break

cap.release()
cv2.destroyAllWindows()

Hi @affanmohammed . If you want to use cv2 in streamlit cloud application, you need to use something called streamlit webrtc. Refer to this blog

Happy Streamlit-ing :balloon:

1 Like

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