Rtsp stream read error when use opencv python

asked 2017-11-10 01:16:40 -0500

lpplbiubiubiu gravatar image

updated 2017-11-10 01:19:41 -0500

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?

edit retag flag offensive close merge delete

Comments

1

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()

berak gravatar imageberak ( 2017-11-10 01:41:37 -0500 )edit

You can't put while condition block inside if/else condition block.

if v.isOpened():                                                             
    success, frame = v.read()                                                
while(True):                                                             
    idx += 1                                                             
    success, frame = v.read()
supra56 gravatar imagesupra56 ( 2017-11-11 13:51:02 -0500 )edit