【发布时间】:2020-07-04 02:03:51
【问题描述】:
我很难理解为什么我无法从我的 IP 摄像头获得“实时”提要。
似乎有一个缓冲区,如果没有被读取,它会导致帧建立 - 而且由于我的代码的每次迭代都需要一些时间,所以会出现积压,最终对实际发生的事情来说几乎是缓慢的。
我发现下面的代码会触发一个线程在循环中读取相机以尝试避免这种情况。但现在我得到了大约 5 帧的“实时”提要,然后它停止并在另外几帧中显示相同的图像。
##camera class - this stops the RTSP feed getting caught in the buffer
class Camera:
def __init__(self, rtsp_link):
#init last ready and last frame
self.last_frame = None
self.last_ready = None
self.lock = Lock()
#set capture decive
capture = cv2.VideoCapture(rtsp_link,apiPreference=cv2.CAP_FFMPEG)
#set thread to clear buffer
thread = threading.Thread(target=self.rtsp_cam_buffer, args=(capture,), name="rtsp_read_thread")
thread.daemon = True
thread.start()
#delay start of next step to avoid errors
time.sleep(2)
def rtsp_cam_buffer(self, capture):
#loop forever
while True:
with self.lock:
capture.grab()
self.last_ready, self.last_frame = capture.retrieve()
def getFrame(self):
#get last frame
if (self.last_ready is not None) and (self.last_frame is not None):
return self.last_frame.copy())
else:
return None
在这种情况下正确的做法是什么?有没有办法解决这个问题?
或
我应该使用 gstreamer 或 ffmpeg 之类的东西来获取相机源吗?如果是这样,哪个更好,为什么?有什么建议或页面可以给我一些让它工作的python示例吗?我找不到对我有意义的大量内容。
谢谢
【问题讨论】:
标签: python opencv opencv3.0 rtsp