图像人脸车牌模糊(Egoblur)
更新时间:2026-06-15
ImageEgoBlur 使用说明
简介
ImageEgoBlur 是基于 Daft 的图片隐私脱敏算子,使用 EgoBlur TorchScript 模型检测图片中的人脸和车牌,并对检测区域进行模糊处理。适用于图片发布前的隐私保护、数据集脱敏、自动化合规处理等场景。
功能
- 支持检测并模糊图片中的人脸区域
- 支持检测并模糊图片中的车牌区域
- 支持 EgoBlur
gen1和gen2模型 - 支持返回处理后图片的 Base64 编码
- 支持将处理后图片保存到 BOS 或本地目录
算子参数
输入
| 字段 | 类型 | 说明 |
|---|---|---|
image_path |
str | 图片地址。测试示例中使用 HTTP URL,如 https://{bucket}.bj.bcebos.com/face.jpg |
输出
egoblur_result 列,类型为 struct。
| 字段 | 类型 | 说明 |
|---|---|---|
base64 |
str | null |
image_path |
str | null |
face_bounding_boxes |
list[list[int]] | 检测到的人脸框,格式为 [x1, y1, x2, y2] |
lp_bounding_boxes |
list[list[int]] | 检测到的车牌框,格式为 [x1, y1, x2, y2] |
单条样本处理失败时,该条结果会返回空结果,例如 base64=None、image_path=None、检测框为空列表,不影响同批次其他样本。
参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
face_model_path |
str | "" |
人脸检测模型路径,如 /opt/aihc/models/Egoblur/ego_blur_face_gen1.jit |
lp_model_path |
str | "" |
车牌检测模型路径,如 /opt/aihc/models/Egoblur/ego_blur_lp_gen1.jit |
output_dir |
str | "" |
处理后图片保存目录,支持 BOS 或本地路径。为空时不保存图片 |
image_src_type |
str | "image_url" |
输入图片类型,可选 image_url、image_base64、image_binary |
model_generation |
str | "auto" |
模型代际,可选 auto、gen1、gen2。auto 会从模型文件名识别 gen2 |
camera_name |
str | "camera-rgb" |
gen2 模型使用的相机名,用于选择默认阈值 |
face_score_threshold |
float | None | None |
lp_score_threshold |
float | None | None |
nms_iou_threshold |
float | None | None |
scale_factor |
float | 1.0 |
模糊区域缩放系数,>1 会扩大模糊区域 |
return_base64 |
bool | False |
是否在输出中返回处理后图片的 Base64 编码 |
return_image_format |
str | "png" |
输出图片格式,可选 png、jpg、jpeg |
至少需要提供 face_model_path 或 lp_model_path 之一。
调用示例
Python
1from __future__ import annotations
2
3import inspect
4import os
5
6import daft
7from daft import col
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.image.image_egoblur import ImageEgoBlur
10
11
12def _get_optional_float(name: str) -> float | None:
13 value = os.getenv(name)
14 return float(value) if value not in {None, ""} else None
15
16
17def _get_bool(name: str, default: bool = False) -> bool:
18 value = os.getenv(name)
19 if value is None:
20 return default
21 return value.lower() in {"1", "true", "yes", "y", "on"}
22
23
24if __name__ == "__main__":
25 if os.getenv("DAFT_RUNNER", "native") == "ray":
26 import ray
27 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
28 daft.set_runner_ray()
29 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
30
31 model_base = os.getenv("MODEL_PATH", "/opt/aihc/models")
32 model_generation = os.getenv("MODEL_GENERATION", "gen1").lower()
33 if model_generation == "gen2" and "model_generation" not in inspect.signature(ImageEgoBlur.__init__).parameters:
34 raise RuntimeError(
35 "MODEL_GENERATION=gen2 requires the gen2-capable ImageEgoBlur implementation. "
36 "The imported ImageEgoBlur is still the old gen1-only version."
37 )
38
39 face_model_path = os.getenv("FACE_MODEL_PATH", os.path.join(model_base, f"ego_blur_face_{model_generation}.jit"))
40 lp_model_path = os.getenv("LP_MODEL_PATH", os.path.join(model_base, f"ego_blur_lp_{model_generation}.jit"))
41 output_dir = os.getenv("OUTPUT_DIR", "")
42
43 samples = {"image_path": ["https://{bucket}.bj.bcebos.com/face.jpg"]}
44 ds = daft.from_pydict(samples)
45
46 constructor_kwargs = {
47 "face_model_path": face_model_path,
48 "lp_model_path": lp_model_path,
49 "model_generation": model_generation,
50 "camera_name": os.getenv("CAMERA_NAME", "camera-rgb"),
51 "output_dir": output_dir,
52 "return_base64": _get_bool("RETURN_BASE64"),
53 }
54 optional_float_kwargs = {
55 "face_score_threshold": _get_optional_float("FACE_SCORE_THRESHOLD"),
56 "lp_score_threshold": _get_optional_float("LP_SCORE_THRESHOLD"),
57 "nms_iou_threshold": _get_optional_float("NMS_IOU_THRESHOLD"),
58 }
59 constructor_kwargs.update({key: value for key, value in optional_float_kwargs.items() if value is not None})
60
61 ds = ds.with_column(
62 "egoblur_result",
63 aihc_udf(
64 ImageEgoBlur,
65 construct_args=constructor_kwargs,
66 num_gpus=0,
67 batch_size=1,
68 concurrency=1,
69 )(col("image_path")),
70 )
71
72 ds.show()
73#╭────────────────────────────────┬────────────────────────────────────────────────────────────────────╮
74#│ image_path ┆ egoblur_result │
75#│ --- ┆ --- │
76#│ String ┆ Struct[base64: String, image_path: String, face_bounding_boxes: …] │
77#╞════════════════════════════════╪════════════════════════════════════════════════════════════════════╡
78#│ https://{bucket}.bj.bcebos.com/┆ {base64: None, image_path: None, face_bounding_boxes: [[...]], …} │
79#╰────────────────────────────────┴────────────────────────────────────────────────────────────────────╯
评价此篇文章
