Setting a frame rate doesn't always work like you expect. It depends on two things:

  1. What your camera is capable of outputting.
  2. Whether the current capture backend you're using supports changing frame rates.

So point (1). Your camera will have a list of formats which it is capable of delivering to a capture device (e.g. your computer). This might be 1920x1080 @ 30 fps or 1920x1080 @ 60 fps and it also specifies a pixel format. The vast majority of consumer cameras do not let you change their frame rates with any more granularity than that. And most capture libraries will refuse to change to a capture format that the camera isn't advertising.

Even machine vision cameras, which allow you much more control, typically only offer a selection of frame rates (e.g. 1, 2, 5, 10, 15, 25, 30, etc). If you want a non-supported frame rate at a hardware level, usually the only way to do it is to use hardware triggering.

And point (2). When you use cv.VideoCapture you're really calling a platform-specific library like DirectShow or V4L2. We call this a backend. You can specify exactly which backend is in use by using something like:

cv2.VideoCapture(0 + cv2.CAP_DSHOW)

There are lots of CAP_X's defined, but only some will apply to your platform (e.g CAP_V4L2 is for Linux only). On Windows, forcing the system to use DirectShow is a pretty good bet. However as above, if your camera only reports it can output 30fps and 60fps, requesting 10fps will be meaningless. Worse, a lot of settings simply report True in OpenCV when they're not actually implemented. You've seen that most of the time reading parameters will give you sensible results though, however if the parameter isn't implemented (e.g exposure is a common one that isn't) then you might get nonsense.

You're better off waiting for a period of time and then reading the last image.

Be careful with this strategy. Don't do this:

while capturing:
    res, image = cap.read()
    time.sleep(1)

you need to make sure you're continually purging the camera's frame buffer otherwise you will start to see lag in your videos. Something like the following should work:

frame_rate = 10
prev = 0

while capturing:

    time_elapsed = time.time() - prev
    res, image = cap.read()

    if time_elapsed > 1./frame_rate:
        prev = time.time()

        # Do something with your image here.
        process_image()

For an application like a hand detector, what works well is to have a thread capturing images and the detector running in another thread (which also controls the GUI). Your detector pulls the last image captured, runs and display the results (you may need to lock access to the image buffer while you're reading/writing it). That way your bottleneck is the detector, not the performance of the camera.

Answer from Josh on Stack Overflow
🌐
PyImageSearch
pyimagesearch.com › home › blog › increasing webcam fps with python and opencv
Increasing webcam FPS with Python and OpenCV - PyImageSearch
February 20, 2023 - The “secret” to obtaining higher FPS when processing video streams with OpenCV is to move the I/O (i.e., the reading of frames from the camera sensor) to a separate thread. You see, accessing your webcam/USB camera using the cv2.VideoCapture ...
🌐
Chief Delphi
chiefdelphi.com › technical
Increase Camera FPS OpenCV python - Technical - Chief Delphi
February 14, 2024 - Hello intelligent people of FRC community! My team is trying to increase the camera fps using built in opencv python functions. The cameras should be able to get 60fps, but we are currently getting only 10fps sadly. This is what we’ve tried so far: while True: cap = cv.VideoCapture(path) cap:cv.VideoCapture cap.set(cv.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720) cap.set(cv.CAP_PROP_FPS, 30) cap.set(cv.CAP_PROP_FOURCC, ...
Discussions

python - Changing FPS in OpenCV - Stack Overflow
I have an application that needs to capture only a few frames per second from a webcam. Setting videowriter in the below code to 3 frames per second results in the webcam's normal framerate of More on stackoverflow.com
🌐 stackoverflow.com
How to configure your webcam to 60 FPS? - Python - OpenCV
Subject: Need Help with Logitech C922 Pro HD Streaming Webcam 60FPS Issue Hello, First of all, I would appreciate your understanding as my English is not fluent. I am using the Logitech C922 Pro HD Streaming Webcam to capture real-time video at 60FPS. The webcam supports 60FPS, and the video ... More on forum.opencv.org
🌐 forum.opencv.org
0
February 18, 2025
How to decrease frame rate of USB webcam 2.0 using openCV python - Stack Overflow
I am working on ubuntu 16.04 and using a USB 2.0 webcam. I want to decrease the frame rate somehow since the project I'm working on requires face detection which really lags the video hence want to More on stackoverflow.com
🌐 stackoverflow.com
How to make python record 30 or 60 frames per second using openCV and a Logitech C920 - Stack Overflow
I am trying to record a video in 720p at 60 FPS or 1080p at 30 FPS, However when using the C920 webcam and OpenCV on python I can only get about 10 fps on 720p and 5fps on 1080p. I have tried a lot More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 6
47

Setting a frame rate doesn't always work like you expect. It depends on two things:

  1. What your camera is capable of outputting.
  2. Whether the current capture backend you're using supports changing frame rates.

So point (1). Your camera will have a list of formats which it is capable of delivering to a capture device (e.g. your computer). This might be 1920x1080 @ 30 fps or 1920x1080 @ 60 fps and it also specifies a pixel format. The vast majority of consumer cameras do not let you change their frame rates with any more granularity than that. And most capture libraries will refuse to change to a capture format that the camera isn't advertising.

Even machine vision cameras, which allow you much more control, typically only offer a selection of frame rates (e.g. 1, 2, 5, 10, 15, 25, 30, etc). If you want a non-supported frame rate at a hardware level, usually the only way to do it is to use hardware triggering.

And point (2). When you use cv.VideoCapture you're really calling a platform-specific library like DirectShow or V4L2. We call this a backend. You can specify exactly which backend is in use by using something like:

cv2.VideoCapture(0 + cv2.CAP_DSHOW)

There are lots of CAP_X's defined, but only some will apply to your platform (e.g CAP_V4L2 is for Linux only). On Windows, forcing the system to use DirectShow is a pretty good bet. However as above, if your camera only reports it can output 30fps and 60fps, requesting 10fps will be meaningless. Worse, a lot of settings simply report True in OpenCV when they're not actually implemented. You've seen that most of the time reading parameters will give you sensible results though, however if the parameter isn't implemented (e.g exposure is a common one that isn't) then you might get nonsense.

You're better off waiting for a period of time and then reading the last image.

Be careful with this strategy. Don't do this:

while capturing:
    res, image = cap.read()
    time.sleep(1)

you need to make sure you're continually purging the camera's frame buffer otherwise you will start to see lag in your videos. Something like the following should work:

frame_rate = 10
prev = 0

while capturing:

    time_elapsed = time.time() - prev
    res, image = cap.read()

    if time_elapsed > 1./frame_rate:
        prev = time.time()

        # Do something with your image here.
        process_image()

For an application like a hand detector, what works well is to have a thread capturing images and the detector running in another thread (which also controls the GUI). Your detector pulls the last image captured, runs and display the results (you may need to lock access to the image buffer while you're reading/writing it). That way your bottleneck is the detector, not the performance of the camera.

2 of 6
7

I could not set the FPS for my camera so I manage to limit the FPS based on time so that only 1 frame per second made it into the rest of my code. It is not exact, but I do not need exact, just a limiter instead of 30fps. HTH

import time

fpsLimit = 1 # throttle limit
startTime = time.time()
cv = cv2.VideoCapture(0)
While True:
    frame = cv.read
    nowTime = time.time()
    if (int(nowTime - startTime)) > fpsLimit:
        # do other cv2 stuff....
        startTime = time.time() # reset time
Top answer
1 of 2
2

You set the FPS for the output via the VideoWriter,
but you didn't attempt to set the FPS for the input via the VideoCapture.

In order to do that you can try to call cv2.VideoCapture, with the cv2.CAP_PROP_FPS property after you create cap.
For example:

cap.set(cv2.CAP_PROP_FPS, 3)

However - note that the actual behavior is dependant on the specific capture device you are using. Some support only certain FPSs. See also this post regarding it: change frame rate in opencv 3.4.2.

If it does work you will be able to simplify your code a lot - just capture frames, process them and save (without any manual fps management).

2 of 2
1

This method programmatically sets frames per second. A 6.1mb file was created when frame rate was set for 30fps, and a 0.9mb file when set for 3fps.

#!/usr/bin/env python3

import cv2
import numpy as np
import time
import datetime
import pathlib
import imutils

cap = cv2.VideoCapture(0)

if (cap.isOpened() == False): 
  print("Unable to read camera feed")

capture_duration = 15
frame_per_sec = 30
prev = 0
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('C:\\videoPy\\LZ\\'outpout.avi',cv2.VideoWriter_fourcc('m','j','p','g'),frame_per_sec, (frame_width,frame_height))
start_time = time.time()
while( int(time.time() - start_time) < capture_duration ): 
#start fps
  time_elapsed = time.time() - prev
  while(time_elapsed > 1./frame_per_sec):                   
        ret, frame = cap.read()
        if not ret:
            break
        if time_elapsed > 1./frame_per_sec:
          prev = time.time()
#end fps
        if ret==True:
          frame = imutils.resize(frame, width=frame_width)
          out.write(frame)
          cv2.imshow('frame',frame) 
        if cv2.waitKey(1) & 0xFF == ord('q'):
          break
        else:
           break

cap.release()
out.release()
 
cv2.destroyAllWindows()
Starred by 15 users
Forked by 3 users
Languages: Python
🌐
InsideNothing
pmcg2k15.wordpress.com › 2015 › 12 › 21 › increasing-webcam-fps-with-python-and-opencv
Increasing webcam FPS with Python and OpenCV – InsideNothing
December 21, 2015 - To see the affects of webcam I/O threading in action, just execute the following command: ... Figure 1: By using threading with Python and OpenCV, we are able to increase our FPS by over 379%!
🌐
Facebook
facebook.com › dataquestio › posts › increasing-webcam-fps-with-python-and-opencv › 1028055453928237
Dataquest - Increasing webcam FPS with Python and ...
July 8, 2022 - Dataquest. 82,959 followers · 10 talking about this. Get the skills you need to get a job in data science with Dataquest's online learning platform.
Find elsewhere
🌐
OpenCV
forum.opencv.org › python
How to configure your webcam to 60 FPS? - Python - OpenCV
February 18, 2025 - Subject: Need Help with Logitech C922 Pro HD Streaming Webcam 60FPS Issue Hello, First of all, I would appreciate your understanding as my English is not fluent. I am using the Logitech C922 Pro HD Streaming Webcam to capture real-time video at 60FPS. The webcam supports 60FPS, and the video format is MJPG.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 209635 › how-can-i-control-webcam-fps
how can i control webcam fps - OpenCV Q&A Forum
February 28, 2019 - I have difficulty handling fps in real-time webcam, not video file. Currently my code always give 30 fps in any resolution like 640 x 480 or 1280 x 720. And I'm using logitech c922 which says it supports 60 fps in 720p (1280 x 720) And I tried 'cap.set(cv2.CAP_PROP_FPS, 60)' where cap is, for example, 'cv2.VideoCapture(0)'. (it's written in python by the way).
🌐
PyImageSearch
pyimagesearch.com › home › blog › increasing raspberry pi fps with python and opencv
Increasing Raspberry Pi FPS with Python and OpenCV - PyImageSearch
February 20, 2023 - Using threading allowed us to increase our video processing rate by a nice 246%; however, its important to note that as the processing pipeline becomes more complex, the FPS processing rate will go down as well. In next week’s post, we’ll create a Python class that incorporates last week’s WebcamVideoStream and today’s PiVideoStream into a single class, allowing new video processing blog posts on PyImageSearch to run on either a USB camera or a Raspberry Pi camera module without changing a single line of code!
🌐
GitHub
gist.github.com › Nannigalaxy › 0fbe211b11950d49c2085c8d8e79eb3f
Simple Webcam Video Streaming with FPS count using OpenCV Python 3 · GitHub
Save Nannigalaxy/0fbe211b11950d49c2085c8d8e79eb3f to your computer and use it in GitHub Desktop. Download ZIP · Simple Webcam Video Streaming with FPS count using OpenCV Python 3 · Raw · opencv_webcam.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
Top answer
1 of 3
2

One potential way is to set a timestamp within the loop and keep track of the time the last frame was shown. For instance, only once a certain amount of time has elapsed then you show the frame. At the same time, you constantly read frames to keep the buffer empty to ensure that you have the most recent frame. You don't want to use time.sleep() because it will freeze the program and not keep the buffer empty. Once the timestamp hits, you show the frame and reset the timestamp.

import cv2
import time

cap = cv2.VideoCapture(0)

# Timeout to display frames in seconds
# FPS = 1/TIMEOUT 
# So 1/.025 = 40 FPS
TIMEOUT = .025
old_timestamp = time.time()

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    if (time.time() - old_timestamp) > TIMEOUT:
        # Display the resulting frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        old_timestamp = time.time()

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
2 of 3
0

When working correctly, this app will be suspended at the read() call until the next frame from the streaming webcam is available. Smooth display depends on being able to execute whatever you may have added to the loop in less than the 1/FPS seconds. It also depends on the camera being UVC compliant and it may depend on the encoding algorithm being MJPEG, which is the case for most webcams. However the fact that you see delay up to 1/3 second is curious because that is a typical GOP period for mpeg or other inter-frame encoders.

If none of the above applies to your case then I suspect the problem is platform related rather than an OCV issue. Have you tried to duplicate the problem on another system?

Top answer
1 of 1
2

Admittedly the CPU on the RPI B+ isnt the greatest (single core, 700MHz w/o overclocking), but there is a chance there might be other issues slowing down your processing pipeline

Have you done any profiling to identify the bottlenecks in your script(s) ?

In my experience, One of the slowest operations is I/O to disk (microsd card or USB storage). If your pipeline involves one or more disk IO there is a good chance that is pulling down your overall fps throughput.

The simplest way (without resorting to external tools) to identify such problems is to simply comment out or bypass some sections of your scripts and looking at the fps. If no improvement in fps, then restore it and move on to another section.

if you are open to using additional or external tools, here are some links I think you might find useful

  • https://docs.python.org/2/library/profile.html
  • https://www.huyng.com/posts/python-performance-analysis
  • https://stackoverflow.com/a/582337/1635109

While multithreading can improve responsiveness it doesnt really improve throughput for CPU intensive applications on single core devices if you find the CPU is indeed your bottleneck your options are somewhat limited

  • Overclocking
  • use a RPi2/RPI3 (quad core @ 900/1200 MHz)

Here are some interesting writeups on the pros/cons of multithreading targeting different types of applications and scenarios (I/O bound, CPU bound, UI interaction etc)

  • https://msdn.microsoft.com/en-us/library/ms810437.aspx
  • https://msdn.microsoft.com/en-us/library/ms810439.aspx
🌐
Stack Overflow
stackoverflow.com › questions › 74048393 › can-i-speed-up-processing-live-video-from-webcam
python - Can I speed up processing live video from webcam - Stack Overflow
from imutils.video import WebcamVideoStream import cv2 as cv import mediapipe as mp import time mp_hands = mp.solutions.hands mp_drawing = mp.solutions.drawing_utils vs = WebcamVideoStream(src=0).start() # FPS handler, to show actual framerate inside a loop def fps_handle(start): end = time.time() total_time = end - start if total_time != 0: fps = 1 / total_time print(f"FPS: {round(fps, 2)}") cv.putText(frame, f"FPS: {int(fps)}", (20,70), cv.FONT_HERSHEY_PLAIN, 2, (255,0,255), 2, 1) else: print(f"FPS: ???") cv.putText(frame, f"FPS: ???", (20,70), cv.FONT_HERSHEY_PLAIN, 2, (255,0,255), 2, 1) wi
🌐
Reddit
reddit.com › r/computervision › speeding up frame capture in opencv
r/computervision on Reddit: Speeding up frame capture in opencv
January 15, 2020 -

I'm working on an application in python using opencv for use in athletic coaching. I'd like it to run at 60fps, but I can't seem to get it to that speed. I've determined that the part that is slowing everything down is the capture process; frames aren't getting from the camera to the cpu fast enough. All testing I've done so far indicates that the processing and the rendering are not the bottleneck; it's specifically the video input that is slowing everything down.

Now, I know it is possible for the camera to run at 60fps, because when I open up the same camera in guvcview (at the same resolution), I can get a full 60fps.

I don't want to share my entire project, but here's the part that manages frame capture: https://pastebin.com/nnx32rQK. My main app calls the get_frame() function in its own thread. I'm running this on an intel NUC6CAYH (celeron CPU, 2.3 ghz), running linux.

Any help would be welcome!

EDIT: I went into more detail below, but it looks like having the cap.read() within the Feed class is a whole lot slower than just accessing cap.read() directly..... and I have no idea why

🌐
OpenCV
forum.opencv.org › t › opencv-camera-low-fps › 567
OpenCV Camera Low FPS - OpenCV
December 28, 2020 - Hello, I was using OpenCV 4.5.1 to measure the FPS of my camera and I found the actual FPS measured is way below my expectation. The code I used for measurement is from How to find frame rate or frames per second (fps) in OpenCV ( Python / C++ ) ? | Learn OpenCV #include "opencv2/opencv.hpp" #include using namespace cv; using namespace std; int main(int argc, char** argv) { // Start default camera VideoCapture video(0); // With webcam get(CV_CAP_PROP_FPS) does not work....