# dlib

**dlib** 是一个可移植的 C++ 机器学习算法与软件工具包，带有 Python 绑定，由戴维斯·金（Davis King）创建并维护。它被工业界和学术界用于机器人、嵌入式设备、移动电话以及大型高性能计算环境等领域，尤以其人脸检测、面部关键点和人脸识别组件而闻名。<sup>[1](https://dlib.net/)</sup> 该库采用 Boost 软件许可证，允许在任何应用中免费使用，包括闭源商业软件。<sup>[1](https://dlib.net/)</sup><sup> • </sup><sup>[2](https://github.com/davisking/dlib)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个现代 C++ 工具包，涵盖机器学习、优化、线程和计算机视觉算法，通过 pip 提供 Python 绑定<sup>[1](https://dlib.net/)</sup><sup> • </sup><sup>[2](https://github.com/davisking/dlib)</sup> |
| 许可证 | Boost 软件许可证 1.0；允许在闭源商业产品中使用<sup>[2](https://github.com/davisking/dlib)</sup> |
| 人脸识别准确率 | 在 Labeled Faces in the Wild 基准上官方数据为 99.38%；在一项独立流水线基准中为 96.8%<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup><sup> • </sup><sup>[4](https://doi.org/10.17671/gazibtd.1399077)</sup> |
| 嵌入 | 来自 29 层卷积的 ResNet-34 衍生模型的 128 维描述符，以欧几里得距离低于阈值进行匹配<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup><sup> • </sup><sup>[5](https://link.springer.com/article/10.1186/s13640-020-00510-w)</sup> |
| 检测器速度 | CNN（MMOD）检测器：在 CPU 上每张 640x480 图像约 370 毫秒，在 Titan X GPU 上为 45 毫秒（批处理约 18 毫秒）；HOG 检测器是 CPU 上最快的方法，但会漏检小于 70x70 像素的人脸<sup>[6](http://blog.dlib.net/2016/10/easily-create-high-quality-object.html)</sup><sup> • </sup><sup>[7](https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/)</sup> |
| 依赖 | 不需要其他软件包；仅使用操作系统开箱即用的 API，C++ 使用无需安装或配置步骤<sup>[1](https://dlib.net/)</sup> |
| 最新版本 | dlib 20.0.1，2026 年 3 月 29 日；要求 C++14 和 CMake 3.10.0<sup>[8](https://www.dlib.net/release_notes.html)</sup><sup> • </sup><sup>[19](https://security.snyk.io/package/pip/dlib)</sup> |

## 历史与开发

GitHub 仓库创建于 2014 年 1 月 29 日，到 2020 年代中期已获得 14,462 个星标和 3,436 个复刻。<sup>[2](https://github.com/davisking/dlib)</sup> PyPI 记录显示自 2015 年 9 月的 18.17.100 版本起持续发布，均由戴维斯·金编写。<sup>[9](https://pypi.org/project/dlib/)</sup> 该库最初是一个通用机器学习和软件工具包；2017 年 2 月，金发布了下文所述的深度度量学习人脸识别模型，其方法遵循 Schroff 等人（CVPR 2015）的 FaceNet 论文。<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup><sup> • </sup><sup>[10](https://openaccess.thecvf.com/content_cvpr_2015/papers/Schroff_FaceNet_A_Unified_2015_CVPR_paper.pdf)</sup>

## 架构与核心组件

dlib 的组织方式是一组基本独立的组件，而非单一框架。

**机器学习。** 该库实现了支持向量机、相关向量机、多类分类、结构化 SVM 工具、聚类算法（线性与核 k-means、Chinese Whispers 和 Newman 聚类）、径向基函数网络以及多层感知机。<sup>[1](https://dlib.net/)</sup>

**数值与优化。** 矩阵对象在可用时可以使用 BLAS 和 LAPACK，优化套件包括共轭梯度、BFGS 和 L-BFGS、Levenberg-Marquardt、BOBYQA、二次规划求解器以及组合优化工具。<sup>[1](https://dlib.net/)</sup>

**图模型与线程。** dlib 为贝叶斯网络提供联合树精确推断和 Gibbs 采样 MCMC，另有一个可移植的线程 API，包含线程池、并行 for 循环、消息管道和定时器。<sup>[1](https://dlib.net/)</sup>

**图像处理。** 特征提取包括 SURF、HOG 和 FHOG，以及正面人脸检测、物体姿态估计和人脸识别。<sup>[1](https://dlib.net/)</sup>

## 计算机视觉与人脸分析流水线

dlib 的人脸分析栈分阶段运行：检测、对齐、嵌入、比较。

**检测。** 经典的基于 HOG 的正面人脸检测器在 CPU 上速度很快。基于最大间隔目标检测（MMOD）的 CNN 检测器更准确，并且，在深度学习方法中不同寻常的是，它仅需 4 张训练图像即可训练，而典型方法需要数千张。<sup>[6](http://blog.dlib.net/2016/10/easily-create-high-quality-object.html)</sup>

**关键点。** 面部关键点估计实现了 Kazemi 和 Sullivan（2014）的回归树集成方法，定位映射面部结构的 68 个 (x, y) 坐标；还提供了更快的 5 点变体。<sup>[11](https://pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/)</sup> 5 点模型在金使用 dlib 的 imglab 工具标注的 7,198 张人脸上训练，属于公有领域。68 点模型存在许可限制：其训练数据集来自伦敦[帝国理工学院](https://www.edgepedia.org/zh/imperial-college-london)，排除商业用途，数据集创建者 Stefanos Zafeiriou 要求训练出的模型不得用于商业产品。<sup>[12](https://github.com/davisking/dlib-models)</sup>

**嵌入。** 人脸识别模型是一个具有 29 个卷积层的 ResNet，本质上是 ResNet-34 去掉一些层并将滤波器数量减半，在约 300 万张覆盖 7,485 个身份的人脸上从头训练。设计了一种结构化度量损失，将身份投影到半径为 0.6 的不重叠球体中，网络输出 128 维描述符，这是 FaceNet 确立的设计。<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup><sup> • </sup><sup>[10](https://openaccess.thecvf.com/content_cvpr_2015/papers/Schroff_FaceNet_A_Unified_2015_CVPR_paper.pdf)</sup> 比较采用最近邻：计算探测向量与存储的底库向量之间的[欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)，当距离低于预定义阈值时判定为匹配。<sup>[5](https://link.springer.com/article/10.1186/s13640-020-00510-w)</sup>

## 数据一览

官方 LFW 数据为 99.38%，意味着给定两张人脸图像，模型有 99.38% 的概率正确预测它们是否显示同一个人；模型仓库记录的平均准确率为 0.993833，标准差为 0.00272732。<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup><sup> • </sup><sup>[12](https://github.com/davisking/dlib-models)</sup> 金自己的博客指出了这种误差如何累积：30 张图像产生 435 次成对比较，在独立性假设下，至少一次比较出错的概率为 93.33%。<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup>

一项对完整流水线的独立基准测试测得 dlib 准确率为 96.8%，而其声明为 99.3%，ArcFace 为 96.7%（声明 99.5%），VGG-Face 为 96.6%；[OpenFace](https://www.edgepedia.org/zh/openface)（78.7%）、DeepFace（68.7%）和 DeepId（65.6%）远低于其声明分数。<sup>[4](https://doi.org/10.17671/gazibtd.1399077)</sup> 在同一研究中，省略检测阶段时 dlib 的准确率下降了 27.3 个百分点，而 ArcFace 为 41.8%，FaceNet128d 为 30.8%；在所有配置中，dlib 和 ArcFace 各自误分类 3 对，主要是涉及太阳镜、帽子、年龄和情绪差异的假阴性。<sup>[4](https://doi.org/10.17671/gazibtd.1399077)</sup>

速度数据取决于硬件和检测器。CNN 检测器在 CPU 上每张 640x480 图像约需 370 毫秒，在 NVIDIA Titan X（Maxwell）上单张图像为 45 毫秒，批处理每张约 18 毫秒；在 GPU 上其运行速度与仅 CPU 的 HOG 检测器大致相同。<sup>[6](http://blog.dlib.net/2016/10/easily-create-high-quality-object.html)</sup> 在 300x300 图像的仅 CPU 比较中，dlib 的 HOG 检测器是测试中最快的方法，但无法检测小于 70x70 像素的人脸，且除 MMOD CNN 外所有方法在该尺寸下均实时运行。<sup>[7](https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/)</sup> 作为参照，美国国家科学院关于人脸识别的共识报告指出，准确的算法在服务器级 CPU 上可在 0.1 秒至数秒内生成模板，而训练所必需的 GPU 通常对识别并非必要。<sup>[13](https://www.nationalacademies.org/read/27397/chapter/4)</sup> 在 Intel Core i7-7740X 上，OpenFace 和基于 Inception 的 FaceNet_Re 每张人脸缩略图的前向传播时间低于 dlib 的 ResNet 模型。<sup>[5](https://link.springer.com/article/10.1186/s13640-020-00510-w)</sup>

## 与 OpenCV、MediaPipe 和 scikit-learn 的比较

与 OpenCV 相比，实际区别在于检测器选择：在 LearnOpenCV 的比较中，dlib 的 HOG 检测器是最快的 CPU 选项，而 OpenCV 的深度学习检测器和 dlib 的 MMOD CNN 以速度换取准确性，MMOD 在 GPU 上非常快但在 CPU 上非常慢。<sup>[7](https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/)</sup> 在流水线基准中，启用对齐模式时 dlib、RetinaFace 和 [MTCNN](https://www.edgepedia.org/zh/mtcnn) 均表现出显著的性能提升，确立了它们作为跨配置稳健检测器的地位。<sup>[4](https://doi.org/10.17671/gazibtd.1399077)</sup> 与现代嵌入方法相比，dlib 的 128 维描述符小于 ArcFace 的 ResNet-100/50/34 系统输出的 512 维向量。<sup>[5](https://link.springer.com/article/10.1186/s13640-020-00510-w)</sup> 本文所总结的基准文献未涵盖与 MediaPipe 或 scikit-learn 在相同任务上的直接对比。

## 准确性局限与人口统计学差异

金的博客直言不讳地指出，LFW 严重偏向白种成年美国公众人物，因此其他分布上的准确性可能需要不同的阈值。<sup>[3](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)</sup> 领域内的发现为这一担忧提供了数字。美国国土安全部（DHS）对 158 种人脸识别系统组合的研究发现，在 96% 的模型上，佩戴眼镜者的同一人匹配相似度分数较低；在 74% 的模型上，女性与历史底库匹配的分数较低；在 57% 的模型上，肤色较深者的分数较低；在肤色亮度的影响具有统计显著性的情况下，测量的肤色亮度比自我报告的种族更能预测平均同一人匹配相似度。<sup>[14](https://www.dhs.gov/sites/default/files/2023-09/23_0926_st_demographic_effects_across_158_facial_recognition_systems.pdf)</sup> 较早的 DHS 资助研究发现，仅约 10% 的相似度分数变异可归因于种族和性别相同，并表明可以在一定准确率代价下构建忽略种族和性别的算法。<sup>[15](https://www.dhs.gov/sites/default/files/publications/21_0922_st_quantifying-commercial-face-recognition-gender-and-race_updated.pdf)</sup> 兰德公司（RAND）的分析指出，训练数据中的偏差可能影响人脸识别工具识别人员的表现。<sup>[16](https://www.rand.org/content/dam/rand/pubs/research_reports/RR4200/RR4226/RAND_RR4226.pdf)</sup> 这些研究广泛涵盖人脸识别系统而非特指 dlib 的模型，但它们描述了训练数据偏差的条件，在这种条件下，在偏差数据集上训练出的识别器可能表现不均衡。

## 采用与实际使用

除了已记录的机器人、嵌入式设备、移动电话和高性能计算领域之外，<sup>[1](https://dlib.net/)</sup> 一项[树莓派](https://www.edgepedia.org/zh/raspberry-pi)研究发现，Pi 2、3 和 4 可以使用 OpenCV 和 dlib 的 Haar 或 HOG 检测器执行实时人脸检测，而深度神经网络检测器不适合这些设备上的实时任务。<sup>[17](https://pubs.aip.org/aip/acp/article/2420/1/020035/1028586/Case-study-of-face-detection-libraries-on)</sup> 基于 dlib 的典型应用包括将实时摄像头画面与已注册编码进行匹配的人脸考勤系统、生物特征认证、使用 68 个关键点的虚拟滤镜、从关键点位置推断情绪，以及监控中的物体跟踪。<sup>[18](https://painlessprogramming.com/using-dlib-in-python/)</sup>

对于 C++ 使用，不需要其他软件包，也没有安装或配置步骤；仅需操作系统开箱即用的 API。<sup>[1](https://dlib.net/)</sup> 对于 Python，dlib 可通过 `pip install dlib` 从 PyPI 安装，或从 GitHub 仓库构建。<sup>[2](https://github.com/davisking/dlib)</sup> 由于该包编译 C++ 代码，pip 成功前需要可用的构建工具链，这使得安装比大多数 Python 包更为复杂。<sup>[18](https://painlessprogramming.com/using-dlib-in-python/)</sup> PyPI 记录显示上月下载量为 171,622 次，上周为 37,748 次。<sup>[9](https://pypi.org/project/dlib/)</sup>

## 近期发展

开发在 2025 年明显恢复。dlib 20.0 于 2025 年 5 月 27 日发布，将要求提升至 C++14 和 CMake 3.10.0，并增加了一组面向现代深度学习的功能：新的 DNN 层包括 rms_norm_、positional_encodings_、embeddings_ 和 linear_；用于子词分词的字节对编码分词器（bpe_tokenizer）；FFMPEG 封装（dlib::ffmpeg::decoder 和 dlib::ffmpeg::demuxer）以及新的 FFT 工具包括 stft() 和 istft()；还有 auto_train_multiclass_svm_linear_classifier()，一个不需要用户提供超参数的多类 SVM 训练器。<sup>[8](https://www.dlib.net/release_notes.html)</sup> 后续的 20.0.1 版本随后在 PyPI 上发布。<sup>[19](https://security.snyk.io/package/pip/dlib)</sup>

分发与健康信号喜忧参半。Debian 于 2024 年 10 月将 dlib 19.24.6+dfsg-1 接纳入 unstable，并于 2026 年 1 月 3 日将 dlib 20.0+dfsg-2 接纳入 unstable，2026 年 1 月 9 日迁移至 testing。<sup>[20](https://tracker.debian.org/pkg/dlib/news/)</sup> Snyk 报告该包无已知漏洞，但将其维护状态归类为不活跃，指出尽管过去 12 个月内有发布，但过去一个月内无拉取请求活动。<sup>[19](https://security.snyk.io/package/pip/dlib)</sup>

## References

1. [dlib C++ Library, official documentation](https://dlib.net/)
2. [davisking/dlib, GitHub repository and README](https://github.com/davisking/dlib)
3. [High Quality Face Recognition with Deep Metric Learning, dlib blog (Davis King, February 2017)](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html)
4. [A Benchmark of Facial Recognition Pipelines and Co-Usability Performances of Modules, Gaziantep University Journal of Science and Technology](https://doi.org/10.17671/gazibtd.1399077)
5. [Deep-learned faces: a survey, EURASIP Journal on Image and Video Processing (2020)](https://link.springer.com/article/10.1186/s13640-020-00510-w)
6. [Easily Create High Quality Object Detectors with Deep Learning, dlib blog (October 2016)](http://blog.dlib.net/2016/10/easily-create-high-quality-object.html)
7. [Face Detection: Dlib, OpenCV, and Deep Learning, LearnOpenCV](https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/)
8. [dlib C++ Library, Release notes](https://www.dlib.net/release_notes.html)
9. [dlib on PyPI](https://pypi.org/project/dlib/)
10. [Schroff et al. (2015). FaceNet: A Unified Embedding for Face Recognition and Clustering, CVPR 2015](https://openaccess.thecvf.com/content_cvpr_2015/papers/Schroff_FaceNet_A_Unified_2015_CVPR_paper.pdf)
11. [Facial landmarks with dlib, OpenCV, and Python, PyImageSearch](https://pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/)
12. [davisking/dlib-models, GitHub repository](https://github.com/davisking/dlib-models)
13. [Facial Recognition Technology: Current Capabilities, Future Prospects, and Governance, National Academies](https://www.nationalacademies.org/read/27397/chapter/4)
14. [Demographic Effects in Facial Recognition Across 158 System Combinations over Four Studies, DHS S&T (2023)](https://www.dhs.gov/sites/default/files/2023-09/23_0926_st_demographic_effects_across_158_facial_recognition_systems.pdf)
15. [Quantifying the Extent to Which Race and Gender Features Determine Identity in Commercial Face Recognition Algorithms, DHS S&T](https://www.dhs.gov/sites/default/files/publications/21_0922_st_quantifying-commercial-face-recognition-gender-and-race_updated.pdf)
16. [Face Recognition Technologies: Designing Systems that Protect Privacy and Prevent Bias, RAND](https://www.rand.org/content/dam/rand/pubs/research_reports/RR4200/RR4226/RAND_RR4226.pdf)
17. [Case study of face detection libraries on Raspberry Pi, AIP Conference Proceedings](https://pubs.aip.org/aip/acp/article/2420/1/020035/1028586/Case-study-of-face-detection-libraries-on)
18. [Using dlib in Python: A Full Guide to Face Detection and More, Painless Programming](https://painlessprogramming.com/using-dlib-in-python/)
19. [dlib, Snyk package health database](https://security.snyk.io/package/pip/dlib)
20. [Debian Package Tracker, dlib news](https://tracker.debian.org/pkg/dlib/news/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/dlib

English original: https://www.edgepedia.org/dlib
