[深度学习] Python人脸识别库Deepface使用教程
off999 2024-10-20 08:09 17 浏览 0 评论
deepface是一个Python轻量级人脸识别和人脸属性分析(年龄、性别、情感和种族)框架,提供非常简单的接口就可以实现各种人脸识别算法的应用。deepface官方仓库为?deepface???。deepface提供了多种模型,模型下载地址为??deepface_models??。
安装方式: pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple
deepface主要提供以下人脸识别算法,具体对应接口为:
- DeepFace.verify:人脸验证
- DeepFace.find:人脸识别
- DeepFace.analyze:人脸属性分析
- DeepFace.detectFace:人脸检测
- DeepFace.represent:人脸特征提取
- DeepFace.stream:人脸实时分析
总体而言,这个项目的人脸识别模型识别效果还行,但是离工程应用还是有一定的距离,不过还是非常推荐学习该库内部代码。
某些网站会判定本文人脸图片违规,这是网站识别算法自身问题。
本文所有算法展示效果和代码见:
github: ??Python-Study-Notes??
此外可以看一看另外一个人脸识别库,功能更加齐全:[深度学习] Python人脸识别库face_recognition使用教程
文章目录
- 0 数据准备
- 1 人脸验证DeepFace.verify
- 2 人脸识别DeepFace.find
- 3 人脸属性分析DeepFace.analyze
- 4 人脸检测DeepFace.detectFace
- 5 人脸特征提取DeepFace.represent
- 6 参考
0 数据准备
# deep库的导入就一行代码
from deepface import DeepFace
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
import os
import cv2
import numpy as np
所使用的数据集为网络明星图片,共五个明星,每个明星三张人脸,数据集的路径如下:
root
├── images
│ ├── baijingting
│ │ ├── 0000.jpg
│ │ ├── 0001.jpg
│ ├── jiangwei
│ │ ├── 0000.jpg
│
├── code
数据展示结果如下:
# --- 展示图片
def show_img(imgs: list, img_names: list) -> None:
imgs_count = len(imgs)
for i in range(imgs_count):
ax = plt.subplot(1, imgs_count, i+1)
ax.imshow(imgs[i])
ax.set_title(img_names[i])
ax.set_xticks([])
ax.set_yticks([])
plt.tight_layout(h_pad=3)
plt.show()
img_path = "images"
for person_dir in os.listdir(img_path):
imgs = []
img_names = []
for file in os.listdir(os.path.join(img_path, person_dir)):
imgs.append(Image.open(os.path.join(img_path, person_dir, file)))
img_names.append(person_dir + '/' + file)
show_img(imgs, img_names)
)
)
1 人脸验证DeepFace.verify
此函数用于验证图像对是同一个人还是不同的人。函数接口为:
verify(img1_path, img2_path = '', model_name = 'VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'opencv', align = True, prog_bar = True, normalization = 'base')
输入参数介绍:
img1_path:传递的图像路径、numpy数组(BGR)或based64编码图像
model_name:模型名,支持VGG-Face, Facenet, OpenFace, DeepFace, DeepID, Dlib, ArcFace,Ensemble等
distance_metric:度量标准,支持cosine, euclidean, euclidean_l2
model:构建deepface模型。每次调用verify函数都会重新建立人脸识别模型。可以选择传递预构建的人脸识别模型。如DeepFace.build_model('VGG-Face')构建模型
enforce_detection:如果在图像中检测不到任何人脸,则验证函数将返回异常。将此设置为False将不会出现此异常
detector_backend:人脸识别算法后端,支持retinaface, mtcnn, opencv, ssd,dlib
align:是否人脸对齐
prog_bar:启用或禁用进度条
normalization:人脸归一化的方式
输出结果介绍:
如果img1_path是输入一张人脸就是返回一个字典,如果输入列表则返回一个字典列表。具体参数如下:
verified:是否同一个人
distance:人脸距离,越小越相似
max_threshold_to_verify:判断为同一个人的阈值
model: 所用模型
similarity_metric: 相似性度量标准
各识别模型的精度如下,LFW和YTF都是小型数据集。Human-beings表示人类识别精度。
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% |
demo1
# 模型名
models_name = ["VGG-Face", "Facenet", "Facenet512", "OpenFace",
"DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", 'Ensemble']
model_name = models_name[5]
result = DeepFace.verify(img1_path="images/baijingting/0001.jpg",
img2_path="images/pengyuyan/0001.jpg",
model_name=model_name)
# 展示结果,两个人不是同一个人
print(result)
1/1 [==============================] - 0s 170ms/step
1/1 [==============================] - 0s 20ms/step
{'verified': False, 'distance': 0.0751386867894902, 'threshold': 0.015, 'model': 'DeepID', 'detector_backend': 'opencv', 'similarity_metric': 'cosine'}
demo2
models_name = ["VGG-Face", "Facenet", "Facenet512", "OpenFace",
"DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", 'Ensemble']
# 提前加载模型,避免重复加载
model_name = models_name[1]
# 创建模型
model = DeepFace.build_model(model_name)
# 列表中每一个子项表示用于对比的图像
img_paths = [["images/baijingting/0000.jpg", "images/baijingting/0001.jpg"],
["images/baijingting/0000.jpg", "images/zhaoliying/0001.jpg"]]
# 度量标准
metrics = ["cosine", "euclidean", "euclidean_l2"]
results = DeepFace.verify(img_paths,
model_name=model_name,
model=model,
distance_metric=metrics[2],
prog_bar=False)
# 展示结果
for result in results.items():
print(result)
1/1 [==============================] - 2s 2s/step
1/1 [==============================] - 0s 52ms/step
1/1 [==============================] - 0s 55ms/step
1/1 [==============================] - 0s 66ms/step
('pair_1', {'verified': True, 'distance': 0.6328494898310356, 'threshold': 0.8, 'model': 'Facenet', 'detector_backend': 'opencv', 'similarity_metric': 'euclidean_l2'})
('pair_2', {'verified': False, 'distance': 1.1700473293978308, 'threshold': 0.8, 'model': 'Facenet', 'detector_backend': 'opencv', 'similarity_metric': 'euclidean_l2'})
2 人脸识别DeepFace.find
此函数用于从数据集中检索当前人脸相似的图片。函数接口为:
find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'opencv', align = True, prog_bar = True, normalization = 'base', silent=False):
输入参数和verify差不多,主要多了人脸检索库路径地址:
db_path:检索库路径,
silent: 是否静默显示数据,
输出结果介绍:
一个包含相似图像的pandas dataframe数据体,包括图像路径和距离值,
models_name = ["VGG-Face", "Facenet", "Facenet512", "OpenFace",
"DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", 'Ensemble']
# db_path是库文件地址
# 第一次会提取各个图像的特征,并保存到本地pkl文件以供下次直接调用
result = DeepFace.find(img_path="images/baijingting/0000.jpg",
db_path="images", model_name=models_name[1])
1/1 [==============================] - 0s 55ms/step
1/1 [==============================] - 0s 64ms/step
1/1 [==============================] - 0s 63ms/step
1/1 [==============================] - 0s 61ms/step
1/1 [==============================] - 0s 64ms/step
1/1 [==============================] - 0s 58ms/step
1/1 [==============================] - 0s 55ms/step
1/1 [==============================] - 0s 65ms/step
1/1 [==============================] - 0s 59ms/step
1/1 [==============================] - 0s 55ms/step
1/1 [==============================] - 0s 51ms/step
1/1 [==============================] - 0s 52ms/step
1/1 [==============================] - 0s 53ms/step
1/1 [==============================] - 0s 52ms/step
1/1 [==============================] - 0s 55ms/step
Representations stored in images / representations_facenet.pkl file. Please delete this file when you add new identities in your database.
1/1 [==============================] - 0s 56ms/step
find function lasts 3.254298448562622 seconds
# 展示结果,第一个是识别图像本身,后面两个是相似图片
print(result)
identity Facenet_cosine
0 images\baijingting/0000.jpg -2.220446e-16
1 images\baijingting/0001.jpg 2.002492e-01
2 images\baijingting/0002.jpg 2.328966e-01
3 人脸属性分析DeepFace.analyze
此函数用于分析当前人脸的面部属性,包括年龄,性别,面部表情(包括愤怒、恐惧、正常、悲伤、厌恶、快乐和惊讶),种族(包括亚洲人、白人、中东人、印度人、拉丁裔和黑人)。函数接口为:
analyze(img_path, actions = ('emotion', 'age', 'gender', 'race') , models = None, enforce_detection = True, detector_backend = 'opencv', prog_bar = True)
输入参数和verify差不多,主要多了属性设置actions:
actions:识别属性,包括age, gender, emotion, race
输出结果介绍:
如果img_path是输入一张人脸就是返回一个字典,如果输入列表则返回一个字典列表。具体参数如下:
region:人脸坐标,wywh格式
age:年龄
gender:性别
dominant_emotion: 主导情绪,也就是情绪识别结果
emotion:各个情绪度量值,值越大表示越倾向
dominant_race:种族结果
race:各个种族度量值
# 输入检测图像,这里只识别情绪,因为其他模型实在太大了,下载下来要很久。
result = DeepFace.analyze(img_path = "images/jiangwen/0000.jpg", actions = ['emotion'])
print(result)
1/1 [==============================] - 0s 113ms/step
{'emotion': {'angry': 2.147514166495057e-06, 'disgust': 3.124029827739067e-14, 'fear': 1.990160924947304e-06, 'happy': 99.9697208404541, 'sad': 1.9864262412738753e-05, 'surprise': 0.01537421194370836, 'neutral': 0.014887277211528271}, 'dominant_emotion': 'happy', 'region': {'x': 198, 'y': 34, 'w': 185, 'h': 185}}
数据可视化看看结果
im = Image.open( "images/jiangwen/0000.jpg")
# 坐标位置
x,y,w,h = result['region']['x'],result['region']['y'],result['region']['w'],result['region']['h']
draw = ImageDraw.Draw(im)
# 画框
draw.rectangle((x,y,x+w,y+h), outline="red", width=3)
print("表情:{}".format(result["dominant_emotion"]))
show_img([im],["jiangwen"])
表情:happy
4 人脸检测DeepFace.detectFace
此函数用于检测人脸,如果图像中有多个人脸只会返回一个,函数接口为:
detectFace(img_path, target_size = (224, 224), detector_backend = 'opencv', enforce_detection = True, align = True)
输入参数和verify差不多,主要多了可以设置返回图像的尺寸的参数target_size,输出返回一张RGB的numpy数组图像
result = DeepFace.detectFace(img_path = "images/zhangziyi/0000.jpg",align = True)
print(result.shape)
show_img([result],["zhangziyi"])
(224, 224, 3)
# 不进行人脸对齐
result = DeepFace.detectFace(img_path = "images/zhangziyi/0000.jpg",align = False)
print(result.shape)
show_img([result],["zhangziyi"])
(224, 224, 3)
5 人脸特征提取DeepFace.represent
该函数用于将面部图像表示为特征向量,函数接口为:
represent(img_path, model_name = 'VGG-Face', model = None, enforce_detection = True, detector_backend = 'opencv', align = True, normalization = 'base')
输入参数和verify差不多。输出返回图像特征多维向量,特征向量的维度根据模型而变化。
models_name = ["VGG-Face", "Facenet", "Facenet512", "OpenFace",
"DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", 'Ensemble']
result = DeepFace.represent(img_path="images/baijingting/0000.jpg", model_name=models_name[1])
print("特征维度为:{}".format(len(result)))
1/1 [==============================] - 0s 61ms/step
特征维度为:128
当然提取特征可以自己计算距离,设置阈值。示例如下。
# 计算l2距离
def l2_distance(input1: np.ndarray, input2: np.ndarray) -> float:
# 手动计算 np.sqrt(np.sum((result1- result2)**2))
return np.linalg.norm(input1-input2)
# 计算l1距离
def l1_distance(input1: np.ndarray, input2: np.ndarray) -> float:
# 手动计算 np.sum(abs(input1-input2))
return np.linalg.norm(input1-input2, ord=1)
# 计算余弦距离
def IP_distance(input1: np.ndarray, input2: np.ndarray) -> float:
return 1 - np.dot(input1, input2)/np.linalg.norm(input1)/np.linalg.norm(input2)
models_name = ["VGG-Face", "Facenet", "Facenet512", "OpenFace",
"DeepFace", "DeepID", "ArcFace", "Dlib", "SFace", 'Ensemble']
# 提前加载模型,避免重复加载
model_name = models_name[1]
# 创建模型
model = DeepFace.build_model(model_name)
# res1和res3为同一个人
res1 = DeepFace.represent(
img_path="images/baijingting/0000.jpg", model_name=models_name[1], model=model)
res2 = DeepFace.represent(
img_path="images/zhangziyi/0000.jpg", model_name=models_name[1], model=model)
res3 = DeepFace.represent(
img_path="images/baijingting/0001.jpg", model_name=models_name[1], model=model)
# 转换为numpy类型
res1 = np.array(res1)
res2 = np.array(res2)
res3 = np.array(res3)
print("res1与res2的余弦距离为:{}".format(IP_distance(res1,res2)))
print("res1与res3的余弦距离为:{}".format(IP_distance(res1,res3)))
print("res1与res2的l2距离为:{}".format(l2_distance(res1,res2)))
print("res1与res3的l2距离为:{}".format(l2_distance(res1,res3)))
print("res1与res2的l1距离为:{}".format(l1_distance(res1,res2)))
print("res1与res3的l1距离为:{}".format(l1_distance(res1,res3)))
1/1 [==============================] - 0s 54ms/step
1/1 [==============================] - 0s 62ms/step
1/1 [==============================] - 0s 52ms/step
res1与res2的余弦距离为:0.6868675298615137
res1与res3的余弦距离为:0.2002492383897012
res1与res2的l2距离为:12.135816884638682
res1与res3的l2距离为:6.657409646028565
res1与res2的l1距离为:110.3180431430228
res1与res3的l1距离为:58.20380371063948
6 参考
- ??deepface??
- ??deepface_models??
- [深度学习] Python人脸识别库face_recognition使用教程
- ??Python-Study-Notes??
相关推荐
- Python 数据分析——利用Pandas进行分组统计
-
话说天下大势,分久必合,合久必分。数据分析也是如此,我们经常要对数据进行分组与聚合,以对不同组的数据进行深入解读。本章将介绍如何利用Pandas中的GroupBy操作函数来完成数据的分组、聚合以及统计...
- python数据分析:介绍pandas库的数据类型Series和DataFrame
-
安装pandaspipinstallpandas-ihttps://mirrors.aliyun.com/pypi/simple/使用pandas直接导入即可importpandasas...
- 使用DataFrame计算两列的总和和最大值_[python]
-
【如果对您有用,请关注并转发,谢谢~~】最近在处理气象类相关数据的空间计算,在做综合性计算的时候,DataFrame针对每列的统计求和、最大值等较为方便,对某行的两列或多列数据进行求和与最大值等的简便...
- 8-Python内置函数
-
Python提供了丰富的内置函数,这些函数可以直接使用而无需导入任何模块。以下是一些常用的内置函数及其示例:1-print()1-1-说明输出指定的信息到控制台。1-2-例子2-len()2-1-说...
- Python中函数式编程函数: reduce()函数
-
Python中的reduce()函数是一个强大的工具,它通过连续地将指定的函数应用于序列(如列表)来对序列(如列表)执行累积操作。它是functools模块的一部分,这意味着您需要在使用它之...
- 万万没想到,除了香农计划,Python3.11竟还有这么多性能提升
-
众所周知,Python3.11版本带来了较大的性能提升,但是,它具体在哪些方面上得到了优化呢?除了著名的“香农计划”外,它还包含哪些与性能相关的优化呢?本文将带你一探究竟!作者:BeshrKay...
- 最全python3.11版12类75个内置函数大全
-
获取全部内置函数:importbuiltins#导入模块yc=[]#异常属性nc=[]#不可调用fn=[]#内置函数defll(ty=builtins):...
- 软件测试笔试题
-
测试工程师岗位,3-5年,10-14k1.我司有一款产品,类似TeamViewer,向日葵,mstsc,QQ远程控制产品,一个PC客户端产品,请设想一下测试要点。并写出2.写出常用的SQL语句8条,l...
- 备战各大互联网巨头公司招聘会,最全Python面试大全,共300题
-
前言众所周知,越是顶尖的互联网公司在面试这一part的要求就越高,需要你有很好的技术功底、项目经验、一份漂亮的简历,当然还有避免不了的笔试过关。对于Python的工程师来说,全面掌握好有关Python...
- 经典 SQL 数据库笔试题及答案整理
-
马上又是金三银四啦,有蛮多小伙伴在跳槽找工作,但对于年限稍短的软件测试工程师,难免会需要进行笔试,而在笔试中,基本都会碰到一道关于数据库的大题,今天这篇文章呢,就收录了下最近学员反馈上来的一些数据库笔...
- 用Python开发日常小软件,让生活与工作更高效!附实例代码
-
引言:Python如何让生活更轻松?在数字化时代,编程早已不是程序员的专属技能。Python凭借其简洁易学的特点,成为普通人提升效率、解决日常问题的得力工具。无论是自动化重复任务、处理数据,还是开发个...
- 太牛了!102个Python实战项目被我扒到了!建议收藏!
-
挖到宝了!整整102个Python实战项目合集,从基础语法到高阶应用全覆盖,附完整源码+数据集,手把手带你从代码小白变身实战大神!这波羊毛不薅真的亏到哭!超全项目库,学练一站式搞定这份资...
- Python中的并发编程
-
1.Python对并发编程的支持多线程:threading,利用CPU和IO可以同时执行的原理,让CPU不会干巴巴等待IO完成。多进程:multiprocessing,利用多核CPU...
- Python 也有内存泄漏?
-
1.背景前段时间接手了一个边缘视觉识别的项目,大功能已经开发的差不多了,主要是需要是优化一些性能问题。其中比较突出的内存泄漏的问题,而且不止一处,有些比较有代表性,可以总结一下。为了更好地可视化内存...
- python爬虫之多线程threading、多进程、协程aiohttp批量下载图片
-
一、单线程常规下载常规单线程执行脚本爬取壁纸图片,只爬取一页的图片。importdatetimeimportreimportrequestsfrombs4importBeautifu...
你 发表评论:
欢迎- 一周热门
-
-
python 3.8调用dll - Could not find module 错误的解决方法
-
加密Python源码方案 PyArmor(python项目源码加密)
-
Python3.8如何安装Numpy(python3.6安装numpy)
-
大学生机械制图搜题软件?7个受欢迎的搜题分享了
-
编写一个自动生成双色球号码的 Python 小脚本
-
免费男女身高在线计算器,身高计算公式
-
将python文件打包成exe程序,复制到每台电脑都可以运行
-
Python学习入门教程,字符串函数扩充详解
-
Python数据分析实战-使用replace方法模糊匹配替换某列的值
-
Python进度条显示方案(python2 进度条)
-
- 最近发表
- 标签列表
-
- python计时 (54)
- python安装路径 (54)
- python类型转换 (75)
- python进度条 (54)
- python的for循环 (56)
- python串口编程 (60)
- python写入txt (51)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python qt (52)
- python人脸识别 (54)
- python斐波那契数列 (51)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- centos7安装python (53)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)