UniFace快速上手:人脸检测、框选与draw_detections可视化实战教程

发布时间:2026/10/9 14:27:52
UniFace快速上手:人脸检测、框选与draw_detections可视化实战教程
UniFace快速上手人脸检测、框选与draw_detections可视化实战教程【免费下载链接】unifaceUniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.项目地址: https://gitcode.com/gh_mirrors/un/unifaceUniFace 是一个轻量级、面向生产环境的 Python 人脸分析库将人脸检测、识别、关键点、年龄性别、视线等 15 种任务统一到同一套 API 下。本教程以一张合照为例带你 5 分钟完成安装 UniFace、检测出图中所有人脸、读取 bbox 框选坐标并用draw_detections一行代码完成框选 关键点可视化。为什么选择 UniFace一条 pip 命令安装CPU / Apple Silicon / NVIDIA CUDA 全支持模型权重首次使用时自动下载并做 SHA-256 校验统一 API检测器都返回同一种Face对象换模型只需改一行 import内置可视化工具uniface/draw.py 提供draw_detections、draw_tracks、draw_gaze等现成绘制函数无需自己写 OpenCV 绘图逻辑检测模型丰富RetinaFace、SCRFD、CenterFace、YOLOv5-Face、YOLOv8-Face、BlazeFace 随选完整模型对比见 docs/modules/detection.md。安装 UniFace三步跑通pip install uniface[cpu] # CPU 与 Apple Silicon # pip install uniface[gpu] # NVIDIA CUDA 用户也可以从源码安装仓库为只读克隆仅供学习参考git clone https://gitcode.com/gh_mirrors/un/uniface cd uniface pip install uniface[cpu]第一次人脸检测用 RetinaFace 圈出所有人下面这段代码是本教程的核心加载一张图片检测出全部人脸并打印每个人脸的框选坐标。import cv2 from uniface.detection import RetinaFace image cv2.imread(photo.jpg) # 用 OpenCV 读图BGR 格式 detector RetinaFace() # 首次运行自动下载模型 faces detector.detect(image) # 检测返回 Face 对象列表 print(f共检测到 {len(faces)} 张人脸) for face in faces: print(face.bbox, face.confidence)RetinaFace 的实现位于 uniface/detection/retinaface.py#L27-L113。如果追求速度可以换成SCRFD()或CenterFace()调用方式完全一致。看懂 Face 对象bbox、confidence 与 landmarksdetect()返回的每个 Face 对象都带有三个必备字段是后续所有分析的基础字段类型含义face.bboxndarray (4,)框选坐标格式为[x1, y1, x2, y2]左上角 右下角face.confidencefloat检测置信度0~1越接近 1 越可靠face.landmarksndarray (5, 2)5 个关键点左右眼、鼻尖、左右嘴角bbox采用 OpenCV 常见的xyxy坐标系x1, y1是框左上角x2, y2是框右下角。想裁剪人脸区域时直接切图即可x1, y1, x2, y2 map(int, face.bbox) face_crop image[y1:y2, x1:x2] # 第一张人脸的裁剪图draw_detections一行代码完成框选 关键点可视化检测完之后用 draw_detections 把结果画回图片上一行搞定from uniface.draw import draw_detections draw_detections(imageimage, facesfaces) # 原地绘制到 image 上 cv2.imwrite(output.jpg, image)draw_detections 参数速查参数默认值作用image必填待绘制的 BGR 图像会被原地修改faces—detect()返回的 Face 列表vis_threshold0.6置信度过滤阈值低于该值的人脸不画框draw_scoreFalse是否在框角上显示置信度分数corner_bboxTrue使用角标式框科技感False 为普通矩形想要显示每张脸的置信度只需多加一个参数draw_detections(imageimage, facesfaces, vis_threshold0.5, draw_scoreTrue)小贴士vis_threshold调高如 0.8可减少误检调低如 0.3能抓出更小、更模糊的人脸——在人群密集照片上效果明显。实战进阶摄像头实时人脸检测框选把检测 绘制放进循环就是一个实时人脸摄像头应用import cv2 from uniface.detection import SCRFD from uniface.draw import draw_detections detector SCRFD() cap cv2.VideoCapture(0) while True: ok, frame cap.read() if not ok: break draw_detections(imageframe, facesdetector.detect(frame)) cv2.imshow(UniFace, frame) if cv2.waitKey(1) 0xFF ord(q): break不用写代码也能体验仓库自带的命令行工具支持图片、视频和摄像头三种输入python tools/detect.py --source photo.jpg python tools/detect.py --source 0 --detector scrfd # 0 表示摄像头工具源码见 tools/detect.py可切换 6 种检测器做效果对比。下一步检测只是起点框选出人脸后UniFace 还能继续做106/98 点密集关键点、Face Mesh 3D 网格、年龄性别、情绪、头姿、视线、人脸比对与打码匿名化等。 更多场景教程docs/quickstart.md 可运行示例examples/01_face_detection.ipynb 各模块 APIdocs/modules/ 检测器模块源码uniface/detection/现在打开你的第一张合照跑起来试试吧【免费下载链接】unifaceUniFace: A Unified Face Analysis Library for Python | Detection, alignment, landmarks, face-mesh, recognition, parsing, gaze, attributes and anti-spoofing under one API.项目地址: https://gitcode.com/gh_mirrors/un/uniface创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考