Skip to content

Uncategorized

Increase OpenCV speed by 2x with Python and Multithreading | Tutorial

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

In this tutorial, we will explore how to accelerate OpenCV to process more than 500 frames per second (FPS). But first, why should we focus on speeding up OpenCV? Faster frame processing is crucial when working with video applications like object detection, object segmentation, and similar tasks that are time-intensive. The quicker we can process frames, the more efficient the overall system becomes, saving both time and computational resources.

Efficiency means lower operational costs for businesses relying on servers to handle such tasks. Slow processing could lead to buffer overloads and errors in industries such as CCTV monitoring, where multiple cameras send live footage. Therefore, speeding up OpenCV is not just a luxury but often a necessity. In this article, we’ll walk through code examples and use multi-threading to significantly improve processing speed.

Understanding the Basics: A Linear Approach

Let’s start by looking at the basics of OpenCV Multithreading, a linear method for capturing and displaying video frames using OpenCV. Below is a simple Python code snippet that illustrates this:

import cv2

# Create a capture object to load the video
cap = cv2.VideoCapture('sea.mp4')

while True:
    ret, frame = cap.read()  # Capture frame
    if not ret:
        break  # End video when no frames are left
    cv2.imshow('Frame', frame)  # Display the frame
    if cv2.waitKey(1) == 27:  # Exit when 'ESC' key is pressed
        break

cap.release()
cv2.destroyAllWindows()

This code captures frames from a video file (sea.mp4), displays them, and closes when the video ends or the user presses the ESC key. It’s straightforward but not optimized for speed. To understand how fast this code runs, we can add a feature to calculate the FPS:

# Import necessary utilities
from gui_utils import GUIUtils  # A custom utility to calculate FPS

# Initialize FPS calculation
gu = GUIUtils()

# Inside the loop, before displaying the frame
frame = gu.show_fps(frame)

Running this setup typically achieves around 300 FPS. However, because the process of capturing and displaying frames happens in sequence (one after the other), we can optimize it further.

opencv low fps

Improving Speed with OpenCV Multi-threading

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. Multi-threading allows these two tasks—frame capturing and frame displaying—to happen simultaneously. This parallelism is the key to faster performance.

Here’s a revised version of the code, using multi-threading:

import cv2
from gui_utils import GUIUtils
from cap_multithreading import CapMultiThreading

gu = GUIUtils()

cap = CapMultiThreading("sea.mp4")

while True:
    ret, frame = cap.get_frame()
    if not ret:
        break

    frame = gu.show_fps(frame)
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()

In this multi-threaded approach, frame capturing and displaying occur simultaneously, effectively reducing bottlenecks. This code achieves 500–600 FPS, a significant improvement over the initial linear method, nearly doubling the speed.

Opencv hight speed fps

How Multi-threading Works

The core of this speed boost lies in how multi-threading is set up. The CapMultiThreading class works by constantly loading frames into a buffer while the system is busy displaying others. Here’s a quick breakdown of what’s happening:

  1. Frame Buffering: Instead of waiting to grab a new frame when the display finishes, the frames are preloaded into a memory buffer.
  2. Parallel Execution: The frame capture thread runs in parallel with the display process, allowing both to happen at the same time.
  3. Reduced Latency: Since frames are preloaded, the program does not experience any downtime waiting for frames to be fetched from the video file, leading to faster performance.

In the CapMultiThreading class, frames are stored in a buffer (e.g., 128 frames). The frame displayed comes directly from this buffer, which is constantly being filled by a separate thread. This eliminates the usual waiting time between frames, dramatically improving FPS.

Use Cases Beyond Basic Video Processing

This tutorial uses a basic video file to demonstrate OpenCV speed-up techniques, but the same principles apply to more advanced tasks like object detection, segmentation, or even real-time keypoint recognition. When using these advanced tasks, incorporating GPU acceleration (such as CUDA) will provide even more significant performance improvements. In projects like Traffic Survey, this type of optimization is essential

Download Code and Resources

The code and the required files for OpenCV Multithreading (e.g., CapMultiThreading.py and GUIutils.py) are available for download from the link below.

Conclusion

We’ve seen that by implementing multi-threading, we can significantly speed up OpenCV, going from 300 FPS to over 500 FPS. This tutorial demonstrated a simple yet powerful way to optimize OpenCV’s performance, which can be extended to various computer vision tasks. If you’re dealing with large datasets or need to process videos efficiently, implementing these techniques will make a noticeable difference.