视频安全性检测器 - 多源输入、统一帧采样与批量推理
输入列名 | 说明 |
|---|---|
videos | 输入视频列,类型依据 video_src_type(url/base64/binary) |
一个结构化结果数组,其中每个元素为视频的 NSFW 置信度分数;采样/推理失败或无有效帧则为 None
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
video_src_type | str | "video_url" | 输入视频的格式类型。可选值:["video_url", "video_base64", "video_binary"] |
model_path | str | "/opt/las/models" | 模型基础路径。 |
model_name | str | "Falconsai/nsfw_image_detection" | 模型名称/子目录。可选值:["Falconsai/nsfw_image_detection"] |
dtype | str | "float16" | 模型推理精度选择。可选值:["float16", "float32"] |
sample_mode | str | "by_count_uniform" | 采样模式。可选值:["by_count_uniform", "by_interval_time", "by_interval_frames", "by_fps", "by_timestamps"] |
start_time_sec | float | 0.0 | 采样起始时间(秒)。 |
end_time_sec | float or None | None | 采样结束时间(秒),None 表示到视频末尾。 |
count_k | int or None | None | 均匀采样帧数(by_count_uniform 使用)。 |
interval_sec | float or None | None | 时间间隔(秒,by_interval_time 使用)。 |
interval_frames | int or None | None | 解码帧间隔(by_interval_frames 使用)。 |
target_fps | float or None | None | 目标采样 FPS(by_fps 使用)。 |
timestamps_sec | list[float] or None | None | 采样时间戳列表(秒,by_timestamps 使用)。 |
max_frames | int or None | None | 返回帧上限,None 表示不限制。 |
reduce_mode | str | "avg" | 多帧聚合策略。可选值:["avg", "max", "min"] |
video_format | str | "mp4" | 二进制/base64 输入的视频格式。 |
batch_size | int | 16 | 批量推理的帧数量。 |
rank | int | 0 | GPU 编号。 |
下面的代码展示了如何使用 Daft(适用于分布式)运行算子对视频进行安全性检测。
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.context.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 │ # ╰────────────────────────────────┴──────────╯ 、、、