Ask Your Question
0

Skip frames and seek to end of RTSP stream

asked 2018-08-10 03:27:29 -0600

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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-08-10 03:48:04 -0600

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
edit flag offensive delete link more

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 ( 2020-05-22 21:55:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-10 03:27:01 -0600

Seen: 10,133 times

Last updated: Aug 10 '18