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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Force Framerate for VideoStream(src=0)?

jperraud opened this issue · comments

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

`