You need to enable JavaScript to run this app.
AI 数据湖服务

AI 数据湖服务

复制全文
图片处理
图片人脸模糊
复制全文
图片人脸模糊

算子介绍

描述

图片人脸模糊处理算子

主要功能

  • 自动检测图片中的人脸并进行模糊处理
  • 支持多种模糊类型(均值、盒式、高斯)
  • 支持 URL、本地路径、Base64、二进制等多种输入方式
  • 可选输出模糊后图片的 Base64 编码以及保存到的路径
  • cpu 和 gpu 环境均可运行

适用场景

  • 隐私合规:发布前对图片中的人脸进行脱敏打码
  • 数据预处理:为训练/评测数据集生成带人脸模糊的版本
  • 内容审核/展示:对涉及敏感人脸的图片进行自动模糊处理

注意事项

  • 单条样本处理失败时,该条结果返回 {"base64": None, "image_path": None},不影响同一批次内其他样本
  • 当配置 output_dir 时,模糊后的图片会上传到指定TOS/本地目录

Daft 调用

算子参数

输入

输入列名

说明

images

输入图片列。根据 image_src_type 参数的不同,内容可以是图片 URL/路径、Base64 字符串或二进制数据。

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 模型子目录名称,与 model_path 组合成完整路径。

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" 模式,要求 radius >= 0

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…                                                     │
    # ╰────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────╯
最近更新时间:2026.01.15 18:06:19
这个页面对您有帮助吗?
有用
有用
无用
无用