我的任务是使用OpenCV处理来自Hikvision摄像机的流视频。
我试试这个
""“
cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword@172.16.30.248:555/Streaming/channels/1/")还有这个
""“
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)在第一种情况下,在实时和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的流吗?
发布于 2021-08-11 20:32:50
在纳坦西斯的岗位上,我找到了工作解决方案。一切都结束了。我使用简单的修改,他的代码为我的情况,以获得框架的主要功能。
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:
passhttps://stackoverflow.com/questions/68723205
复制相似问题