Kinda new to image processing, so pardon me if the question is too stupid.

Using the library cv2 in python to capture video from my webcam, which, so far as I can see as a programmer, is basically an array of images. But nothing specifies the frame rate of a video. Suppose I use the following code snippet to capture a video.

while True:
    capture, frame = cap.read()
    cv2.imshow('Being Captured. Hit Escape to Stop',frame)
    if cv2.waitKey(1)&0xFF==27:break

As I understand about the waitKey function, the camera captures a frame, waits for a millisecond, then captures the next one etc. So if a millisecond for each still image, does it mean the frame-rate is 1 KHz? Typical rates for most videos is 25-30 Hz, right? So can I adjust that just by changing the argument of waitKey in inverse proportion? Or, is my thought process wrong somewhere?


2 Answers 2


It does not determine the framerate, but it has an influence on the framerate.

The first caveat is that although you're only waiting 1ms between frames, the program has other work to do in order to render each frame, so the delay between frames is not purely caused by that 1ms wait.

The next caveat is that (from the doc)

Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time.

So, you may be thinking, okay, I won't get 1/delay framerate, but I will get at most 1/delay framerate. Alas! Caveat 3 strikes:

What if you spam keyboard input, e.g. by holding down a key and letting it repeat? Then the wait function will get cut short. Again, you may be led astray by thinking, but if I use delay=1 (millisecond) this will be trivial because no keyboard at a rate anywhere near 1 KHz! But remember that this delay is a minimum amount of time it will wait, in reality the wait is much longer.

In an application I wrote (more complex than this though) using cv2.waitKey(1) after showing each frame, I get a 50% increase in fps by simply holding down a key on my keyboard.

 Hands-off FPS: 64.09
Key-repeat FPS: 90.99

The moral is that if you want stable, controlled FPS, you should pick another route, because there are so many caveats and unpredictabilities here.


waitKey actually determines how much time your program "waits" for a key being pressed by the user before going on with the rest of the program.

It is usually indeed used to "simulate" a given frame rate between two successive frames to be displayed. However, if your image processing step takes 1 second to be executed, your video will be displayed in slow motion anyway.

You can also use it with the argument 0 (or no argument at all as it is the default) if you want your program to display the same frame until you press any key. That last use case is sometimes useful when you want to carefully observe a series of following frames without saving them on your hard drive.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.