# How the NSFW Filter Works in Deep‑Live‑Cam: Integration and Content Detection Explained

> Discover how the Deep-Live-Cam NSFW filter integrates and detects adult content using opennsfw2. Learn about its CLI flags global state and runtime guards for automatic blocking.

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

---

**The NSFW filter in Deep‑Live‑Cam uses the opennsfw2 library to automatically block adult content through a three‑layer integration spanning CLI flags, global state, and runtime guards.**

Deep‑Live‑Cam includes an optional **NSFW (Not‑Safe‑For‑Work) filter** that prevents the processing of adult or inappropriate visual content. This safety mechanism is woven directly into the application architecture to protect both batch operations and live webcam previews. According to the hacksider/Deep‑Live‑Cam source code, the filter operates via a coordinated system of command‑line arguments, global configuration variables, and real‑time detection checks.

## Three-Layer Integration Architecture

The filter is implemented across three distinct layers to ensure comprehensive coverage regardless of how the application is launched or controlled.

### Command-Line and Configuration Flag

The entry point for enabling the filter starts in **[`modules/core.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/core.py)**, where the argument parser adds a `--nsfw-filter` switch with `dest='nsfw_filter'`. When this flag is present at launch, the code sets `modules.globals.nsfw_filter` to `True`.

```bash

# Enable NSFW filtering from the command line

python run.py -s source.png -t target.mp4 --nsfw-filter

```

This boolean value persists throughout the session and serves as the authoritative configuration for all downstream processing.

### Global State Toggle

The single source of truth for the filter state resides in **[`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py)**, which defines the `nsfw_filter` boolean variable. This global flag is consulted by both the processing engine and the user interface. While the current UI implementation has the switch commented out, the architecture supports a `CTkSwitch` that writes to this variable, with persistence handled via [`switch_states.json`](https://github.com/hacksider/Deep-Live-Cam/blob/main/switch_states.json) through the `save_switch_states` and `load_switch_states` functions in **[`modules/ui.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/ui.py)**.

### Runtime Processing Guard

Before any image or video frame enters the processing pipeline, the application invokes `ui.check_and_ignore_nsfw`. This guard function appears in critical paths such as `modules/ui.py:update_preview` and `modules/core.py:start`. If the global flag is enabled and the content triggers the NSFW detector, the function aborts the operation, optionally destroying the UI and displaying *“Processing ignored!”* to the user.

## Content Detection Capabilities

The actual classification logic lives in **[`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py)**, which wraps the third‑party `opennsfw2` library to provide three distinct detection modalities.

### Image Analysis

The `predict_image(path)` function evaluates a single image file by calling `opennsfw2.predict_image` and comparing the returned probability against a `MAX_PROBABILITY` threshold of **0.85**. If the model predicts a probability exceeding this value, the function returns `True`, flagging the content as NSFW.

### Video Sampling

For video files, `predict_video(path)` utilizes `opennsfw2.predict_video_frames` with a `frame_interval` of **100 milliseconds**. The function samples frames throughout the video duration, checking each against the 0.85 probability threshold. If **any** sampled frame exceeds the threshold, the entire video is classified as NSFW and processing is blocked.

### Real-Time Frame Inspection

The `predict_frame(frame)` function enables live detection on individual NumPy arrays (BGR format) captured from webcams. This method optionally applies color correction, converts the frame to a PIL image, preprocesses it using `opennsfw2.Preprocessing.YAHOO`, and evaluates it against a cached model instance (`opennsfw2.make_open_nsfw_model`). This capability allows the UI preview to abort instantly when inappropriate content appears in the camera feed.

## Implementation Details and Thresholds

The detection system relies on a fixed probability threshold of **0.85** defined as `MAX_PROBABILITY` in the predicter module. This conservative setting minimizes false negatives while maintaining reasonable performance for real‑time applications. The underlying `opennsfw2` model handles the heavy lifting of content classification, allowing Deep‑Live‑Cam to focus on integration rather than model training.

## Code Examples

Enable the filter programmatically for custom integrations:

```python
import modules.globals as G

# Activate NSFW protection globally

G.nsfw_filter = True

```

Manually invoke detection on specific files:

```python
from modules import predicter

# Check a single image

if predicter.predict_image("uploaded_photo.jpg"):
    print("NSFW content detected – operation cancelled")

# Verify an entire video

if predicter.predict_video("target_clip.mp4"):
    print("Video contains inappropriate frames – skipping processing")

```

Implement live-preview guards in custom UIs:

```python
from modules import ui, globals as G
import cv2

# Capture frame from webcam

frame = cv2.imread("webcam_capture.jpg")

# Check before processing

if G.nsfw_filter and ui.check_and_ignore_nsfw(frame):
    # Frame flagged as NSFW – halt processing pipeline

    pass

```

## Summary

- **CLI Integration**: The `--nsfw-filter` flag in [`modules/core.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/core.py) initializes the protection system at startup.
- **Global State**: [`modules/globals.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/globals.py) maintains the `nsfw_filter` boolean as the single source of truth across the application.
- **Detection Backend**: [`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py) wraps `opennsfw2` to provide `predict_image`, `predict_video`, and `predict_frame` functions.
- **Probability Threshold**: Content exceeding 0.85 probability is classified as NSFW and blocked from processing.
- **Video Sampling**: Videos are checked every 100ms; any offending frame triggers a block on the entire file.
- **Runtime Guards**: `check_and_ignore_nsfw` in [`modules/ui.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/ui.py) intercepts inappropriate content before it reaches the processing pipeline.

## Frequently Asked Questions

### How do I enable the NSFW filter in Deep‑Live‑Cam?

You can enable the filter by adding the `--nsfw-filter` flag when launching the application from the command line. The flag is parsed in [`modules/core.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/core.py) and sets `modules.globals.nsfw_filter` to `True`, activating protection for the entire session.

### What types of content can the filter detect?

The filter can detect adult or inappropriate content in three formats: static images via `predict_image`, video files via `predict_video` (which samples frames every 100ms), and live camera frames via `predict_frame`. Any content exceeding the 0.85 probability threshold is flagged as NSFW.

### Which library performs the actual content classification?

Deep‑Live‑Cam delegates the detection logic to the `opennsfw2` library. The [`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py) file wraps this library to provide a consistent interface for images, videos, and real‑time frames while managing model caching and preprocessing.

### Can I adjust the sensitivity of the NSFW filter?

The sensitivity is controlled by the `MAX_PROBABILITY` constant, which is hardcoded to 0.85 in [`modules/predicter.py`](https://github.com/hacksider/Deep-Live-Cam/blob/main/modules/predicter.py). To adjust sensitivity, you would need to modify this threshold value in the source code, though doing so may increase false positives or negatives depending on your adjustment direction.