Traditional techniques for improving image analysis:

  1. Reduce the image to a monochrome sample.
  2. Reduce the range of samples, e.g. from 8-bit monochrome to 4-bit monochrome.
  3. Reduce the size of the image, e.g. 1024x1924 to 64x64.
  4. Reduce the frame rate, e.g 60fps to 5fps.
  5. Perform a higher level function to guess where the target area is with say a lower resolution, then perform the regular analysis on the cropped output, e.g. perform image recognition to locate the hand before determining the gesture.
Answer from Steve-o on Stack Overflow
🌐
OpenCV
docs.opencv.org › 3.4 › dc › d71 › tutorial_py_optimization.html
OpenCV: Performance Measurement and Improvement Techniques
Avoid using loops in Python as much as possible, especially double/triple loops etc. They are inherently slow. Vectorize the algorithm/code to the maximum extent possible, because Numpy and OpenCV are optimized for vector operations.
🌐
Pysource
pysource.com › home › blog › increase opencv speed by 2x with python and multithreading | tutorial
Increase OpenCV speed by 2x with Python and Multithreading | Tutorial - Pysource
October 15, 2024 - To speed up OpenCV, we need to introduce multi-threading. OpenCV Multithreading in a linear approach, while the program is busy displaying the current frame, it is not capturing the next frame.
Discussions

Speed up opencv from python? - Stack Overflow
Have people had success speeding up video (post) processing in python/opencv? I'm using 4.5.2 from brew on a (non-M1) MacBook Pro. The two "tall poles" I expect to impact performance are:... More on stackoverflow.com
🌐 stackoverflow.com
June 23, 2021
Python opencv - How can I speed up my movie - Stack Overflow
I vould like to speed up my movie by 2 times meaning that every frame vould appear for half of it's original time. My code currently does not do this and I do not understand why. The error I am cur... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Getting better performance using OpenCV? - Stack Overflow
I need real time processing, but the internal functions of OpenCV are not providing this. I am doing hand gesture recognition, and it works almost perfectly, except for the fact that the resulting ... More on stackoverflow.com
🌐 stackoverflow.com
Speeding up frame capture in opencv
You're creating a new thread with every single frame. Just creating the threads, let alone doing something in them, is going to be enough to prevent you from getting to 60 fps. Capture frames in one thread, do your processing / motion detection / etc on another. Don't create them as data is coming in, they should be setup and running when you're pulling frames. More on reddit.com
🌐 r/computervision
17
2
January 15, 2020
🌐
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io › en › latest › py_tutorials › py_core › py_optimization › py_optimization.html
Performance Measurement and Improvement Techniques — OpenCV-Python Tutorials beta documentation
Avoid using loops in Python as far as possible, especially double/triple loops etc. They are inherently slow. Vectorize the algorithm/code to the maximum possible extent because Numpy and OpenCV are optimized for vector operations.
🌐
Stack Overflow
stackoverflow.com › questions › 68100254 › speed-up-opencv-from-python
Speed up opencv from python? - Stack Overflow
June 23, 2021 - My initial attempts to convert Mat -> UMat have had no noticeable improvement, even though I can see that OpenCV source has OpenCL implementations for the methods I am using (undistort, cvtColor, calcOpticalFlowFarneback, etc). FWIW, I'm getting < 5 fps for a 30 fps source. ... The intent is to use python because of the speed of iteration, so I'm hoping for some easy tricks to speed of the computational step of each iteration.
🌐
Stack Overflow
stackoverflow.com › questions › 67986279 › python-opencv-how-can-i-speed-up-my-movie
Python opencv - How can I speed up my movie - Stack Overflow
Sign up to request clarification or add additional context in comments. ... Firstly, the waitkey only takes input as int type. That int number represents for ms per frame, and increase that number will slow down not speed up your video.
Top answer
1 of 3
11

Traditional techniques for improving image analysis:

  1. Reduce the image to a monochrome sample.
  2. Reduce the range of samples, e.g. from 8-bit monochrome to 4-bit monochrome.
  3. Reduce the size of the image, e.g. 1024x1924 to 64x64.
  4. Reduce the frame rate, e.g 60fps to 5fps.
  5. Perform a higher level function to guess where the target area is with say a lower resolution, then perform the regular analysis on the cropped output, e.g. perform image recognition to locate the hand before determining the gesture.
2 of 3
1

Steve-o's answer is good for optimizing your code efficiency. I recommend adding some logic to monitor execution times to help you identify where to spend efforts optimizing.

OpenCV logic for time monitoring (python):

startTime = cv.getTickCount()
# your code execution
time = (cv.getTickCount() - startTime)/ cv.getTickFrequency()

Boost logic for time monitoring:

boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
// do something time-consuming
boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();

boost::posix_time::time_duration timeTaken = end - start;
std::cout << timeTaken << std::endl;

How you configure your OpenCV build matters a lot in my experience. IPP isn't the only option to give you better performance. It really is worth kicking the tires on your build to get better hardware utilization.

The other areas to look at are CPU and memory utilization. If you watch your CPU and/or memory utilization, you'll probably find that 10% of your code is working hard and the rest of the time things are largely idle.

  • Consider restructuring your logic as a pipeline using threads so that you can process multiple images at once (if you're tracking and need the results of previous images, you need to break up your code into multiple segments such as preprocessing/analysis and use a std::queue to buffer between them, and imshow won't work from worker threads so you'll need to push result images into a queue and imshow from the main thread)
  • Consider using persistent/global objects for things like kernels/detectors that don't need to get recreated each time
  • Is your throughput slowing down the longer your program runs? You may need to look at how you are handling disposing of images/variables within the main loop's scope
  • Segmenting your code in functions makes it more readable, easier to benchmark, and descopes variables earlier (temporary Mat and results variables free up memory when descoped)
  • If you're doing low-level processing on Mat pixels where you iterate over a large portion of the image, use a single parallel for and avoid writing
  • Depending on how you are running your code, you may be able to disable debugging to get better performance
  • If you're streaming and dumping frames, prefer changing the camera settings to throttle the streaming rate instead of dumping frames
  • If you're converting from1 12 to 8 bits or only using a region of your image, prefer doing this at the camera hardware level

Here's an example of a parallel for loop:

cv::parallel_for_(cv::Range(0, img.rows * img.cols), &
{
    for (int r = range.start; r < range.end; r++)
    {
        int x = r / img.rows;
        int y = r % img.rows;
        uchar pixelVal = img.at<uchar>(y, x);
        //do work here
    }
});

If you're hardware constrained (ie fully utilizing CPU and/or memory), then you need to look at priotizing your process/OS perfomance optimizations/freeing system resources/upgrading your hardware

  • Increase the priority of the process to be more greedy with respect to other programs running on the computer (in linux you have nice(int inc) in unistd.h, in windows SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) in windows.h)
  • Optimize your power settings for maximum performance in general
  • Disable CPU core parking
  • Optimize your acquisition hardware settings (increase rx/tx buffers, etc) to offload work from your CPU
🌐
PyImageSearch
pyimagesearch.com › home › blog › faster video file fps with cv2.videocapture and opencv
Faster video file FPS with cv2.VideoCapture and OpenCV - PyImageSearch
April 17, 2021 - (source: Wikipedia) Since the .read ... speedup simply by creating a separate thread from our main Python script that is solely responsible for reading frames from the video file and maintaining a queue....
🌐
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

Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 36605701 › how-to-speedup-this-opencv-python-code
how to speedup this opencv python code? - Stack Overflow
For example, you could use something like this to reduce the image to a list of coordinates satisfying the conditions in the two nested loops + two nested ifs. That should be a lot less to process with Python only.
🌐
Reddit
reddit.com › r/computervision › my opencv python script takes 22 minutes to finish. how can i make it significantly faster?
My OpenCV python script takes 22 minutes to finish. How can I make it significantly faster? : r/computervision
March 19, 2022 - All that said, I think the most likely bottleneck is going to be file / memory management in this case. Make sure to load up the data in batches and keep all the files you can in memory if you have enough, disk operations on small files is slow even on modern SSDs.
🌐
Stack Overflow
stackoverflow.com › questions › 61926172 › how-to-speed-up-python-code-from-2-5-to-30-fps-or-more-in-real-time-video
opencv - how to speed up python code from 2.5 to 30 FPS or more in real time video? - Stack Overflow
I am trying to detect objects in real time video and need to speed up from 2.5 to 30 frames per second (FPS) or more. Unfortunately, it requests 30 FPS at least to work well. ... Copyimport cv2 import numpy as np from imutils.video import FPS # capturing video through webcam import time from skimage.morphology import disk from skimage.filters.rank import entropy from skimage.filters import threshold_otsu cap = cv2.VideoCapture(0) # video dimension in python-opencv width = cap.get(3) # float height = cap.get(4) # float print width, height time.sleep(2.0) fps = FPS().start() while (1): _, img =
🌐
Stack Overflow
stackoverflow.com › questions › 71170840 › is-there-any-method-to-make-open-cv-run-faster-using-python
tensorflow - Is there any method to make open-cv run faster using python - Stack Overflow
import cv2 import numpy as np import time def getFileNames(): classFile = 'additional Files//coco.names' with open(classFile, 'rt') as f: classNames = f.read().rstrip('\n').split('\n') return classNames def nnSetup(): configPath = 'additional Files//ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' weightsPath = 'additional Files//frozen_inference_graph.pb' net = cv2.dnn_DetectionModel(weightsPath, configPath) return net thres = 0.58 # Threshold to detect object nms_threshold = 0.15 cap = cv2.VideoCapture(0) cap.set(3,1280) cap.set(4,720) classNames = getFileNames() net = nnSetup() net.setInputSiz
🌐
KDnuggets
kdnuggets.com › 2018 › 10 › get-speed-up-data-pre-processing-python.html
Get a 2–6x Speed-up on Your Data Pre-processing with Python - KDnuggets
boots up as many Python processes as you have CPU cores, in my case 6. The actually processing line is this one: ... The executor.map() takes as input the function you would like to run and a list where each element of the list is a single input to our function. Since we have 6 cores, we will be processing 6 items from that list at the same time! ... We get a run time of 1.14265 seconds, a nearly x6 speed-up!
Top answer
1 of 1
4

I've had this same problem using openCV video capture with text-detection. It's not the webcam quality, but the fact that openCV can only show you frames as fast as you can process them in your gender detection. The solution that worked for me is to use multi-threading.

You can create a thread for the OpenCV video capture, then another thread for your image processing. The caveat: you can't magically make your image processing happen quicker without making changes the image process itself. It takes as long as it takes. What you can do is allow openCV to work on its own and send the frames to an exchange class, then allow the image processing to grab a frame and work at its own pace as CV2 continues as normal.

Here is a (shortened) version of my class for OCR image processing. You can see that in start() i'm creating a thread pointed at the ocr() process. This is where your gender identification process can go.

class OCR:

    # def __init__(self, exchange: VideoStream, language=None):
    def __init__(self):
        self.exchange = None
        # init stuff for OCR not relevant to my example, but note that it 
        # takes a VideoStream class called exchange which is where this class 
        # grabs frames to process

    def start(self):
        Thread(target=self.ocr, args=()).start()
        return self

    def set_exchange(self, video_stream):
        self.exchange = video_stream

    def ocr(self):

        while not self.stopped:
            if self.exchange is not None:
                frame = self.exchange.frame

                # # # OCR stuff goes here

Now here is the VideoStream class that grabs frames at its own pace, in a different thread. The image processing class (OCR) can then take these frames at it's own pace and the two don't affect each other's performance.

class VideoStream:
    """Class for CV2 video capture. The start() method will create a new 
thread to read the video stream"""
    def __init__(self, src=0):
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()
        # self._boxes = None
        self.stopped = False

    def start(self):
        Thread(target=self.get, args=()).start()
        return self

    def get(self):
        while not self.stopped:
            (self.grabbed, self.frame) = self.stream.read()

    def get_video_dimensions(self):
        width = self.stream.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = self.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
        return int(width), int(height)

    def stop_process(self):
        self.stopped = True

Then you can do your CV2 imshow loop as you normally would.

exchange = VideoStream(0).start()
ocr = OCR().start()
ocr.set_exchange(exchange)

 while True:  # Begins a loop for the real-time OCR display
        pressed_key = cv2.waitKey(1) & 0xFF
        if pressed_key == ord('q'):
            stop_stream_ocr(exchange, ocr)
            break

        frame = exchange.frame 

        cv2.imshow("Video Get Frame", frame)
        cps1.increment()

Note that you can't really control what thread CV2 decides to use in its internal workings, but this approach will allow you to display your webcam at its natural fps and while image processing happens in the background.

🌐
OpenCV Q&A Forum
answers.opencv.org › question › 232802 › accelerate-opencv-functions-on-gpu-through-cuda
accelerate OpenCV functions on gpu through CUDA - OpenCV Q&A Forum
July 23, 2025 - The best solution I've found is to translate my python algorithm in c++, and then use the accelerated functions provided in cv::cuda.
🌐
Stack Overflow
stackoverflow.com › questions › 46848869 › how-to-make-execution-speed-faster-python-opencv
How to make execution speed faster (python opencv) - Stack Overflow
October 20, 2017 - I wrote a code which devide a image in two part and repeat this action with other images which is in same folder. However, execution speed is very slowlly. Do you have any idea that the speed make ...
🌐
GitHub
github.com › opencv › opencv-python › issues › 481
How to speed up make. · Issue #481 · opencv/opencv-python
May 7, 2021 - I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
Author: opencv
Top answer
1 of 2
6

In the docs it is stated:

Note

This function should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)

In cv2.waitKey(X) function X means the number of milliseconds for an image to be displayed on the screen. In your case it is set to 1, so theoretically you are able to achieve 1000 fps (frames per seconds). But frame decoding takes time in VideoCapture object and limits your framerate. To change the playback speed you need to declare variable and use it as a parameter in waitKey function.

import cv2

cap = cv2.VideoCapture('video.mp4')
frameTime = 10 # time of each frame in ms, you can add logic to change this value.
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(frameTime) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Alternatively, as frame decoding is the most time consuming task you can move it to the second thread and use a queue of decoded frames. See this link for details.

The third approach is to separate grabbing and decoding process and simply decode every nth frame. That will result in displaying only a subset of frames from the source video but from the user perspective the video will be played faster.

import cv2

cap = cv2.VideoCapture('video.mp4')
i=0 #frame counter
frameTime = 1 # time of each frame in ms, you can add logic to change this value.
while(cap.isOpened()):
    ret = cap.grab() #grab frame
    i=i+1 #increment counter
    if i % 3 == 0: # display only one third of the frames, you can change this parameter according to your needs
        ret, frame = cap.retrieve() #decode frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(frameTime) & 0xFF == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()
2 of 2
2

You can use ffmpeg to speed up (or slow down) your video like fast forwarding by using "presentation time stamps".

For speed up an example would be:

ffmpeg -i YOUR_INPUT_MOVIE.mp4 -vf  "setpts=0.20*PTS" YOUR_OUTPUT_MOVIE.mp4

which will speed up your movie by 5x.

For slow down an example would be:

ffmpeg -i YOUR_INPUT_MOVIE.mp4 -vf  "setpts=5*PTS" YOUR_OUTPUT_MOVIE.mp4

which will slow down your movie by 5x.

Note: this method will drop frames.

🌐
Towards Data Science
towardsdatascience.com › home › latest › lightning fast video reading in python
Lightning Fast Video Reading in Python | Towards Data Science
March 28, 2021 - If you are working with high-resolution video, the speedups become more extreme, with almost double the speed in the IO Limited case. If you are working with many video files, you should check out my article on optimising this. OpenCV video reader is not well-suited to every situation; choose the right video reading library for best performance and results · If you want frame-accurate seeking, don't try and do this in OpenCV, use a library like decord. If you want to read videos efficiently in Python, use a library like VidGears/CamGears or imutils.
🌐
Stack Exchange
raspberrypi.stackexchange.com › questions › 28773 › how-do-i-increase-the-speed-of-my-python-opencv-program
cpu - How do I increase the speed of my Python OpenCV program? - Raspberry Pi Stack Exchange
March 18, 2015 - Specs: RPi 2, not overclocked, did rpi-upgrade, using ssh to run vnc on laptop. I've got a face tracking algorithm that is working fine on the Pi (and my PC). Problem is it has a lag of about 2 seconds on the Pi which is workable but not great. I'm running the program out of the lxterminal via python (2).