图像人脸模糊
更新时间:2026-06-15
简介
图片人脸模糊处理算子
功能描述
- 自动检测图片中的人脸并进行模糊处理
- 支持多种模糊类型(均值、盒式、高斯)
- 支持 URL、本地路径、Base64、二进制等多种输入方式
- 可选输出模糊后图片的 Base64 编码以及保存到的路径
- cpu 和 gpu 环境均可运行
- 隐私合规:发布前对图片中的人脸进行脱敏打码
- 数据预处理:为训练/评测数据集生成带人脸模糊的版本
- 内容审核/展示:对涉及敏感人脸的图片进行自动模糊处理
- 单条样本处理失败时,该条结果返回 {"base64": None, "image_path": None},不影响同一批次内其他样本
- 当配置 output_dir 时,模糊后的图片会上传到指定BOS/本地目录
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | - |
| images_name | - |
输出
| 输出 | 含义 |
|---|---|
| result | 算子输出 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| output_dir | str | '' | 将模糊处理后的图片保存到的 BOS 目录或者本地路径。 格式:"bos://bucket/path/" 或 "/local/path/" 默认值:"" |
| model_path | str | '/opt/aihc/models' | 人脸检测模型的基础目录路径,用于 InsightFace 模型加载。 默认值:"/opt/aihc/models"。 |
| model_name | str | 'insightface' | InsightFace 模型子目录名称,用于拼接实际模型路径 model_path/model_name。 默认值:"insightface"。 |
| image_src_type | str | 'image_url' | 输入图片的数据类型。 可选值:"image_url"、"image_base64"、"image_binary"。 - "image_url": HTTP/BOS/本地路径等可通过路径访问的图片 - "image_base64": Base64 编码字符串 - "image_binary": 图片二进制数据(bytes) 默认值:"image_url"。 |
| blur_type | str | 'gaussian' | 人脸区域的模糊方式。 可选值:"mean"、"box"、"gaussian"。 - "mean": 使用均值模糊(ImageFilter.BLUR) - "box": 使用盒式模糊(ImageFilter.BoxBlur) - "gaussian": 使用高斯模糊(ImageFilter.GaussianBlur) 默认值:"gaussian"。 |
| radius | float | 10.0 | 模糊半径,适用于 "box" 和 "gaussian" 模式。 要求:radius >= 0,默认值:10.0。 |
| det_thresh | float | 0.5 | InsightFace 人脸检测置信度阈值。 建议:隐私打码场景可适当降低以减少漏检,精细修图可提高以减少误检。 默认值:0.5。 |
| det_size | tuple[int, int] | (640, 640) | InsightFace 人脸检测输入尺寸 (width, height)。 普通网络图片/头像等使用默认值 (640, 640) 即可。 默认值:(640, 640)。 |
| return_base64 | bool | False | 是否在输出结果中包含模糊后图片的 Base64 编码。 默认值:False。 |
| return_image_format | str | 'png' | 输出图片的格式 可选值:"png"、"jpg"、"jpeg", 默认值:"png"。 |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.image.image_face_blur import ImageFaceBlur
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 # TODO: 根据实际场景准备样本数据
19 samples = {"images": [...], "images_name": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "output_dir": '',
23 "model_path": '/opt/aihc/models',
24 "model_name": 'insightface',
25 "image_src_type": 'image_url',
26 "blur_type": 'gaussian',
27 }
28 ds = ds.with_column(
29 "result",
30 aihc_udf(
31 ImageFaceBlur,
32 construct_args=constructor_kwargs,
33 num_cpus=1,
34 concurrency=4,
35 batch_size=8,
36 )(col("images"), col("images_name")),
37 )
38 ds.show()
评价此篇文章
