DeepFace【部署 01】轻量级人脸识别和面部属性分析框架deepface安装使用详解(网盘分享模型文件)

简介: DeepFace【部署 01】轻量级人脸识别和面部属性分析框架deepface安装使用详解(网盘分享模型文件)

Github地址:https://github.com/serengil/deepface/tree/master

模型下载地址:https://github.com/serengil/deepface_models/releases/

百度网盘模型下载:

链接:https://pan.baidu.com/s/11AJfX_c_LfmdPsznjNhu_g

提取码:7q38

官方自我介绍: Deepface is a lightweight face recognition and facial attribute analysis (age, gender, emotion and race) framework for python. It is a hybrid face recognition framework wrapping state-of-the-art models: VGG-Face, Google FaceNet, OpenFace, Facebook DeepFace, DeepID, ArcFace, Dlib and SFace.

Experiments show that human beings have 97.53% accuracy on facial recognition tasks whereas those models already reached and passed that accuracy level.

请小伙伴儿们注意: 以下内容基本来自官网的翻译,和自己的实践记录,不准确的地方望指正。

1.安装

1.1 官方的三种方式

  1. The easiest way to install deepface is to download it from PyPI. It’s going to install the library itself and its prerequisites as well.
pip install deepface
  1. Secondly, DeepFace is also available at Conda. You can alternatively install the package via conda.
conda install -c conda-forge deepface
  1. Thirdly, you can install deepface from its source code.
$ git clone https://github.com/serengil/deepface.git
$ cd deepface
$ pip install -e .

1.2 使用的方式

我使用的是Anaconda+PyCharm的社区版。

  1. Anaconda操作

创建虚拟环境+deepface安装:

# 安装deepface
pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装deprecated
pip install deprecated==1.2.13
# 防止报错
ModuleNotFoundError: No module named 'deprecated'
  1. Pycharm操作

下载源码:https://github.com/serengil/deepface/tree/master

设置运行环境:File->Setting->Project->Python Interpreter->使用Anaconda创建的环境

2.使用

2.1 模型文件下载

模型下载地址:https://github.com/serengil/deepface_models/releases/,文件列表:

预训练面部识别模型:

  • VGG-Face 由Visual Geometry Group开发,是一种基于深度卷积神经网络的人脸识别模型。
  • FaceNet (128D, 512D) 由Google开发,是一种基于三维卷积神经网络的人脸识别模型。
  • OpenFace 由Carnegie Mellon University开发,是一种基于深度神经网络的人脸识别模型。
  • DeepID 由香港中文大学开发,是一种基于深度神经网络的人脸识别模型。
  • ArcFace 由中科院自动化所开发,是一种基于深度卷积神经网络的人脸识别模型。

预训练面部特征分析模型:

  • Age
  • Gender
  • Emotion
  • Race / Ethnicity

直接使用浏览器的下载速度堪称龟速,还很容易失败,我是用云服务器进行的下载,以下是全部链接:

wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/age_model_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/arcface_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/deepid_keras_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/facenet512_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/facenet_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/facial_expression_model_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/gender_model_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/openface_weights.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/race_model_single_batch.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/retinaface.h5
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/vgg_face_weights.h5
wget -c https://github.com/serengil/deepface_models/archive/refs/tags/v1.0.zip
wget -c https://github.com/serengil/deepface_models/archive/refs/tags/v1.0.tar.gz

刚开始速度也是很慢,随后就会起飞,如果一分钟还是几K的速度就断开重新开始,速度几M是可以达到的。下载成功后放置到functions.get_deepface_home()目录下【不同设备的目录不同以下仅供参考】:

  • Windows系统C:\Users\Administrator下的/.deepface/weights/目录下。
  • Linux系统/root下的/.deepface/weights/目录下。

执行以下代码获取模型目录:

from deepface.commons import functions
if __name__ == "__main__":
    home = functions.get_deepface_home()
    print(home)

2.2 Facial Recognition

A modern face recognition pipeline consists of 5 common stages: detect, align, normalize, represent and verify. While Deepface handles all these common stages in the background, you don’t need to acquire in-depth knowledge about all the processes behind it. You can just call its verification, find or analysis function with a single line of code.

如今的人脸识别流程包括5个基本阶段:检测、对齐、归一化、表示和验证。Deepface会在后台处理所有这些,因此您不需要深入了解其背后的所有过程。你可以用一行代码调用它的验证、查找或分析函数。

2.3 Face Verification

这个函数验证一对人脸是否是同一个人。它期望输入的是精确的图像路径。也可以传递numpy或base64编码的图像。然后,它将返回一个字典,你只需要检查它的verified键。

result = DeepFace.verify(img1_path = "img1.jpg", img2_path = "img2.jpg")
# 由于测试时【VGG-Face】模型未下载完成故使用【Facenet】
result = DeepFace.verify(img1_path="tests/dataset/img1.jpg", img2_path="tests/dataset/img2.jpg", model_name="Facenet")

结果数据如下:

{
    "detector_backend": "opencv",
    "distance": 0.24256996744273296,
    "facial_areas": {
        "img1": {
            "h": 769,
            "w": 769,
            "x": 345,
            "y": 211
        },
        "img2": {
            "h": 512,
            "w": 512,
            "x": 516,
            "y": 192
        }
    },
    "model": "Facenet",
    "similarity_metric": "cosine",
    "threshold": 0.4,
    "time": 5.93,
    "verified": True
}

由于使用的模型不同,结果跟图片中的不完全一致:

验证函数也可以处理多人脸对的情况。将会比较最相似的面部。测试代码及结果如下:

result = DeepFace.verify(img1_path="tests/dataset/img1.jpg", img2_path="tests/dataset/couple.jpg", model_name="Facenet")
{
    "detector_backend": "opencv",
    "distance": 0.2260340280992379,
    "facial_areas": {
        "img1": {
            "h": 769,
            "w": 769,
            "x": 345,
            "y": 211
        },
        "img2": {
            "h": 779,
            "w": 779,
            "x": 318,
            "y": 534
        }
    },
    "model": "Facenet",
    "similarity_metric": "cosine",
    "threshold": 0.4,
    "time": 5.2,
    "verified": True
}

由于使用的模型不同,结果跟图片中的不完全一致:

2.4 Face recognition

人脸识别需要多次进行人脸验证。在这里,deepface有一个开箱即用的查找函数来处理这个动作。它将在数据库路径中查找输入图像的身份它将返回pandas数据帧列表作为输出。同时,人脸数据库中的人脸嵌入被存储在pickle文件中,以便下次快速搜索。结果将是出现在源图像中的脸的大小。此外,数据库中的目标图像也可以有多个人脸。

dfs = DeepFace.find(img_path = "img1.jpg", db_path = "C:/workspace/my_db")
# 使用源码里的测试图片
dfs = DeepFace.find(img_path="tests/dataset/img1.jpg", db_path="tests/dataset/", model_name="Facenet")

结果数据,facenet_cosine越小相似度越高:

[                    identity  source_x  ...  source_h  Facenet_cosine
0    tests/dataset//img1.jpg       345  ...       769        0.000000
1   tests/dataset//img11.jpg       345  ...       769        0.184640
2    tests/dataset//img7.jpg       345  ...       769        0.193672
3   tests/dataset//img10.jpg       345  ...       769        0.221339
4  tests/dataset//couple.jpg       345  ...       769        0.226034
5    tests/dataset//img6.jpg       345  ...       769        0.230715
6    tests/dataset//img2.jpg       345  ...       769        0.242570
7    tests/dataset//img4.jpg       345  ...       769        0.246557
8    tests/dataset//img5.jpg       345  ...       769        0.259066
[9 rows x 6 columns]]

2.5 Embeddings

人脸识别模型基本上是将人脸图像表示为多维向量。有时候,你直接需要这些嵌入向量。DeepFace带有专门的表示函数。标识函数返回嵌入列表。结果将是出现在图像路径中的面的大小。

embedding_objs = DeepFace.represent(img_path = "tests/dataset/img1.jpg")
# 实际测试
embedding_objs = DeepFace.represent(img_path="tests/dataset/img1.jpg", model_name="Facenet")

结果数据,:

[
    {
        "embedding": [
            0.8134615421295166,
            1.0957121849060059,
            1.477981686592102,
            -1.6971195936203003,
            0.49749791622161865,
            -0.6081872582435608...[省略了一些数据]
        ],
        "face_confidence": 10.068783960014116,
        "facial_area": {
            "h": 769,
            "w": 769,
            "x": 345,
            "y": 211
        }
    }
]

这个函数返回一个数组作为嵌入。嵌入数组的大小将根据模型名称而有所不同。例如,VGG-Face是默认模型,它将面部图像表示为2622维向量。

embedding = embedding_objs[0]["embedding"]
assert isinstance(embedding, list)
assert model_name = "VGG-Face" and len(embedding) == 2622
# 实际测试
embedding = embedding_objs[0]["embedding"]
assert isinstance(embedding, list)
assert len(embedding) == 128

在这里,嵌入也是用2622个槽水平绘制的。每个槽对应嵌入向量中的一个维度值,维度值在右侧的颜色条中进行说明。与2D条形码类似,垂直维度在插图中不存储任何信息。

2.6 Face recognition models

Deepface is a hybrid face recognition package. It currently wraps many state-of-the-art face recognition models: VGG-Face,Google FaceNet, OpenFace, Facebook DeepFace, DeepID, ArcFace, DlibandSFace. The default configuration uses VGG-Face model.

models = ["VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib", "SFace"]
# 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 and Dlib are overperforming ones based on experiments. You can find out the scores of those models below on both Labeled Faces in the Wild and YouTube Faces in the Wild data sets declared by its creators.

Model LFW Score YTF Score
Facenet512 99.65% -
SFace 99.60% -
ArcFace 99.41% -
Dlib 99.38 % -
Facenet 99.20% -
VGG-Face 98.78% 97.40%
Human-beings 97.53% -
OpenFace 93.80% -
DeepID - 97.05%

LFW Score和YTF Score是两种用于评估人脸识别算法性能的指标,分别基于LFW和YTF两个数据集。LFW(Labeled Faces in the Wild)是一个包含超过13,000张带有人名标注的人脸图像的数据集,这些图像来自互联网上不同的人脸图库,涵盖了各种人种、姿态和表情。YTF(YouTube Faces)是一个包含3425个视频的人脸视频数据集,这些视频来自YouTube上1595个不同人的视频,涉及大量的姿态、表情、光照和遮挡变化。LFW Score和YTF Score分别使用LFW和YTF数据集中的匹配对和不匹配对来测试人脸识别算法是否能够正确判断两张人脸是否属于同一个人。一般来说,LFW Score和YTF Score越高,表示人脸识别算法越准确。

2.7 Similarity

人脸识别模型是正则卷积神经网络,它们负责将人脸表示为向量。我们认为同一个人的两张脸应该比不同人的两张脸更相似。相似度可以通过余弦相似度、欧氏距离和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]
)
# 实际测试
# face verification
result = DeepFace.verify(img1_path="tests/dataset/img1.jpg",
                         img2_path="tests/dataset/img2.jpg",
                         model_name="Facenet",
                         distance_metric=metrics[1]
                         )
# face recognition
dfs = DeepFace.find(img_path="tests/dataset/img1.jpg",
                    db_path="tests/dataset/",
                    model_name="Facenet",
                    distance_metric=metrics[2]
                    )

前面的cosine距离是0.24256996744273296:

{
    "detector_backend": "opencv",
    "distance": 8.146639638611239,
    "facial_areas": {
        "img1": {
            "h": 769,
            "w": 769,
            "x": 345,
            "y": 211
        },
        "img2": {
            "h": 512,
            "w": 512,
            "x": 516,
            "y": 192
        }
    },
    "model": "Facenet",
    "similarity_metric": "euclidean",
    "threshold": 10,
    "time": 4.53,
    "verified": True
}

之前的Facenet_cosineFacenet_euclidean_l2区别还是挺大的:

[                    identity  source_x  ...  source_h  Facenet_euclidean_l2
0    tests/dataset//img1.jpg       345  ...       769              0.000000
1   tests/dataset//img11.jpg       345  ...       769              0.607683
2    tests/dataset//img7.jpg       345  ...       769              0.622369
3   tests/dataset//img10.jpg       345  ...       769              0.665341
4  tests/dataset//couple.jpg       345  ...       769              0.672360
5    tests/dataset//img6.jpg       345  ...       769              0.679286
6    tests/dataset//img2.jpg       345  ...       769              0.696520
7    tests/dataset//img4.jpg       345  ...       769              0.702221
8    tests/dataset//img5.jpg       345  ...       769              0.719814
[9 rows x 6 columns]]

欧几里得L2形式似乎比余弦和正则欧几里得距离更稳定。但是实际使用,更常用的是cosine因为它的结果是0~1可以换算成相似度100%~0%

2.8 Facial Attribute Analysis

Deepface还配备了强大的面部属性分析模块,包括年龄性别面部表情(包括愤怒,恐惧,中性,悲伤,厌恶,惊喜)和种族(包括亚洲人、白人、中东人、印度人、拉丁裔和黑人)预测。结果将是出现在源图像中的脸的大小。

objs = DeepFace.analyze(img_path = "img4.jpg", 
        actions = ['age', 'gender', 'race', 'emotion']
)
# 实际测试
objs = DeepFace.analyze(img_path="tests/dataset/img4.jpg", actions=['age', 'gender', 'race', 'emotion'])

结果数据:

[
    {
        "age": 31,
        "dominant_emotion": "happy",
        "dominant_gender": "Woman",
        "dominant_race": "white",
        "emotion": {
            "angry": 0.06343021349136967,
            "disgust": 0.000025026363773344608,
            "fear": 0.16905087126557575,
            "happy": 92.43509859620349,
            "neutral": 6.627006038784261,
            "sad": 0.4689154121827588,
            "surprise": 0.23647171420196125
        },
        "gender": {
            "Man": 0.00018949589275507606,
            "Woman": 99.99980926513672
        },
        "race": {
            "asian": 0.05465112952298698,
            "black": 0.0038303477149617426,
            "indian": 0.08727377218708528,
            "latino hispanic": 3.3318923909600016,
            "middle eastern": 5.098589813625205,
            "white": 91.42376133345135
        },
        "region": {
            "h": 919,
            "w": 919,
            "x": 419,
            "y": 301
        }
    }
]

根据其教程,年龄模型的平均绝对误差为±4.65,性别模型的准确率为97.44%,精度为96.29%,召回率为95.05%。

2.9 Face Detectors

人脸检测和对齐是现代人脸识别流程的重要早期阶段。实验表明,仅仅对齐就能使人脸识别准确率提高近1%。OpenCVSSDDlibMTCNNRetinaFaceMediaPipeYOLOv8 FaceYuNet探测器被包裹在deepface中。

所有deepface函数都接受一个可选的检测器后端输入参数。你可以用这个参数在这些检测器之间切换。OpenCV是默认的检测器。

backends = [
  'opencv', 
  'ssd', 
  'dlib', 
  'mtcnn', 
  'retinaface', 
  'mediapipe',
  'yolov8',
  'yunet',
]
#face verification
obj = DeepFace.verify(img1_path = "img1.jpg", 
        img2_path = "img2.jpg", 
        detector_backend = backends[0]
)
#face recognition
dfs = DeepFace.find(img_path = "img.jpg", 
        db_path = "my_db", 
        detector_backend = backends[1]
)
#embeddings
embedding_objs = DeepFace.represent(img_path = "img.jpg", 
        detector_backend = backends[2]
)
#facial analysis
demographies = DeepFace.analyze(img_path = "img4.jpg", 
        detector_backend = backends[3]
)
#face detection and alignment
face_objs = DeepFace.extract_faces(img_path = "img.jpg", 
        target_size = (224, 224), 
        detector_backend = backends[4]
)
# 这里不再进行详细的测试

人脸识别模型实际上是CNN模型,它们期望标准大小的输入。因此,在表示之前需要调整大小。为了避免变形,deepface在检测和对齐后根据目标大小参数添加黑色填充像素。

RetinaFaceMTCNN似乎在检测和对齐阶段表现优异,但它们要慢得多。如果管道的速度更重要,那么应该使用opencv或ssd。另一方面,如果你考虑到准确性,那么你应该使用retinaface或mtcnn。

如下图所示,即使在人群中,RetinaFace的表现也非常令人满意。此外,它还具有令人难以置信的面部地标检测性能。突出的红点表示一些面部标志,如眼睛、鼻子和嘴巴。因此,RetinaFace的对齐分数也很高。

你可以在这个回购上找到更多关于RetinaFace的信息。

3.总结

框架很强大,有现成的模型和api,部署等由于篇幅所限我们下篇继续。

4.更新

  • 2023-09-28 虚拟环境添加deprecated安装和get_deepface_home()方法说明。
目录
相关文章
|
4月前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之uniapp框架如何使用阿里云金融级人脸识别
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
119 0
|
3月前
|
机器学习/深度学习 监控 算法框架/工具
使用Python实现深度学习模型:人脸识别与人脸表情分析
【7月更文挑战第18天】 使用Python实现深度学习模型:人脸识别与人脸表情分析
180 2
|
3月前
|
机器学习/深度学习 数据采集 算法
Python实现PCA降维和KNN人脸识别模型(PCA和KNeighborsClassifier算法)项目实战
Python实现PCA降维和KNN人脸识别模型(PCA和KNeighborsClassifier算法)项目实战
|
5月前
|
算法 计算机视觉 异构计算
基于肤色模型的人脸识别FPGA实现,包含tb测试文件和MATLAB辅助验证
这是一个关于肤色检测算法的摘要:使用MATLAB 2022a和Vivado 2019.2进行测试和仿真,涉及图像预处理、RGB到YCbCr转换、肤色模型(基于阈值或概率)以及人脸检测。核心程序展示了如何读取图像数据并输入到FPGA处理,通过`tops`模块进行中值滤波、颜色空间转换及人脸检测,最终结果输出到"face.txt"。
|
5月前
|
并行计算 Linux 计算机视觉
DeepFace【部署 04】轻量级人脸识别和面部属性分析框架deepface使用Docker部署CPU+GPU两个版本及cuDNN安装
DeepFace【部署 04】轻量级人脸识别和面部属性分析框架deepface使用Docker部署CPU+GPU两个版本及cuDNN安装
586 0
|
5月前
|
Linux API 计算机视觉
DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)
DeepFace【部署 03】轻量级人脸识别和面部属性分析框架deepface在Linux环境下服务部署(conda虚拟环境+docker)
225 0
|
5月前
|
Kubernetes Shell API
DeepFace【部署 02】轻量级人脸识别和面部属性分析框架(实时分析+API+Docker部署+命令行接口)
DeepFace【部署 02】轻量级人脸识别和面部属性分析框架(实时分析+API+Docker部署+命令行接口)
275 0
|
5月前
|
弹性计算 Java PHP
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
对于初次购买阿里云产品的用户来说,第一步要做的是注册账号并完成实名认证,然后才是购买阿里云服务器或者其他云产品,本文为大家以图文形式展示一下新手用户从注册阿里云账号、实名认证到购买云服务器完整详细教程,以供参考。
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
2114 341

热门文章

最新文章