First time here? Check out the FAQ!

Ask Your Question
0

Skip frames and seek to end of RTSP stream

asked Aug 10 '18

MLu gravatar image

Hi

I capture and process an IP camera RTSP stream in a OpenCV 3.4.2 on Raspberry Pi. Unfortunately the processing takes quite a lot of time, roughly 0.2s per frame, and the stream quickly gets delayed.

I don't mind if I skip some frames so I'm looking for a way to seek to the end of the stream before capturing and processing the next frame.

vcap = cv2.VideoCapture("rtsp://{IPcam}/12")

while(1):
    ret, frame = vcap.read()
    time.sleep(0.2)              # <= Simulate processing time
    cv2.imshow('VIDEO', frame)
    if cv2.waitKey(1) == 27:
        break
    vcap.seek_to_end()           # <== How to do this?

How can I do that vcap.seek_to_end() to catch up with the stream, discard the missed frames, and start processing the most current one?

Thanks!

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Aug 10 '18

berak gravatar image

an ip stream has no "end". and you can't "seek" anything there. itdoes not know the future and does not remember the past.

but vcap.read() is actually a combination of grab() and retrieve() . so you should:

  • grab() as often as you can. after say, 5 frames you have reached the latest img, probably.
  • retrieve() and process the img only, when desired (e.g. if you're sure, you have the latest)


vcap = cv2.VideoCapture("rtsp://{IPcam}/12")
ct = 0
while(1):
    ct += 1
    ret = vcap.grab()
    if ct % 5 == 0: # skip some frames
        ret, frame = vcap.retrieve()
        if not ret: break
        time.sleep(0.2)              # <= Simulate processing time
        cv2.imshow('VIDEO', frame)
    if cv2.waitKey(1) == 27:
        break
Preview: (hide)

Comments

This really has solved the issue. Thanks a ton. (Created this forum account just to mark your reply as right answer, but can't being a newbie here :P)

Pandian gravatar imagePandian (May 23 '0)edit

Question Tools

1 follower

Stats

Asked: Aug 10 '18

Seen: 10,331 times

Last updated: Aug 10 '18