跳过框架,并寻求在OpenCV Python中结束RTSP流

发布于 2025-02-11 11:12:36 字数 972 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2025-02-18 11:12:36

正如评论中一些人所说的那样,问题不是代码。使用螺纹进行操作非常好。

问题在于,与相机的连接非常糟糕。当我做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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文