DeepFace安装使用
- 1.安装
- 1.1 官方的三种方式
- 1.2 使用的方式
- 2.使用
- 2.1 模型文件下载
- 2.2 Facial Recognition
- 2.3 Face Verification
- 2.4 Face recognition
- 2.5 Embeddings
- 2.6 Face recognition models
- 2.7 Similarity
- 2.8 Facial Attribute Analysis
- 2.9 Face Detectors
- 3.总结
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 官方的三种方式
- 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
- Secondly, DeepFace is also available at Conda. You can alternatively install the package via conda.
conda install -c conda-forge deepface
- 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的社区版。
- Anaconda操作
创建虚拟环境+deepface安装:
pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple
- 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系统
functions.get_deepface_home()
下的/.deepface/weights/
目录下。
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,
Dliband
SFace. 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_cosine
跟Facenet_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%。OpenCV,SSD,Dlib,MTCNN,RetinaFace,MediaPipe,YOLOv8 Face和YuNet探测器被包裹在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在检测和对齐后根据目标大小参数添加黑色填充像素。
RetinaFace和MTCNN似乎在检测和对齐阶段表现优异,但它们要慢得多。如果管道的速度更重要,那么应该使用opencv或ssd。另一方面,如果你考虑到准确性,那么你应该使用retinaface或mtcnn。
如下图所示,即使在人群中,RetinaFace的表现也非常令人满意。此外,它还具有令人难以置信的面部地标检测性能。突出的红点表示一些面部标志,如眼睛、鼻子和嘴巴。因此,RetinaFace的对齐分数也很高。
你可以在这个回购上找到更多关于RetinaFace的信息。
3.总结
框架很强大,有现成的模型和api,部署等由于篇幅所限我们下篇继续。