视频人脸模糊处理算子
输入列名 | 说明 |
|---|---|
video_paths | 视频文件路径列,支持本地、TOS、HTTP 等多种路径格式。与 video_binaries 二选一。 |
video_binaries | 视频二进制数据或 BASE64 字符串列。与 video_paths 二选一。 |
video_formats | 视频格式字符串列(如 "mp4"、"mov")。当输入为 video_binaries 时,必须提供此列以指定视频格式。 |
output_basenames | 输出文件基础名称列(不含扩展名),用于自定义输出文件的名称。 |
输出为字符串列,每个元素为模糊处理后的视频文件路径。
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
output_dir | str | "" | 将模糊处理后的视频保存到该目录中,目前支持上传到 TOS 或本地目录,格式:"tos://bucket/path/" 或 "/local/path/"。 |
model_path | str | "/opt/las/models" | 人脸检测模型的基础目录路径,用于 InsightFace 模型加载。 |
model_name | str | "insightface" | InsightFace 模型的子目录名称,最终模型路径为 model_path/model_name。 |
blur_type | str | "gaussian" | 模糊方式。可选值:"mean" (均值模糊), "box" (盒式模糊), "gaussian" (高斯模糊)。 |
radius | float | 10.0 | 模糊半径,仅对盒式模糊和高斯模糊生效。要求 radius >= 0,建议根据效果在 5-20 之间调整。 |
det_thresh | float | 0.5 | 人脸检测置信度阈值。用于隐私保护时,建议调低至 0.3-0.4 以避免漏检;用于精细修图时,建议调高至 0.6-0.7 以确保只处理正脸。 |
det_size | tuple[int, int] | (640, 640) | 人脸检测的输入尺寸 (width, height)。对于普通网络视频,默认值 (640, 640) 性价比高;对于监控录像等场景,建议调大至 (1280, 1280) 或更高,以检测远距离的小人脸。 |
下面的代码展示了如何使用 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 import VideoFaceBlur if __name__ == "__main__": # 更改完的视频会保存到指定的TOS路径下,因此,需要设置好环境变量以保证有权限写入TOS,包括:ACCESS_KEY,SECRET_KEY,TOS_ENDPOINT,TOS_REGION,TOS_TEST_DIR TOS_DIR = os.getenv("TOS_DIR", "tos_bucket") output_dir = f"tos://{TOS_DIR}/video/video_face_blur" 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/singer.mp4"]} ds = daft.from_pydict(samples) model_path = os.getenv("MODEL_PATH", "/opt/las/models") constructor_kwargs = { "output_dir": output_dir, "model_path": model_path, "blur_type": "gaussian", "radius": 20.0, } ds = ds.with_column( "results", las_udf( VideoFaceBlur, construct_args=constructor_kwargs, num_gpus=1, batch_size=1, concurrency=1, )(col("video_path")), ) ds.show() # ╭────────────────────────────────┬────────────────────────────────╮ # │ video_path ┆ results │ # │ --- ┆ --- │ # │ String ┆ String │ # ╞════════════════════════════════╪════════════════════════════════╡ # │ https://las-cn-beijing-public… ┆ tos://las-ai-qa-online/qa/tes… │ # ╰────────────────────────────────┴────────────────────────────────╯