# GPU-Accelerated Image Processing Functions in Deep-Live-Cam: A Complete Guide

> Explore GPU accelerated image processing functions like gpu_sharpen and gpu_resize in Deep-Live-Cam. Get faster processing with CUDA fallback.

- Repository: [Kenneth Estanislao/Deep-Live-Cam](https://github.com/hacksider/Deep-Live-Cam)
- Tags: api-reference
- Published: 2026-03-01

---

**Deep-Live-Cam provides drop-in GPU-accelerated replacements for common OpenCV image processing operations through its [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) wrapper, automatically falling back to CPU when CUDA is unavailable.**

The Deep-Live-Cam repository implements a hardware-acceleration abstraction layer that exposes GPU-accelerated image processing functions for real-time face swapping and video processing. These functions wrap OpenCV's CUDA API to deliver significant performance improvements while maintaining API compatibility with standard CPU-based OpenCV calls.

## How CUDA Acceleration Works in Deep-Live-Cam

### Automatic CUDA Detection

At import time, [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) attempts to instantiate `cv2.cuda.GpuMat` and verifies the presence of required CUDA functions including `createGaussianFilter`, `resize`, and `cvtColor`. If all dependencies are available, the module sets `CUDA_AVAILABLE` to **True**; otherwise, it silently falls back to CPU implementations. This detection occurs in lines 28-50 of the source file.

### The GPU Processing Wrapper Pattern

Each public function follows a consistent five-step execution pattern:

1. **Check `CUDA_AVAILABLE`** to determine execution path
2. **Upload** the NumPy array to a `cv2.cuda.GpuMat` object
3. **Execute** the CUDA-accelerated operation using OpenCV's cuda module
4. **Download** the result back to a NumPy array
5. **Fallback** to CPU OpenCV functions if any exception occurs or CUDA is unavailable

This design guarantees functional parity across hardware configurations while maximizing performance on CUDA-capable systems.

## Available GPU-Accelerated Image Processing Functions

Deep-Live-Cam exposes eight primary GPU-accelerated functions that serve as drop-in replacements for standard OpenCV operations:

### gpu_gaussian_blur

**Signature:** `gpu_gaussian_blur(src, ksize, sigma_x, sigma_y=0)`

Applies Gaussian blur using CUDA-accelerated convolution. The function automatically computes kernel size when `(0,0)` is passed for `ksize`. This corresponds to `cv2.GaussianBlur` in the CPU implementation.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 87-112

### gpu_sharpen

**Signature:** `gpu_sharpen(src, strength, sigma=3)`

Enhances image details using an unsharp mask technique. The function subtracts a Gaussian-blurred version from the original and blends the result based on the `strength` parameter. This is computationally expensive on CPU but executes efficiently on GPU.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 147-176

### gpu_add_weighted

**Signature:** `gpu_add_weighted(src1, alpha, src2, beta, gamma)`

Blends two images using the formula `dst = src1*alpha + src2*beta + gamma`. This GPU-accelerated version replaces `cv2.addWeighted` and is essential for alpha blending operations in the face-swapping pipeline.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 119-140

### gpu_resize

**Signature:** `gpu_resize(src, dsize, fx=0, fy=0, interpolation=cv2.INTER_LINEAR)`

Scales images to target dimensions using CUDA-accelerated resampling. Supports all standard interpolation flags including `INTER_NEAREST`, `INTER_LINEAR`, and `INTER_AREA`. This function is critical for normalizing input frame sizes before neural network inference.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 196-225

### gpu_cvt_color

**Signature:** `gpu_cvt_color(src, code)`

Converts between color spaces using GPU acceleration. Common conversions include `cv2.COLOR_BGR2RGB`, `cv2.COLOR_BGR2GRAY`, and `cv2.COLOR_BGR2BGRA`. This eliminates CPU bottlenecks during format conversions required by different processing stages.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 231-248

### gpu_flip

**Signature:** `gpu_flip(src, flip_code)`

Mirrors images vertically, horizontally, or both axes based on the `flip_code` parameter (`0` for vertical, `1` for horizontal, `-1` for both). This accelerates data augmentation and UI preview rendering.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 256-274

### is_gpu_accelerated

**Signature:** `is_gpu_accelerated()`

Returns **True** when the CUDA execution path is active and available for use. This utility function allows conditional logic in calling code to adjust parameters based on hardware capabilities.

**Source:** [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) lines 282-284

## Implementation Examples

The following example demonstrates the complete workflow using Deep-Live-Cam's GPU-accelerated image processing functions:

```python
import cv2
import numpy as np
from modules.gpu_processing import (
    gpu_gaussian_blur,
    gpu_sharpen,
    gpu_add_weighted,
    gpu_resize,
    gpu_cvt_color,
    gpu_flip,
    is_gpu_accelerated,
)

# Load source image

img = cv2.imread("portrait.jpg")  # BGR uint8 image

# 1. GPU-accelerated Gaussian blur

blurred = gpu_gaussian_blur(img, (5, 5), sigma_x=1.5)

# 2. GPU-accelerated sharpening

sharpened = gpu_sharpen(blurred, strength=0.7, sigma=2)

# 3. GPU-accelerated alpha blending

blended = gpu_add_weighted(img, 0.6, sharpened, 0.4, gamma=0)

# 4. GPU-accelerated resize to half resolution

half_size = (img.shape[1] // 2, img.shape[0] // 2)
resized = gpu_resize(blended, dsize=half_size, interpolation=cv2.INTER_AREA)

# 5. GPU-accelerated color conversion (BGR → RGB)

rgb = gpu_cvt_color(resized, cv2.COLOR_BGR2RGB)

# 6. GPU-accelerated horizontal flip

flipped = gpu_flip(rgb, flip_code=1)

# Verify execution path

print("GPU path active:", is_gpu_accelerated())

```

## Integration Points in the Codebase

Deep-Live-Cam utilizes these GPU-accelerated image processing functions across several critical modules:

**[`modules/ui.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/ui.py)** – The user interface rendering pipeline employs `gpu_cvt_color`, `gpu_resize`, and `gpu_flip` to accelerate live preview generation and frame normalization before display.

**[`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py)** – The core face-swapping processor demonstrates heavy usage of `gpu_sharpen`, `gpu_gaussian_blur`, `gpu_add_weighted`, and `gpu_resize` during real-time face blending and enhancement operations.

**[`modules/processors/frame/face_masking.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_masking.py)** – Mask generation and smoothing operations utilize `gpu_gaussian_blur` and `gpu_resize` to process alpha channels and edge softening at video frame rates.

**[`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py)** – Frame preprocessing for neural network inference applies `gpu_cvt_color` when preparing target images for model input, ensuring minimal latency between capture and prediction.

## Summary

- **Deep-Live-Cam** provides eight GPU-accelerated image processing functions in [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) that serve as drop-in replacements for standard OpenCV operations.
- **Automatic fallback** to CPU implementations occurs when CUDA is unavailable, ensuring code portability across heterogeneous hardware environments.
- **Key functions** include `gpu_sharpen`, `gpu_resize`, `gpu_gaussian_blur`, `gpu_add_weighted`, `gpu_cvt_color`, and `gpu_flip`, each wrapping `cv2.cuda` operations with NumPy-compatible interfaces.
- **Integration** spans the UI layer, face-swapping processors, masking modules, and prediction pipelines, accelerating critical real-time video processing paths.

## Frequently Asked Questions

### What GPU-accelerated image processing functions are available in Deep-Live-Cam?

Deep-Live-Cam exposes eight primary functions: `gpu_gaussian_blur` for smoothing, `gpu_sharpen` for detail enhancement, `gpu_add_weighted` for alpha blending, `gpu_resize` for scaling, `gpu_cvt_color` for color space conversion, `gpu_flip` for mirroring, and `is_gpu_accelerated` for runtime capability detection. These are implemented in [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) as CUDA wrappers around OpenCV functions.

### How does Deep-Live-Cam handle systems without CUDA support?

The framework implements automatic CPU fallback through a try-except pattern wrapped around CUDA availability checks. When [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) imports, it attempts to instantiate `cv2.cuda.GpuMat` and verify required functions exist. If this fails, `CUDA_AVAILABLE` remains False, and all GPU functions route to their standard OpenCV CPU equivalents, ensuring identical output quality regardless of hardware.

### Which modules in Deep-Live-Cam use GPU acceleration?

The GPU-accelerated functions integrate across several critical components: [`modules/ui.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/ui.py) uses `gpu_cvt_color`, `gpu_resize`, and `gpu_flip` for preview rendering; [`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py) employs `gpu_sharpen`, `gpu_gaussian_blur`, and `gpu_add_weighted` for real-time face blending; [`modules/processors/frame/face_masking.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_masking.py) utilizes blur and resize operations for mask smoothing; and [`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py) applies color conversion for neural network input preparation.

### What is the performance benefit of using gpu_sharpen versus CPU sharpening?

The `gpu_sharpen` function implements unsharp masking using CUDA-accelerated Gaussian blur and weighted addition operations. While the exact performance delta depends on GPU model and image resolution, CUDA implementations typically provide 5-10x speedup for convolution operations compared to CPU processing. This acceleration is critical in Deep-Live-Cam's real-time face swapping pipeline, where `gpu_sharpen` processes video frames at 30+ FPS that would otherwise create bottlenecks during CPU execution.