使用opencv打开rtsp视频流时,会因为网络问题导致VideoCapture掉线;也会因为图像的后处理阶段耗时过长导致opencv缓冲区数据堆积,从而使程序无法及时处理最新的数据。为此对cv2.VideoCapture进行封装,实现0缓存掉线重连的rtsp直播流播放器,让程序能一直处理最新的数据。
代码实现
from collections import deque
import threading
import cv2,time
#接收摄影机串流影像,采用多线程的方式,降低缓冲区栈图帧的问题。
class VideoCapture:
def __init__(self, URL):
self.URL = URL
self.isstop = False
self.lock = threading.RLock()
self.q=deque(maxlen=100)
# 摄影机连接。
self.capture = cv2.VideoCapture(self.URL)
threading.Thread(target=self.readframe, daemon=True, args