0

我的任务是使用 OpenCV 处理来自 Hikvision IP cam 的流式视频

我试试这个

  1. RTSP

"""

cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword@172.16.30.248:555/Streaming/channels/1/")

和这个

  1. 使用 API 海康威视

"""

cam = Client('http://192.168.1.10', 'admin', 'password', timeout=30)
cam.count_events = 2 
response = cam.Streaming.channels[101].picture(method='get', type='opaque_data')
    for chunk in response.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)

img = cv2.imread('screen.jpg')
cv2.imshow("show", img)
cv2.waitKey(1)

在第一种情况下,我在 realtime 和 cap.read() 之间有大约 9-20 秒的延迟。我用这样的“黑客”解决了它,但没有结果。

"""

class CameraBufferCleanerThread(threading.Thread):
    def __init__(self, camera, name='camera-buffer-cleaner-thread'):
        self.camera = camera
        self.last_frame = None
        super(CameraBufferCleanerThread, self).__init__(name=name)
        self.start()

    def run(self):
        while True:
            ret, self.last_frame = self.camera.read()

"""

第二种情况显示帧有1-2秒的延迟,可以接受,但是fps=1,不是很好。

是否有任何选项可以帮助您获得具有低延迟和正常 fps 的流?

4

1 回答 1

0

在 nathancys 的帖子中,我找到了工作解决方案。它完成了。我使用简单的修改他的代码来获取主函数中的框架。

from threading import Thread
import cv2, time


from threading import Thread
import cv2, time

class ThreadedCamera(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1)

        self.FPS = 1/100
        self.FPS_MS = int(self.FPS * 1000)
        # First initialisation self.status and self.frame
        (self.status, self.frame) = self.capture.read()

        # Start frame retrieval thread
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()
            time.sleep(self.FPS)


if __name__ == '__main__':
    src = 'rtsp://admin:password@192.168.7.100:554/ISAPI/Streaming/Channels/101'
    threaded_camera = ThreadedCamera(src)
    while True:
        try:
            cv2.imshow('frame', threaded_camera.frame)
            cv2.waitKey(threaded_camera.FPS_MS)
        except AttributeError:
            pass
于 2021-08-11T20:32:50.193 回答