# Opacity Blending in Deep-Live-Cam: How Face Swapping Transparency Works

> Discover how Deep-Live-Cam achieves natural face swapping with weighted opacity blending. Learn about the GPU-accelerated algorithm and transparency values used for seamless integration.

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

---

**Deep-Live-Cam blends swapped faces using a weighted opacity algorithm that combines the original frame with the processed output using values from 0.0 (fully transparent) to 1.0 (fully opaque), with GPU-accelerated `cv2.addWeighted` operations.**

Deep-Live-Cam implements **opacity blending** as the final post-processing step in its face swapping pipeline. This mechanism controls how aggressively the generated face overlays the original video frame, allowing users to fine-tune the realism of deepfake outputs through a single global parameter stored in [`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py).

## How Opacity Blending Works in Deep-Live-Cam

The opacity system operates through a five-stage pipeline that preserves the original frame, executes the swap, and performs alpha blending only when necessary.

### Global Opacity Configuration

The blending intensity is controlled by `modules.globals.opacity`, defined in [`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py) at line 57. This floating-point value defaults to `1.0` and is clamped to the range `[0.0, 1.0]` at runtime to prevent invalid blending weights.

```python
opacity = getattr(modules.globals, "opacity", 1.0)
opacity = max(0.0, min(1.0, opacity))   # clamp to valid range

```

### Frame Preservation Strategy

Before any face swapping occurs, the processor saves the original frame conditionally. In [`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py) (lines 139-143), the code creates a copy only when opacity is less than `1.0`, avoiding unnecessary memory overhead for full-opacity swaps.

```python
original_frame = temp_frame if opacity >= 1.0 else temp_frame.copy()

```

### The Blending Algorithm

After the face swap model executes and optional mouth-masking or Poisson blending completes, the system combines the `original_frame` and `swapped_frame` using weighted addition. The algorithm applies the formula:

```

final = original × (1 - opacity) + swapped × opacity

```

This operation occurs in [`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py) following the post-processing steps, utilizing the preserved original frame and the processed output.

### GPU Acceleration

Deep-Live-Cam prioritizes CUDA acceleration for the blending operation. The `gpu_add_weighted` function in [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) (lines 19-26) wraps OpenCV's `cv2.addWeighted` with GPU memory management, falling back to CPU processing when CUDA is unavailable.

```python
final_swapped_frame = gpu_add_weighted(
    original_frame.astype(np.uint8), 1 - opacity,
    swapped_frame.astype(np.uint8),   opacity,
    0)

```

## Implementation Details and Code Examples

Developers can manipulate opacity programmatically or through the UI slider. The following examples demonstrate practical usage:

```python

# Adjust opacity programmatically before processing

from modules import globals

globals.opacity = 0.6  # 60% visibility of swapped face

# Process a single frame with custom opacity

from modules.processors.frame.face_swapper import swap_face
from modules.face_analyser import detect_faces

source_face = detect_faces(source_image)[0]
target_face = detect_faces(target_image)[0]
result = swap_face(source_face, target_face, target_image.copy())

```

## Performance Optimizations

The implementation includes early-exit shortcuts to minimize computational overhead:

- **Full Opacity (`opacity >= 1.0`)**: The system returns the `swapped_frame` directly without performing the weighted blend, eliminating the `cv2.addWeighted` call entirely.
- **Zero Opacity (`opacity == 0.0`)**: The face swapping pipeline is bypassed completely; `process_frame` and `process_frame_v2` return the untouched input frame immediately (see [`face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/face_swapper.py) lines 75-81 and 111-116).

These optimizations ensure that adjusting opacity to extreme values does not introduce unnecessary processing latency.

## Summary

- **Opacity blending** in Deep-Live-Cam combines original and swapped frames using a weighted sum controlled by `modules.globals.opacity`.
- The **blending formula** applies `original × (1 - opacity) + swapped × opacity` using GPU-accelerated `cv2.addWeighted` where available.
- **Performance shortcuts** eliminate blending operations when opacity is `0.0` or `1.0`, returning frames directly without weighted combination.
- Key implementation files include [`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py) for configuration, [`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py) for execution logic, and [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py) for CUDA acceleration.

## Frequently Asked Questions

### How do I adjust the opacity setting in Deep-Live-Cam?

You can modify the opacity value through the UI slider, which updates `modules.globals.opacity` in real-time, or programmatically by importing `modules.globals` and setting `globals.opacity` to a float between `0.0` and `1.0`. Values closer to `0.0` make the swapped face more transparent, while `1.0` shows only the swapped result.

### What happens when I set opacity to 0.0 or 1.0?

When opacity is set to `0.0`, Deep-Live-Cam bypasses the entire face swapping pipeline and returns the original frame untouched, effectively disabling the deepfake effect. When set to `1.0`, the system skips the blending calculation entirely and outputs only the swapped face without any transparency mixing.

### Does opacity blending use GPU acceleration?

Yes, Deep-Live-Cam utilizes CUDA-accelerated blending through the `gpu_add_weighted` function in [`modules/gpu_processing.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/gpu_processing.py), which wraps OpenCV's `cv2.addWeighted` for GPU execution. If CUDA is unavailable, the system automatically falls back to CPU-based `cv2.addWeighted` processing.

### Where is the opacity value stored in the codebase?

The global opacity configuration resides in [`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py) at line 57, where it is defined as a module-level variable defaulting to `1.0`. This value is accessed and clamped to the valid range `[0.0, 1.0]` within [`modules/processors/frame/face_swapper.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/processors/frame/face_swapper.py) before being applied to the blending operation.