You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Video processing
Video safety detection
Copy page
Download PDF
Video safety detection

Operator introduction

Description

Video safety detector – multi-source input, unified frame sampling, and batch inference

Key features

  • Based on a pre-trained image classification model, performs NSFW probability detection on frames sampled from the video and outputs the final score according to the aggregation strategy.
  • Supports multiple video input sources:
  • URL path (video_url)
  • Base64 encoding (video_base64)
  • Binary stream (video_binary)
  • Supports multiple sampling methods (via VideoFrameSampler):
  • by_count_uniform / by_interval_time / by_interval_frames / by_fps / by_timestamps
  • Batch inference:
  • The inference batch size can be controlled via batch_size to improve throughput performance.

Application scenarios

  • Content safety review: Assess NSFW risk for user-uploaded videos.
  • Pre-filtering in production pipelines: Screen video safety before subsequent processing.

Caution

  • Only outputs numerical NSFW confidence (0~1); frame images are not saved.

Daft invocation

Operator parameters

Input

Input column name

Description

videos

Input video column; the type depends on video_src_type (url/base64/binary)

Output

A structured result array, where each element is the NSFW confidence score for a video; returns None if sampling/inference fails or no valid frames are available

Parameters

If a parameter does not have a default value, it is required

Parameter name

Type

Default value

Description

video_src_type

str

"video_url"

The format type of the input video. Optional values: ["video_url", "video_base64", "video_binary"]

model_path

str

"/opt/las/models"

Base path of the model.

model_name

str

"Falconsai/nsfw_image_detection"

Model name/subdirectory. Optional values: ["Falconsai/nsfw_image_detection"]

dtype

str

"float16"

Model inference precision selection. Optional values: ["float16", "float32"]

sample_mode

str

"by_count_uniform"

Sampling mode. Optional values: ["by_count_uniform", "by_interval_time", "by_interval_frames", "by_fps", "by_timestamps"]

start_time_sec

float

0.0

Sampling start time (seconds).

end_time_sec

float or None

None

Sampling end time (seconds); None means until the end of the video.

count_k

int or None

None

Number of uniformly sampled frames (used by by_count_uniform).

interval_sec

float or None

None

Time interval (seconds, used by by_interval_time).

interval_frames

int or None

None

Decoded frame interval (used by by_interval_frames).

target_fps

float or None

None

Target sampling FPS (used by by_fps).

timestamps_sec

list[float] or None

None

List of sampling timestamps (seconds, used by by_timestamps).

max_frames

int or None

None

Maximum number of returned frames; None means no limit.

reduce_mode

str

"avg"

Multi-frame aggregation strategy. Optional values: ["avg", "max", "min"]

video_format

str

"mp4"

Format of binary/base64 input video.

batch_size

int

16

Number of frames for batch inference.

rank

int

0

GPU index.

Examples

The following code demonstrates how to use Daft (for distributed scenarios) to run the operator for video safety detection.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.udf import las_udf
from daft.las.functions.video.video_nsfw_detect import VideoNsfwDetect

if __name__ == "__main__":
    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S.%s".format(),
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.set_runner_ray()

    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    samples = {
        "video_path": [
            f"https://{tos_dir_url}/public/shared_video_dataset/eating_56.mp4"
        ]
    }

    video_src_type = "video_url"
    model_path = os.getenv("MODEL_PATH", "/opt/las/models")
    model_name = "Falconsai/nsfw_image_detection"
    num_gpus = 0

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "nsfw",
        las_udf(
            VideoNsfwDetect,
            construct_args={
                "model_path": model_path,
                "video_src_type": video_src_type,
                "sample_mode": "by_count_uniform",
                "count_k": 3,
            },
            num_gpus=num_gpus,
            batch_size=1,
            concurrency=1,
        )(col("video_path")),
    )

    ds.show()

    # ╭────────────────────────────────┬──────────╮
    # │ video_path                     ┆ nsfw     │
    # │ ---                            ┆ ---      │
    # │ Utf8                           ┆ Float64  │
    # ╞════════════════════════════════╪══════════╡
    # │ https://las-cn-beijing-public… ┆ 0.000684 │
    # ╰────────────────────────────────┴──────────╯
、、、
Last updated: 2026.05.12 19:06:35