OpenCV和IP cam海康威视中的帧延迟

2024-06-26 10:19:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是使用OpenCV处理来自海康威视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秒的延迟。 我用这样一个“hack”解决了它,但没有结果

“”“

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的流


Tags: nameselfimgreadresponse情况cv2camera
1条回答
网友
1楼 · 发布于 2024-06-26 10:19:51

在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

相关问题 更多 >