图像人脸检测
更新时间:2026-06-15
简介
图片人脸检测算子
功能描述
- 自动检测图片中的人脸并返回矩形框列表 (x1, y1, x2, y2)
- 支持 URL、本地路径、Base64、二进制等多种输入方式
- 输出人脸矩形框列表
- cpu 和 gpu 环境均可运行
- 隐私合规:发布前对图片中的人脸进行脱敏打码
- 数据预处理:为训练/评测数据集生成带人脸矩形框的版本
- 内容审核/展示:对涉及敏感人脸的图片进行自动矩形框处理
- 单条样本处理失败时,该条结果返回 None,不影响同一批次内其他样本
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 输入图片列,内容类型由 image_src_type 决定: - 当为 "image_url" 时:每个元素为字符串 URL/本地路径/BOS 路径; - 当为 "image_base64" 时:每个元素为 Base64 编码字符串; - 当为 "image_binary" 时:每个元素为图片二进制数据(bytes)。 |
输出
| 输出 | 含义 |
|---|---|
| result | 包含人脸边界框列表的列,每个元素为一个二维列表,表示图片中检测到的人脸边界框。 每个边界框为一个四元组 (x1, y1, x2, y2),表示左上角和右下角坐标。 如果图片中未检测到人脸,则对应元素为 None。 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| 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"。 |
| det_thresh | float | 0.5 | InsightFace 人脸检测置信度阈值。 建议:隐私打码场景可适当降低以减少漏检,精细修图可提高以减少误检。 默认值:0.5。 |
| det_size | tuple[int, int] | (640, 640) | InsightFace 人脸检测输入尺寸 (width, height)。 普通网络图片/头像等使用默认值 (640, 640) 即可。 默认值:(640, 640)。 |
调用示例
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_detect import ImageFaceDetect
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": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "model_path": '/opt/aihc/models',
23 "model_name": 'insightface',
24 "image_src_type": 'image_url',
25 "det_thresh": 0.5,
26 "det_size": (640, 640),
27 }
28 ds = ds.with_column(
29 "result",
30 aihc_udf(
31 ImageFaceDetect,
32 construct_args=constructor_kwargs,
33 num_cpus=1,
34 concurrency=4,
35 batch_size=8,
36 )(col("images")),
37 )
38 ds.show()
评价此篇文章
