Skip to main content

Overview

GPU acceleration using NVIDIA CUDA dramatically improves inference performance:
  • CPU inference: ~100-300ms per frame (YOLOv4)
  • GPU inference: ~10-30ms per frame (YOLOv4)
  • Speedup: 5-10x faster
GPU acceleration is automatic when CUDA is available. No code changes required.

Benefits of GPU Acceleration

Faster Detection

10x speedup means more frequent detection or processing more streams.

Lower frame_skip

Process every 5-10 frames instead of every 30 frames.

More Streams

Handle 12-16 cameras instead of 4-8 with acceptable performance.

Better Responsiveness

Detect persons entering frame within 0.5 seconds instead of 2-3 seconds.

How GPU Detection Works

RTSP Human Capture automatically detects and uses CUDA GPUs:

GPU Detection Logic

From person_detector.py:62-72:
1

Check CUDA device count

Returns number of CUDA-capable GPUs detected by OpenCV.
2

Configure DNN backend

If CUDA available:
If not:
3

Run inference

Automatically uses GPU if configured, CPU otherwise.
No configuration needed! If you have CUDA installed and CUDA-enabled OpenCV, GPU acceleration is automatic.

Requirements

To enable GPU acceleration, you need:
Compatible GPUs:
  • NVIDIA GTX 10-series or newer
  • NVIDIA RTX 20/30/40-series
  • NVIDIA Tesla/Quadro data center GPUs
  • Compute Capability 3.5 or higher
Check your GPU:
Expected output:
Supported versions:
  • CUDA 11.2 or newer
  • CUDA 12.x recommended
Check CUDA version:
Expected output:
Install CUDA Toolkit:
This is the critical requirement! Standard OpenCV doesn’t include CUDA support.You need opencv-contrib-python compiled with CUDA.

Installing CUDA-enabled OpenCV

Standard OpenCV from PyPI does NOT include CUDA support. You have three options: Use pre-compiled wheels from the opencv-python-cuda-wheels project:
1

Download appropriate wheel

Visit: https://github.com/cudawarped/opencv-python-cuda-wheels/releases/latestSelect wheel matching:
  • Your Python version (e.g., cp312 = Python 3.12)
  • Your platform (e.g., linux_x86_64)
  • Your CUDA version (e.g., cuda122 = CUDA 12.2)
Example filename:
2

Create deps directory

3

Download wheel

Replace with your specific wheel URL.
4

Install with uv

5

Verify CUDA support

Expected output:
If you see 0, CUDA is not available.
The pyproject.toml in this repository is configured to look for CUDA wheels in the deps/ directory.

Option 2: Build from Source

Build OpenCV with CUDA support yourself:
This is time-consuming (1-2 hours) and error-prone. Only recommended if pre-built wheels don’t work.
1

Install build dependencies

2

Clone OpenCV repositories

3

Configure with CMake

Replace CUDA_ARCH_BIN with your GPU’s compute capability:
  • RTX 3060/3070/3080/3090: 8.6
  • RTX 4060/4070/4080/4090: 8.9
  • RTX 2060/2070/2080: 7.5
  • GTX 1060/1070/1080: 6.1
Check your GPU: https://developer.nvidia.com/cuda-gpus
4

Build (this takes 1-2 hours)

5

Verify installation

Should show CUDA-related build flags.

Option 3: Docker with CUDA

Use NVIDIA’s official CUDA container:
Dockerfile
Requires nvidia-docker2 or NVIDIA Container Toolkit installed on host.

Verifying GPU Acceleration

Check 1: CUDA Device Count

Expected output:
OpenCV detects your GPU. GPU acceleration will work.

Check 2: Application Output

Run the application and look for the startup message:
With GPU:
Without GPU:

Check 3: GPU Utilization

Monitor GPU usage during processing:
Expected output:
Key metrics:
  • GPU-Util: Should be 20-50% during inference
  • Memory-Usage: ~300-500 MB for YOLOv4
  • Power: Should increase when processing
If GPU-Util stays at 0%, GPU is not being used despite CUDA being available.

Check 4: Performance Benchmark

Compare inference times:
Disable GPU temporarily:
Run and observe frame processing times in console output.Expected: Detection messages every 1-3 seconds (with frame_skip=15)

Performance Comparison

Single Stream

Times measured on:
  • CPU: Intel i7-9700K @ 3.6GHz
  • GPU: NVIDIA RTX 3060 12GB
  • Resolution: 1920×1080

Multi-Stream Scalability

With frame_skip=15 (2 fps detection rate):
CPU bottleneck: With >3 streams on CPU, frame_skip must be increased to 30+ for usable performance.GPU bottleneck: With >12 streams on mid-range GPU, consider lowering frame_skip or using multiple instances.

Optimizing GPU Performance

1. Adjust Batch Size

YOLO processes one frame at a time. For multi-stream, this is actually optimal since:
  • Threads queue up at the inference lock
  • GPU processes frames sequentially
  • No benefit to batching in this architecture

2. Lower frame_skip for GPU

With GPU, you can afford more frequent detection:
Result:
  • 3x more frequent detection
  • Still faster than CPU at frame_skip=30
  • Better responsiveness

3. Monitor GPU Memory

Each model loaded into GPU memory: Plus per-frame buffers:
  • 1920×1080: ~8 MB per frame
  • Intermediate layers: ~50-100 MB
Total: ~350-450 MB for single instance
Running multiple instances? Ensure total GPU memory usage < 80% of available VRAM:
  • RTX 3060 (12GB): Can run 20+ instances
  • GTX 1660 (6GB): Can run 10+ instances

4. Use Appropriate CUDA Arch

When building OpenCV from source, match CUDA_ARCH_BIN to your GPU:
Mismatch causes performance loss (10-30% slower).

Troubleshooting

Issue: OpenCV doesn’t detect GPUDiagnosis:
  1. Check NVIDIA driver:
    Should show GPU info. If not, driver not installed.
  2. Check CUDA toolkit:
    Should show CUDA version. If not, toolkit not installed.
  3. Check OpenCV build:
    Should show CUDA-related flags. If not, OpenCV not built with CUDA.
Solution:
  • Install NVIDIA driver
  • Install CUDA toolkit
  • Install/build CUDA-enabled OpenCV
Issue:
Even though cv2.cuda.getCudaEnabledDeviceCount() returns > 0.Cause: OpenCV DNN module built without CUDA support (need OPENCV_DNN_CUDA=ON).Verify:
Should show:
Solution: Use pre-built wheels from opencv-python-cuda-wheels (they have DNN CUDA enabled).
Error:
Causes:
  • GPU doesn’t have enough VRAM
  • Multiple applications using GPU
  • Memory leak
Solutions:
  1. Check available memory:
  2. Close other GPU applications (Chrome, games, etc.)
  3. Use smaller model (YOLOv3-tiny instead of YOLOv4)
  4. Process fewer streams
Issue: GPU-Util in nvidia-smi shows less than 10%Causes:
  • Not enough streams (GPU waiting for CPU)
  • frame_skip too high
  • Display rendering is bottleneck
Solutions:
  1. Lower frame_skip:
  2. Add more streams: GPU can handle 8-16 streams efficiently
  3. Disable display:
Issue: GPU not providing expected speedupCheck:
  1. GPU actually being used:
    GPU-Util should be >0% and spike during detection.
  2. Power mode:
    Should show “P2” or “P0” (performance mode), not “P8” (idle).
  3. Thermal throttling: Check temperature in nvidia-smi. If >80°C, may be throttling.
  4. CUDA architecture mismatch: Rebuild OpenCV with correct CUDA_ARCH_BIN for your GPU.

GPU Selection (Multi-GPU Systems)

If you have multiple GPUs, OpenCV uses GPU 0 by default. To select a different GPU:
For multiple instances across GPUs:

Best Practices

Use Pre-built Wheels

Easier and more reliable than building from source. Get from opencv-python-cuda-wheels.

Monitor GPU Usage

Keep nvidia-smi running in a separate terminal to watch GPU utilization.

Update Drivers Regularly

Newer NVIDIA drivers often include performance improvements.

Match CUDA Versions

Ensure OpenCV CUDA version matches installed CUDA toolkit version.

Test Before Deploying

Verify GPU acceleration works with test streams before production deployment.

Plan for Scaling

GPU allows 3-5x more streams than CPU. Plan hardware accordingly.

Budget Setup ($300-500)

GPU: NVIDIA GTX 1660 Super (6GB)
  • 4-8 streams at 1080p
  • 2 fps detection rate
  • YOLOv4

Mid-Range Setup ($500-800)

GPU: NVIDIA RTX 3060 (12GB)
  • 8-12 streams at 1080p
  • 2-3 fps detection rate
  • YOLOv4
  • Room for growth

High-End Setup ($1000+)

GPU: NVIDIA RTX 4070 (12GB) or RTX 3080 (10GB)
  • 12-16 streams at 1080p
  • 3-5 fps detection rate
  • YOLOv4
  • Multiple instances possible

Enterprise/Data Center

GPU: NVIDIA A4000/A5000 or Tesla T4
  • 16-24 streams at 1080p
  • 5+ fps detection rate
  • ECC memory
  • 24/7 reliability
All recommendations assume:
  • 1920×1080 resolution streams
  • YOLOv4 model
  • frame_skip tuned appropriately

Docker GPU Setup

For containerized deployments with GPU:

Install NVIDIA Container Toolkit

Test GPU Access

Should show your GPU info.

Run Application with GPU

Next Steps

Multi-Stream Processing

Leverage GPU to process many streams

Configuration Tuning

Optimize settings for GPU performance

Model Setup

Configure YOLO models

Single Stream

Test GPU with single stream first