Performance Considerations
Real-time video processing must be fast enough to keep up with the frame rate. Understanding performance bottlenecks helps you optimise your CV pipelines.
8 min•By Priygop Team•Updated 2026
Optimising Video Processing
Optimising Video Processing
import cv2
import numpy as np
import time
def benchmark_operations(video_path, n_frames=100):
"""Benchmark different operations to identify bottlenecks."""
cap = cv2.VideoCapture(video_path)
times = {}
frames = []
for _ in range(n_frames):
ret, frame = cap.read()
if ret:
frames.append(frame)
cap.release()
if not frames:
print("No frames loaded")
return
print(f"Benchmarking on {len(frames)} frames...")
# Benchmark grayscale
t0 = time.time()
for f in frames:
cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
times["grayscale"] = (time.time() - t0) / len(frames) * 1000
# Benchmark resize
t0 = time.time()
for f in frames:
cv2.resize(f, (320, 240))
times["resize_320x240"] = (time.time() - t0) / len(frames) * 1000
# Benchmark Gaussian blur
t0 = time.time()
for f in frames:
cv2.GaussianBlur(f, (5, 5), 0)
times["gaussian_blur"] = (time.time() - t0) / len(frames) * 1000
# Benchmark Canny
grays = [cv2.cvtColor(f, cv2.COLOR_BGR2GRAY) for f in frames]
t0 = time.time()
for g in grays:
cv2.Canny(g, 50, 150)
times["canny_edges"] = (time.time() - t0) / len(frames) * 1000
print("\nOperation times (ms per frame):")
for op, ms in sorted(times.items(), key=lambda x: x[1]):
fps_cap = 1000 / ms if ms > 0 else float("inf")
print(f" {op:20s}: {ms:.2f} ms → max {fps_cap:.0f} FPS")
# Tips for optimising:
print("\nOptimisation strategies:")
tips = [
"Resize frames down before processing (640x480 instead of 1920x1080)",
"Process only every Nth frame for non-real-time tasks",
"Use grayscale when colour is not needed",
"Use cv2.INTER_AREA when shrinking (faster than INTER_CUBIC)",
"Enable GPU acceleration: net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)",
"Profile first — optimise only the bottleneck operation",
]
for tip in tips:
print(f" • {tip}")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment