跳过帧并尝试结束OpenCV中的RTSP流 [英] Skip frames and seek to end of RTSP stream in OpenCV

查看:777
本文介绍了跳过帧并尝试结束OpenCV中的RTSP流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Raspberry Pi上的OpenCV 3.4.2中捕获并处理IP摄像机RTSP流.不幸的是,处理过程要花费很多时间,每帧大约需要0.2s,并且流很快就延迟了.

I capture and process an IP camera RTSP stream in a OpenCV 3.4.2 on Raspberry Pi. Unfortunately the processing takes quite a lot of time, roughly 0.2s per frame, and the stream quickly gets delayed.

我不介意是否跳过某些帧,因此我正在寻找一种方法来捕获和处理下一帧之前寻找流的结尾.

I don't mind if I skip some frames so I'm looking for a way to seek to the end of the stream before capturing and processing the next frame.

vcap = cv2.VideoCapture("rtsp://{IPcam}/12")

while(1):
    ret, frame = vcap.read()
    time.sleep(0.2)              # <= Simulate processing time
    cv2.imshow('VIDEO', frame)
    if cv2.waitKey(1) == 27:
        break
    vcap.seek_to_end()           # <== How to do this?

我该怎么做vcap.seek_to_end()以赶上流,丢弃丢失的帧,并开始处理最新的帧?

How can I do that vcap.seek_to_end() to catch up with the stream, discard the missed frames, and start processing the most current one?

谢谢!

推荐答案

尝试一下:

vcap = cv2.VideoCapture("rtspsrc location = rtsp://{IPcam}/12!decodebin!videoconvert!appsink max-buffers = 1 drop = true")

vcap = cv2.VideoCapture("rtspsrc location=rtsp://{IPcam}/12 ! decodebin ! videoconvert ! appsink max-buffers=1 drop=true")

这将使用gstreamer捕获您的相机供稿,并会保留长度为1的缓冲区,并在接收到新的传入帧时丢弃最旧的缓冲区.然后,每次调用vcap.read(),您都应该获得最新的帧.

This uses gstreamer to grab your camera feed, and will maintain a buffer of length 1 and drop the oldest as new incoming frames are received. Then, every time you call vcap.read() you should get the latest frame.

如果您发现CPU使用率很高,也可以尝试在Raspberry Pi上使用OMX解码器,因为这会在GPU上解码视频(假设是h264):! rtph264depay ! h264parse ! omxh264dec ! appsink max-buffers=1 drop=true

You can also try using the OMX decoder on the Raspberry Pi if you notice CPU usage is really high, as this will decode the video (assuming it's h264) on the GPU: ! rtph264depay ! h264parse ! omxh264dec ! appsink max-buffers=1 drop=true

您可能需要重新编译OpenCV,因为默认情况下它是使用FFMPEG支持而不是gstreamer编译的.这非常简单,只需将-D WITH_GSTREAMER=ON -D WITH_FFMPEG=OFF传递给cmake命令.确保已安装apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev的gstreamer开发库.

You may need to recompile OpenCV as by default it's compiled with FFMPEG support, not gstreamer. This is fairly simple, just pass -D WITH_GSTREAMER=ON -D WITH_FFMPEG=OFF to the cmake command. Make sure you have the gstreamer development libs installed apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev.

这篇关于跳过帧并尝试结束OpenCV中的RTSP流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆