跳过框架,并寻求在OpenCV Python中结束RTSP流
我正在尝试使用RTSP流,但是我只想在每个执行时间内最后一个帧:
while True
image.get()
#EXECUTE STUFF AROUND 0.1seconds
我在25FPS摄像机的RTSP流上使用以下代码。问题在于,在完美工作5分钟后,CV22。VIDEOCAPTURE流停止以返回图像(返回false标志)。这是为什么?
import threading
from threading import Lock
import cv2
rtsp_link = "rtsp://url"
vcap = cv2.VideoCapture(rtsp_link)
latest_frame = None
last_ret = None
lo = Lock()
def rtsp_cam_buffer(vcap):
global latest_frame, lo, last_ret
while True:
with lo:
last_ret, latest_frame = vcap.read()
t1 = threading.Thread(target=rtsp_cam_buffer,args=(vcap,),name="rtsp_read_thread")
t1.daemon=True
t1.start()
while True :
if (last_ret is not None) and (latest_frame is not None):
img = latest_frame.copy()
else:
print("unable to read the frame")
time.sleep(0.2)
continue
CV2。VIDEOCAPTURE流的时间停止接收框架,这是不规则的,有时是几秒钟,现在它已经工作了15分钟。如何?!
当RTSP流停止接收图像时,屏幕上没有显示错误。
I am trying to use RTSP stream, but I just want the last frame every certain execution time:
while True
image.get()
#EXECUTE STUFF AROUND 0.1seconds
I am using the following code on a RTSP stream of a 25fps camera. The problem is that after around 5 minutes of working perfectly fine, the cv2.VideoCapture stream stops to return an image (returns False flag). Why is that?
import threading
from threading import Lock
import cv2
rtsp_link = "rtsp://url"
vcap = cv2.VideoCapture(rtsp_link)
latest_frame = None
last_ret = None
lo = Lock()
def rtsp_cam_buffer(vcap):
global latest_frame, lo, last_ret
while True:
with lo:
last_ret, latest_frame = vcap.read()
t1 = threading.Thread(target=rtsp_cam_buffer,args=(vcap,),name="rtsp_read_thread")
t1.daemon=True
t1.start()
while True :
if (last_ret is not None) and (latest_frame is not None):
img = latest_frame.copy()
else:
print("unable to read the frame")
time.sleep(0.2)
continue
The time until the cv2.videocapture stream stops receiving frames it's irregular, sometimes it's seconds, and now it has been working 15 minutes. How?!
No error is shown on the screen when RTSP stream stops receiving images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中一些人所说的那样,问题不是代码。使用螺纹进行操作非常好。
问题在于,与相机的连接非常糟糕。当我做Ping Cameraip时,我损失了50%的数据包。
但是,我更改了代码,并添加了一个条件,即如果在30秒内没有收到任何图像,请重新连接相机,再次执行CV2.VIDECAPTURE。
As some people in the comments have said, the issue was not the code. Doing it with threading works perfectly fine.
The issue was that the connection to the camera was very bad. I lost 50% of the packets when I did PING cameraIP.
Nonetheless, I change the code, and added a condition, that if none image is received during more than 30 seconds, reconnect the camera, executing cv2.VideoCapture again.