Problem releasing the videoCapture (OpenCV) after Rerun the App
See original GitHub issueI am able to stream the video from the webcam to streamlit UI. The problem occurs when I change a parameter in the application;
Here is the code to reproduce the bug:
import streamlit as st
import cv2 as cv
cap = cv.VideoCapture(0)
frameST = st.empty()
param=st.sidebar.slider('chose your value')
while True:
ret, frame = cap.read()
# Stop the program if reached end of video
if not ret:
print("Done processing !!!")
cv.waitKey(3000)
# Release device
cap.release()
break
frameST.image(frame, channels="BGR")
Any help ?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
OpenCv error can't open camera through video capture
In my case, I just reconnected cam to the usb port, and then it was solved! I think this error is caused by...
Read more >upgrading to opencv 3.4.2 to run a programme the camera ...
It seems to be only related with opencv 3.4.2. CameraObject = cv.VideoCapture.release(). does not work. It does throw a type error. TypeError: ...
Read more >How to use Arducam's camera & opencv for live inference?
First, according to Arducam, my device id is 3 rather than 0 when initializing cap = cv2.VideoCapture(3). From there I tried a simple...
Read more >Loading Video Source OpenCV Python Tutorial
VideoCapture (0) . This will return video from the first webcam on your computer. If you are watching the tutorial videos, you will...
Read more >Develop Streamlit-WebRTC Component for Real-Time Video ...
VideoCapture (0) consumes a video stream from the first (indexed as 0) ... After opening the app with a web browser, open the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi all
This seems like the kind of issue I need to stare at for a while so I can wrap my head around it 😖 …but it may be useful for me to jump in real quick to explain how Streamlit works internally.
Streamlit launches a new thread each time it needs to re-execute your script.
Meaning:
So based on this:
…from a cursory look it seems that things are actually working as designed.
But since in your case you’re grabbing a limited resource with
VideoCapture()
, you probably need to make all threads share that resource. The best way to do that is to put it in the Streamlit cache:Let me know if that works for you!
Here is a clever workaround, i run a subthread inside while loop and keep on updating latest time frame, if that subthread finds a time gap greater than 1 sec then it gets terminated and ultimately releasing video capture object… one thing that i observed is if you use your code and stop it from UI, the lines below while loop are not been executed