PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.
MIT License
4.51k stars 1.03k forks source link

Force Framerate for VideoStream(src=0)? #245

Open jperraud opened 3 years ago

jperraud commented 3 years ago

Hi! I try to program a webcam stream with a fixed delay (see code below). I observe very high framerates (up to 250), which makes me believe that I have frames duplicated. However, fluctuating values in framerate between recording without showing the frames and displaying the frames with openCV lead to a very unpredictable delay. Is there a way to control the framerate to have predictable behavior and fixed delay? Cheers, Jonny

import time

from imutils.video import VideoStream
import cv2

vs = VideoStream(src=0).start()
delay = 5
frames = []

time.sleep(5)

start_time = time.time()

while True:

    frame = vs.read()
    frames.append(frame)

    if time.time()-start_time > delay:
        cv2.imshow('', frames.pop(0))

    key = cv2.waitKey(25)
    if key == ord('q'):
        break

vs.stop()

`