跳过帧并尝试结束OpenCV中的RTSP流

MLu*_*MLu 3 python opencv video-capture rtsp

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

我不在乎是否跳过某些帧,因此我正在寻找一种方法来捕获和处理下一个帧,然后找到流的末尾。

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?
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

小智 6

我将 Shubham 的线程实现重写为一个类,以便能够流式传输多个摄像机:

import threading
from threading import Lock
import cv2

class Camera:
    last_frame = None
    last_ready = None
    lock = Lock()

    def __init__(self, rtsp_link):
        capture = cv2.VideoCapture(rtsp_link)
        thread = threading.Thread(target=self.rtsp_cam_buffer, args=(capture,), name="rtsp_read_thread")
        thread.daemon = True
        thread.start()

    def rtsp_cam_buffer(self, capture):
        while True:
            with self.lock:
                self.last_ready, self.last_frame = capture.read()


    def getFrame(self):
        if (self.last_ready is not None) and (self.last_frame is not None):
            return self.last_frame.copy()
        else:
            return None
Run Code Online (Sandbox Code Playgroud)

然后,它可以用作:

capture = Camera('rtsp://...')

while True:
    frame = capture.getFrame()
Run Code Online (Sandbox Code Playgroud)


Mar*_*sen 5

尝试这个:

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

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

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

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