问题描述:每次读取大约 100 帧后,都会出现如下问题非正常结束。
回答1:
我正面临这个问题,我现在刚刚解决了它。
(注意:我使用的是python3)
我怀疑这与时间有关,因为在连续capture.read(). 你的问题让我确信这一点。
此外,当我从我的网络摄像机(使用 H264 编码)流式传输时会出现问题,并且在使用我的笔记本电脑摄像机时没有问题。
所以对我有用的解决方案是使用多线程和 python “线程”模块。一个线程流和其他进程,同时正确管理线程锁,因此不会发生读/写冲突。
回答2:
这仅适用于视频流:
如果返回为 False,您所要做的就是再次初始化 cv2.VideoCapture。😃
为我工作。
vs = cv2.VideoCapture("rtsp://bla:bla@192.168.x.x")
while True:
ret,frame = vs.read()
if not(ret):
st = time.time()
vs = cv2.VideoCapture("rtsp://bla:bla@192.168.x.x")
print("tot time lost due to reinitialization : ",time.time()-st)
continue
cv2.imshow("Current frame", frame)
cv2.waitKey(0)
回答3:
我在具有 ubuntu 16.04 的系统上使用带有 opencv 3.4 和 python3 的海康威视 ip poe 相机。相机以 h264 格式流式传输。
使用 RTSP,我使用 opencv 的视频捕获从相机流式传输,有时我遇到同样的问题“[h264 @ 0x8f915e0] 解码 MB 43 20,字节流 -4 时出错”
当您在进一步处理中使用捕获的帧并在 rtsp 仍在流式传输时在管道中创建延迟时,就会产生此问题。
解决方案是将捕获放在不同的线程上,并将您使用的帧放在另一个线程上。
在使用多线程与 python 的同一进程中,您将有类似的东西:
#thread1
global frame
frame = None
cap = cv2.VideoCapture("rtsp://bla:bla@192.168.x.x")
while True:
ret,frame = cap.read()
#thread2
cv2.imshow("Current frame", frame)
cv2.waitKey(0)
# you can pass now the frame to your application for further processing