图片人脸模糊处理算子
输入列名 | 说明 |
|---|---|
images | 输入图片列。根据 |
images_name | 可选列,图片的逻辑名称或标识,用于生成输出文件名的前缀,不需包含后缀名。 |
处理结果的数组,每个元素包含:
base64 (str | None): 模糊后图片的 Base64 编码。image_path (str | None): 模糊后图片的本地或 TOS 路径;若保存失败或未设置保存,则为 None。face_bounding_boxes (list[tuple[int, int, int, int]] | None): 检测到的人脸矩形框列表 (x, y, w, h);若未检测到人脸或处理失败,则为 None。如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
output_dir | str | "" | 将模糊处理后的图片保存到的 TOS 目录或者本地路径,格式:"tos://bucket/path/" 或 "/local/path/"。 |
model_path | str | "/opt/las/models" | 人脸检测模型的基础目录路径。 |
model_name | str | "insightface" | InsightFace 模型子目录名称,与 |
image_src_type | str | "image_url" | 输入图片的数据类型。可选值:"image_url"、"image_base64"、"image_binary"。 |
blur_type | str | "gaussian" | 人脸区域的模糊方式。可选值:"mean"、"box"、"gaussian"。 |
radius | float | 10.0 | 模糊半径,用于 "box" 和 "gaussian" 模式,要求 |
det_thresh | float | 0.5 | 人脸检测置信度阈值。 |
det_size | tuple[int, int] | (640, 640) | 人脸检测的输入尺寸 (width, height)。 |
return_base64 | bool | False | 是否在输出中包含图片的 Base64 编码。 |
return_image_format | str | "png" | 输出图片的格式。可选值:"png"、"jpg"、"jpeg"。 |
下面的代码展示了如何使用 daft 运行算子对图像中的人脸进行模糊化处理。
from __future__ import annotations import os import daft from daft import col from daft.las.functions.image import ImageFaceBlur from daft.las.functions.udf import las_udf 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 = {"image_path": [f"https://{tos_dir_url}/public/shared_image_dataset/mengnalisa.png"]} ds = daft.from_pydict(samples) model_path = os.getenv("MODEL_PATH", "/opt/las/models") constructor_kwargs = { "model_path": model_path, "blur_type": "gaussian", "radius": 20.0, "return_base64": True } ds = ds.with_column( "results", las_udf( ImageFaceBlur, construct_args=constructor_kwargs, num_gpus=0, batch_size=1, concurrency=1, )(col("image_path")), ) ds.show() # ╭────────────────────────────────┬────────────────────────────────────────────────────────────────────────────────────╮ # │ image_path ┆ results │ # │ --- ┆ --- │ # │ String ┆ Struct[base64: String, image_path: String, face_bounding_boxes: List[List[Int32]]] │ # ╞════════════════════════════════╪════════════════════════════════════════════════════════════════════════════════════╡ # │https://las-cn-beijing-public… ┆ {base64: iVBORw0KGgoAAAANSUhE… │ # ╰────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────╯