Rtsp stream read error when use opencv python
Hello, everyone: When i use opencv which version is 3.3 to read rtsp stream in linux, it can open webcamera and read stream, but after read thosands of frame, it will break with this error: [h264 @ 0x1e336a0] Missing reference picture hear is part of my code:
v = cv2.VideoCapture(rtsp_ip)
idx = 0
disconnect_count = 0
print(v.isOpened())
if v.isOpened():
success, frame = v.read()
while(True):
idx += 1
success, frame = v.read()
cv2.waitKey(20)
if not success:
disconnect_count += 1
if disconnect_count > 20:
v = cv2.VideoCapture(rtsp_ip)
cv2.waitKey(1000)
gc.collect()
print(success, idx)
I konw the error is about ffmpeg, but i want to know why, and how to fix it? when I restart this script, it will run successfully and break again. I tried to reconnect it when some frame is missed, but it doesn't work well, anyone can give me advice?
streams can break, and they will.
your idea of reconnecting seems ok, just note, that cv2.waitKey() only has an effect, if there's a gui window opened, else you need some sort of sleep()
You can't put
while
condition block insideif/else
condition block.