深脸-deepface github项目主业
深脸
DeepFace 是一个轻量级的Python人脸识别人脸属性分析(年龄性别情绪种族框架。它是一个混合人脸识别框架,包含最先进的模型:、、、、、、、和。SFaceGhostFaceNet
表明人类在面部识别任务上的准确率为97.53%,而这些模型已经达到并超过了这一准确率水平。
安装
安装 deepface 最简单的方法是从 下载。它将安装库本身及其先决条件。 $ pip install deepface
或者,您也可以从源代码安装 deepface。源代码可能具有尚未在 pip 版本中发布的新功能。 $ git clone https://github.com/serengil/deepface.git $ cd deepface $ pip install -e .
一旦安装了该库,您将能够导入它并使用其功能。 from deepface import DeepFace
现代面部识别流程-
现代由 5 个常见阶段组成:、、、和。虽然 DeepFace 在后台处理所有这些常见阶段,但您无需深入了解其背后的所有流程。您只需用一行代码调用其验证、查找或分析函数即可。
人脸验证-
此函数验证人脸对是同一个人还是不同的人。它需要精确的图像路径作为输入。也欢迎传递 numpy 或 base64 编码的图像。然后,它将返回一个字典,您只需检查其已验证的密钥即可。 result = DeepFace.verify( img1_path = "img1.jpg", img2_path = "img2.jpg", )
人脸识别
需要多次应用人脸验证。在此,deepface 有一个开箱即用的 find 函数来处理此操作。它将在数据库路径中查找输入图像的身份,并返回 pandas 数据框列表作为输出。同时,人脸数据库的人脸嵌入存储在 pickle 文件中,以便下次更快地搜索。结果将是源图像中出现的脸部大小。此外,数据库中的目标图像也可以有多张脸。 dfs = DeepFace.find( img_path = "img1.jpg", db_path = "C:/workspace/my_db", )
嵌入-
人脸识别模型基本上将人脸图像表示为多维向量。有时,您直接需要这些嵌入向量。DeepFace 带有专用的表示函数。Represent 函数返回嵌入列表。结果将是出现在图像路径中的面部大小。 embedding_objs = DeepFace.represent( img_path = "img.jpg" )
此函数返回一个数组作为嵌入。嵌入数组的大小会根据模型名称而不同。例如,VGG-Face 是默认模型,它将面部图像表示为 4096 维向量。 for embedding_obj in embedding_objs: embedding = embedding_obj["embedding"] assert isinstance(embedding, list) assert ( model_name == "VGG-Face" and len(embedding) == 4096 )
这里,嵌入也是水平有 4096 个槽。每个槽对应嵌入向量中的一个维度值,维度值在右侧的颜色栏中说明。与二维条形码类似,图中的垂直维度不存储任何信息。
人脸识别模型-
DeepFace 是一个混合人脸识别包。它目前封装了许多最先进的人脸识别模型:、、、、、、、和。默认配置使用 VGG-Face 模型。SFaceGhostFaceNet
models = [ "VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", "GhostFaceNet", ] #face verification result = DeepFace.verify( img1_path = "img1.jpg", img2_path = "img2.jpg", model_name = models[0], ) #face recognition dfs = DeepFace.find( img_path = "img1.jpg", db_path = "C:/workspace/my_db", model_name = models[1], ) #embeddings embedding_objs = DeepFace.represent( img_path = "img.jpg", model_name = models[2], )
根据实验,FaceNet、VGG-Face、ArcFace 和 Dlib 的表现都十分出色 - 请参阅更多详情。您可以在下表中找到 DeepFace 中各种模型的测量分数以及其原始研究报告的分数。
模型 | 测量分数 | 公布分数 |
---|---|---|
Facenet512 | 98.4% | 99.6% |
人类 | 97.5% | 97.5% |
脸网 | 97.4% | 99.2% |
迪利布 | 96.8% | 99.3% |
VGG-脸 | 96.7% | 98.9% |
ArcFace | 96.7% | 99.5% |
鬼脸网 | 93.3% | 99.7% |
SFace | 93.0% | 99.5% |
开放脸 | 78.7% | 92.9% |
深脸 | 69.0% | 97.3% |
DeepID | 66.5% | 97.4% |
在 DeepFace 中使用这些模型进行实验可能会发现与原始研究相比存在差异,这是由于采用了不同的检测或规范化技术。此外,一些模型仅发布了主干,缺乏预训练权重。因此,我们使用它们的重新实现而不是原始的预训练权重。
相似性-
人脸识别模型是常规的,它们负责将人脸表示为向量。我们期望同一个人的一对人脸比不同人的一对人脸
相似度可以通过不同的度量来计算,例如、欧几里得距离或 L2 归一化欧几里得距离。默认配置使用余弦相似度。根据,没有一种距离度量比其他距离度量表现更好。 metrics = ["cosine", "euclidean", "euclidean_l2"] #face verification result = DeepFace.verify( img1_path = "img1.jpg", img2_path = "img2.jpg", distance_metric = metrics[1], ) #face recognition dfs = DeepFace.find( img_path = "img1.jpg", db_path = "C:/workspace/my_db", distance_metric = metrics[2], )
面部属性分析-
DeepFace 还配备了强大的面部属性分析模块,包括、(包括愤怒、恐惧、中性、悲伤、厌恶、快乐和惊讶)和(包括亚洲、白人、中东、印度、拉丁美洲和黑人)预测。结果将是源图像中出现的面部大小。 objs = DeepFace.analyze( img_path = "img4.jpg", actions = ['age', 'gender', 'race', 'emotion'], )
年龄模型获得了±4.65 MAE;性别模型获得了97.44%的准确度,96.29%的精确度和95.05%的召回率,如其中所述。
人脸检测与对齐-
人脸检测和对齐是现代人脸识别流程中重要的早期阶段。表明,检测可将人脸识别准确率提高 42%,而对齐可将准确率提高 6%。、、、 、Faster MtCnn
、、、Yolo
和检测器YuNet
都CenterFace
包含在 deepface 中。
所有 deepface 函数均接受可选的检测器后端和对齐输入参数。您可以使用这些参数在这些检测器和对齐模式之间切换。OpenCV 是默认检测器,默认情况下对齐处于启用状态。 backends = [ 'opencv', 'ssd', 'dlib', 'mtcnn', 'fastmtcnn', 'retinaface', 'mediapipe', 'yolov8', 'yunet', 'centerface', ] alignment_modes = [True, False] #face verification obj = DeepFace.verify( img1_path = "img1.jpg", img2_path = "img2.jpg", detector_backend = backends[0], align = alignment_modes[0], ) #face recognition dfs = DeepFace.find( img_path = "img.jpg", db_path = "my_db", detector_backend = backends[1], align = alignment_modes[0], ) #embeddings embedding_objs = DeepFace.represent( img_path = "img.jpg", detector_backend = backends[2], align = alignment_modes[0], ) #facial analysis demographies = DeepFace.analyze( img_path = "img4.jpg", detector_backend = backends[3], align = alignment_modes[0], ) #face detection and alignment face_objs = DeepFace.extract_faces( img_path = "img.jpg", detector_backend = backends[4], align = alignment_modes[0], )
人脸识别模型实际上是 CNN 模型,它们需要标准大小的输入。因此,在表示之前需要调整大小。为了避免变形,deepface 在检测和对齐后根据目标尺寸参数添加黑色填充像素。
和在检测和对齐阶段似乎表现优异,但它们的速度要慢得多。如果您的管道速度更重要,那么您应该使用 opencv 或 ssd。另一方面,如果您考虑准确性,那么您应该使用 retinaface 或 mtcnn。
如下图所示,即使在人群中,RetinaFace 的表现也非常令人满意。此外,它还具有令人难以置信的面部标志检测性能。突出显示的红点显示了一些面部标志,例如眼睛、鼻子和嘴巴。这就是为什么 RetinaFace 的对齐分数也很高的原因。
黄色天使——费内巴切女子排球队
上了解有关 RetinaFace 的更多信息。
实时分析-
您还可以对实时视频运行 deepface。Stream 函数将访问您的网络摄像头并应用人脸识别和面部属性分析。如果该函数可以连续 5 帧聚焦一张脸,则开始分析一帧。然后,它会在 5 秒内显示结果。 DeepFace.stream(db_path = "C:/User/Sefik/Desktop/database")
尽管人脸识别是基于一次性学习的,但您也可以使用一个人的多张人脸图片。您应该重新排列目录结构,如下所示。 user ├── database │ ├── Alice │ │ ├── Alice1.jpg │ │ ├── Alice2.jpg │ ├── Bob │ │ ├── Bob.jpg
React UI- ,
如果您打算直接从浏览器执行人脸验证任务,是一个使用 ReactJS 构建的依赖于 deepface api 的单独存储库。
人脸防欺骗-
DeepFace 还包含一个反欺骗分析模块,用于了解给定图像是真是假。要激活此功能,请anti_spoofing
在任何 DeepFace 任务中将参数设置为 True。
# anti spoofing test in face detection face_objs = DeepFace.extract_faces( img_path="dataset/img1.jpg", anti_spoofing = True ) assert all(face_obj["is_real"] is True for face_obj in face_objs) # anti spoofing test in real time analysis DeepFace.stream( db_path = "C:/User/Sefik/Desktop/database", anti_spoofing = True )
API -
DeepFace 还提供 API - 请参阅了解更多详情。您可以克隆 deepface 源代码并使用以下命令运行 api。它将使用 gunicorn 服务器来启动 rest 服务。这样,您可以从外部系统(例如移动应用程序或 Web)调用 deepface。 cd scripts ./service.sh
API 涵盖了人脸识别、人脸属性分析和向量表示函数。您需要将这些函数作为 http post 方法调用。默认服务端点将用于http://localhost:5005/verify
人脸识别、http://localhost:5005/analyze
人脸属性分析和http://localhost:5005/represent
向量表示。您可以将输入图像作为环境中的精确图像路径、base64 编码字符串或 Web 上的图像传递。,您可以找到一个 postman 项目来了解如何调用这些方法。
Docker 化服务-
以下命令集将localhost:5005
通过 docker 为 deepface 提供服务。然后,您将能够使用 deepface 服务,例如验证、分析和表示。此外,如果您想自己构建映像而不是从 docker hub 预构建映像,可在项目的根文件夹中找到。 # docker build -t serengil/deepface . # build docker image from Dockerfile docker pull serengil/deepface # use pre-built docker image from docker hub docker run -p 5005:5000 serengil/deepface
命令行界面-
DeepFace 还带有命令行界面。您可以在命令行中访问其函数,如下所示。命令 deepface 需要将函数名称作为第一个参数,然后是函数参数。 #face verification $ deepface verify -img1_path tests/dataset/img1.jpg -img2_path tests/dataset/img2.jpg #facial analysis $ deepface analyze -img_path tests/dataset/img1.jpg
中的说明进行操作。
大规模面部识别-
如果您的任务需要在大型数据集上进行面部识别,则应将 DeepFace 与向量索引或向量数据库结合使用。此设置将执行搜索而不是精确搜索,从而使您可以在几毫秒内识别包含数十亿条条目的数据库中的面部。常见的向量索引解决方案包括、、、、。对于向量数据库,流行的选项是和。
相反,如果您的任务涉及在小型到中型数据库上进行面部识别,您可以采用关系数据库(如或或 NoSQL 数据库(如、或来执行精确的最近邻搜索。
贡献
欢迎提出拉取请求!如果您计划贡献一个大型补丁,请先创建一个问题,以首先解决任何前期问题或设计决策。
在创建 PR 之前,您应该通过运行命令在本地运行单元测试和 linting 。一旦 PR 发送,GitHub 测试工作流程将自动运行,单元测试和 linting 作业将在批准前在make test && make lint
中可用。
支持
支持一个项目的方法有很多 - GitHub repo 上的 starring⭐️ 只是其中一种🙏
、或上为其提供资金支持。
此外,如果您成为金、银或铜级赞助商,您公司的徽标将显示在 GitHub 上的 README 上。
引用
如果 deepface 有助于您的研究,请在您的出版物中引用它 - 请参阅更多详细信息。以下是它的 BibTex 条目:
如果您在研究中使用 deepface 进行面部识别或面部检测,请引用以下出版物: @article{serengil2024lightface, title = {A Benchmark of Facial Recognition Pipelines and Co-Usability Performances of Modules}, author = {Serengil, Sefik and Ozpinar, Alper}, journal = {Journal of Information Technologies}, volume = {17}, number = {2}, pages = {95-107}, year = {2024}, doi = {10.17671/gazibtd.1399077}, url = {https://dergipark.org.tr/en/pub/gazibtd/issue/84331/1399077}, publisher = {Gazi University} } @inproceedings{serengil2020lightface, title = {LightFace: A Hybrid Deep Face Recognition Framework}, author = {Serengil, Sefik Ilkin and Ozpinar, Alper}, booktitle = {2020 Innovations in Intelligent Systems and Applications Conference (ASYU)}, pages = {23-27}, year = {2020}, doi = {10.1109/ASYU50717.2020.9259802}, url = {https://ieeexplore.ieee.org/document/9259802}, organization = {IEEE} }
另一方面,如果您在研究中使用 deepface 进行面部属性分析,例如年龄、性别、情绪或种族预测任务,请引用此出版物。 @inproceedings{serengil2021lightface, title = {HyperExtended LightFace: A Facial Attribute Analysis Framework}, author = {Serengil, Sefik Ilkin and Ozpinar, Alper}, booktitle = {2021 International Conference on Engineering and Emerging Technologies (ICEET)}, pages = {1-4}, year = {2021}, doi = {10.1109/ICEET53442.2021.9659697}, url = {https://ieeexplore.ieee.org/document/9659697}, organization = {IEEE} }
此外,如果您在 GitHub 项目中使用 deepface,请deepface
添加requirements.txt
。
执照
DeepFace 是在 MIT 许可下授权的——查看更多详细信息。
DeepFace 包装了一些外部人脸识别模型:、(128d 和 512d)、、、、、、和。此外,年龄、性别和种族/民族模型在 VGG-Face 的主干上通过迁移学习进行训练。同样,DeepFace 包装了许多人脸检测器:、、、、、、、、和。最后,DeepFace 可选择使用来确定给定的图像是真是假。当您打算使用这些模型时,许可证类型将被继承。请检查这些模型的许可证类型以用于生产目的。
DeepFace创建,并根据 Creative Commons 获得许可。