1

I am trying to get a frame from the camera every each 3 seconds using cv2.VideoCapture() so I used time.sleep() to pause the execution, the code is here:

import cv2
import time
cnt = 0 
cap = cv2.VideoCapture(0)
while (True):
    time.sleep(3)
    ret, frame = cap.read()
    cv2.imwrite('{}.png'.format(cnt),frame)
    cnt+=1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

And I got a stopwatch in front on the camera and save a frame each 3 seconds but I found that after the first frame there are other 4 frames that are wrongly saved so the saved images time is: 1.0, 1.1, 1.1, 1.2, 1.3, 2, 3, 4, 5, 6, etc. Then how on earth that's happening?

Ahmed Salah
  • 851
  • 2
  • 10
  • 29
  • I haven't tried this, but what happens if you set the framerate to 1/3 second at the start? – Mark Setchell Jun 26 '18 at 21:14
  • afaik either opencv or the camera itself can use buffers to improve image capturing behaviour, leading to this kind of effect. – Micka Jun 28 '18 at 05:25
  • imho the best way to solve it is to capture each frame without pauses and drop the unwanted ones yourself. – Micka Jun 28 '18 at 05:27

1 Answers1

2

The issue seems to be that opencv is buffering multiple frames when you call read(). From a few other questions (Get most recent frame from webcam, http://answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/) it looks like 5 images are buffered, so you may need to grab() the extra 4 before continuing.

lxop
  • 7,596
  • 3
  • 27
  • 42