Python 使用 face_recognition 人脸识别
Python 使用 face_recognition 人脸识别
官方说明:https://face-recognition.readthedocs.io/en/latest/readme.html
人脸识别
face_recognition 是世界上最简单的人脸识别库。
使用 dlib 最先进的人脸识别功能构建建立深度学习,该模型准确率在99.38%。
Python模块的使用
Python可以安装导入 face_recognition 模块轻松操作,对于简单的几行代码来讲,再简单不过了。
Python操作 face_recognition API 文档:https://face-recognition.readthedocs.io/en/latest/face_recognition.html
自动查找图片中的所有面部
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image)
# face_locations is now an array listing the co-ordinates of each face!
还可以选择更准确的给予深度学习的人脸检测模型
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image, model="cnn")
# face_locations is now an array listing the co-ordinates of each face!
自动定位图像中人物的面部特征
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
# face_landmarks_list is now an array with the locations of each facial feature in each face.
# face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye.
识别图像中的面部并识别它们是谁
import face_recognition
picture_of_me = face_recognition.load_image_file("me.jpg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
unknown_picture = face_recognition.load_image_file("unknown.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
# Now we can see the two face encodings are of the same person with `compare_faces`!
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
if results[0] == True:
print("It's a picture of me!")
else:
print("It's not a picture of me!")
face_recognition 用法
要在项目中使用面部识别,首先导入面部识别库,没有则安装:
import face_recognition
基本思路是首先加載圖片:
# 导入人脸识别库
import face_recognition # 加载图片
image = face_recognition.load_image_file("1.jpg")
上面这一步会将图像加载到 numpy 数组中,如果已经有一个 numpy 数组图像则可以跳过此步骤。
然后对图片进行操作,例如找出面部、识别面部特征、查找面部编码:
例如对此照片进行操作

# 导入人脸识别库
import face_recognition # 加载图片
image = face_recognition.load_image_file("1.jpg") # 查找面部
face_locations = face_recognition.face_locations(image)
# 查找面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
# 查找面部编码
list_of_face_encodings = face_recognition.face_encodings(image) # 打印输出
print(face_locations)
print(face_landmarks_list)
print(list_of_face_encodings)
/usr/bin/python3. /home/wjw/PycharmProjects/face_study/face0112/find_face.py
[(, , , )]
[{'chin': [(, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, )], 'left_eyebrow': [(, ), (, ), (, ), (, ), (, )], 'right_eyebrow': [(, ), (, ), (, ), (, ), (, )], 'nose_bridge': [(, ), (, ), (, ), (, )], 'nose_tip': [(, ), (, ), (, ), (, ), (, )], 'left_eye': [(, ), (, ), (, ), (, ), (, ), (, )], 'right_eye': [(, ), (, ), (, ), (, ), (, ), (, )], 'top_lip': [(, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, )], 'bottom_lip': [(, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, )]}]
[array([-0.14088562, 0.00503807, 0.00270613, -0.07196694, -0.13449337,
-0.07765003, -0.03745099, -0.09381913, 0.12006464, -0.14438102,
0.13404925, -0.06327219, -0.17859964, -0.05488868, -0.02019649,
0.1671212 , -0.1643257 , -0.12276072, -0.03441665, -0.05535197,
0.10760178, 0.04479133, -0.06407147, 0.0689199 , -0.11934121,
-0.32660219, -0.07756624, -0.06931646, 0.04064362, -0.05714978,
-0.0353414 , 0.0762421 , -0.18261658, -0.07098956, 0.02025999,
0.13947421, -0.00086442, -0.05380288, 0.17013952, 0.03612047,
-0.24374251, 0.02234841, 0.06126914, 0.25475574, 0.11198805,
0.01954928, 0.01119124, -0.10833667, 0.14647615, -0.14495029,
-0.00890255, 0.12340544, 0.05062022, 0.07525564, 0.0184714 ,
-0.0970083 , 0.07874238, 0.09881058, -0.15751837, 0.02846039,
0.0963228 , -0.07531998, -0.0176545 , -0.07000162, 0.25344211,
0.03867894, -0.09201257, -0.1658347 , 0.12261658, -0.1535762 ,
-0.15940444, 0.04406216, -0.12239387, -0.10966937, -0.30615237,
-0.00739088, 0.39348996, 0.108335 , -0.20034787, 0.08009379,
-0.05592394, -0.0375729 , 0.23610245, 0.16506384, 0.03575533,
0.04828007, -0.04044699, 0.01277492, 0.25646573, -0.00142263,
-0.04078939, 0.18071812, 0.0617944 , 0.12697747, 0.02988701,
-0.00425877, -0.07669616, 0.00568433, -0.10959606, -0.03289849,
0.08964096, -0.00859835, 0.00752143, 0.14310959, -0.14807181,
0.18848835, 0.03889544, 0.0564449 , 0.03094865, 0.05897319,
-0.11886788, -0.03628988, 0.09417973, -0.20971358, 0.22439443,
0.18054837, 0.0444049 , 0.06860743, 0.1211487 , 0.02242998,
-0.01343671, -0.00214755, -0.24110457, -0.03643485, 0.13142672,
-0.05264375, 0.09808614, 0.00694137])] Process finished with exit code
可以将面部编码相互比较以查看面部是否匹配,注意:查找面部编码有点慢,因此如果在以后还需对次图片进行面部分析参考,建议将每个图片的结果存留缓存或存储进数据库。
一旦得到面部编码,便可以比较他们
# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_face_encodings, a_single_unknown_face_encoding)
就是这么简单!
face_recognition 模块内容
batch_face_locations 使用CNN人脸检测器返回图像中人脸边界框的二维数组
face_recognition.batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128)
使用CNN人脸检测器返回图像中人脸边界框的二维数组
如果您使用的是GPU,这可以提供更快的结果,因为GPU
可以一次处理一批图像。如果你不使用GPU,你就不需要这个功能。
:param img:图像列表(每个图像都是一个numpy数组)
:param number_of_times_to_upsample:要对图像进行多少次upsample以查找面。数字越大,面越小。
:param batch_size:每个GPU处理批中要包含多少图像。
:return:按css(上、右、下、左)顺序找到的面位置的元组列表
compare_faces 将人脸编码列表与候选编码进行比较,看它们是否匹配。
face_recognition.compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)
将人脸编码列表与候选编码进行比较,看它们是否匹配。
:param known_face_encodings:已知人脸编码列表
:param face_encoding_to_check:要与列表进行比较的单个面编码
:param tolerance:面与面之间的距离应视为匹配。越低越严格。0.6是典型的最佳性能。
:return:一个true/false值列表,指示要检查的已知面部编码与面部编码匹配。
face_distance 给定面部编码列表,将它们与已知的面部编码进行比较,并获得每个比较面部的欧氏距离。距离告诉您脸部的相似程度。
face_recognition.face_distance(face_encodings, face_to_compare)
给定面部编码列表,将它们与已知的面部编码进行比较,并获得每个比较面部的欧氏距离。距离告诉您脸部的相似程度。
:param faces:要比较的面编码列表
:param face_to_compare:要与之比较的人脸编码
:返回:一个numpy ndarray,每个面的距离与“faces”数组的顺序相同。
face_encodings 给定的图像,返回的128维编码每个脸对脸的形象。
face_recognition.face_encodings(face_image, known_face_locations=None, num_jitters=1)
给定的图像,返回的128维编码每个脸对脸的形象。
参数:face_image:图像是包含一个或多个面
参数:known_face_locations:可选的如果您已经知道每个面的边界框。
参数:num_jitters:计算编码时重新采样面的次数。更高更准确,但更慢(即100慢100倍)
参数:return:128维面部编码列表(图像中每个面部一个)
face_landmarks 给定图像,返回图像中每个面部的面部特征位置(眼睛,鼻子等)的字典。
face_recognition.face_landmarks(face_image,face_locations = None,model ='large' )
给定图像,返回图像中每个面部的面部特征位置(眼睛,鼻子等)的字典。
face_image - 要搜索的图像
face_locations - 可选择提供要检查的面部位置列表。
model - 可选 - 要使用的模型。“大”(默认)或“小”,只返回5分但速度更快。
return:面部特征位置(眼睛,鼻子等)的序列表
face_locations 返回图像中人脸边界框的数组。
face_recognition.face_locations(img,number_of_times_to_upsample = 1,model ='hog' )
返回图像中人脸边界框的数组。
img - 一个图像(作为一个numpy数组)
number_of_times_to_upsample - 对图像进行上采样以查找面部的次数。数字越大,面部越小。
model - 使用哪种人脸检测模型。“hog”不太准确,但在CPU上更快。“cnn”是一种更准确的深度学习模型,它是GPU / CUDA加速(如果可用)。默认为“hog”。
return:css(顶部,右侧,底部,左侧)顺序中找到的面部位置的元组列表
load_image_file 将图像文件(.jpg,.png等)加载到numpy数组中。
face_recognition.load_image_file(file,mode ='RGB' )
将图像文件(.jpg,.png等)加载到numpy数组中。
file - 要加载的图像文件名或文件对象
mode - 将图像转换为的格式。仅支持“RGB”(8位RGB,3个通道)和“L”(黑色和白色)
return:图像内容为numpy数组。
完成!
Python 使用 face_recognition 人脸识别的更多相关文章
- Python 人工智能之人脸识别 face_recognition 模块安装
Python人工智能之人脸识别face_recognition安装 face_recognition 模块使用系统环境搭建 系统环境 Ubuntu / deepin操作系统 Python 3.6 py ...
- Python的开源人脸识别库:离线识别率高达99.38%
Python的开源人脸识别库:离线识别率高达99.38% github源码:https://github.com/ageitgey/face_recognition#face-recognitio ...
- Python的开源人脸识别库:离线识别率高达99.38%(附源码)
Python的开源人脸识别库:离线识别率高达99.38%(附源码) 转https://cloud.tencent.com/developer/article/1359073 11.11 智慧上云 ...
- 模块 face_recognition 人脸识别
face_recognition 人脸识别 api 说明 1 load_image_file 将img文件加载到numpy 数组中 2 face_locations 查找图像中所有面部和所有面部特征的 ...
- python 与 百度人脸识别api
用python来做人脸识别代码量少 思路清晰, 在使用之前我们需要在我们的配置的编译器中通过pip install baidu-aip 即可 from aip import AipFac ...
- 基于Python的开源人脸识别库:离线识别率高达99.38%
项目地址:https://github.com/ageitgey/face_recognition#face-recognition 本文的模型使用了C++工具箱dlib基于深度学习的最新人脸识别方法 ...
- 用 20 行 python 代码实现人脸识别!
点击上方"Python编程与实战",选择"置顶公众号" 第一时间获取 Python 技术干货! 阅读文本大概需要 11分钟. 今天给大家介绍一个世界上最简洁的人 ...
- 主成分分析 (PCA) 与其高维度下python实现(简单人脸识别)
Introduction 主成分分析(Principal Components Analysis)是一种对特征进行降维的方法.由于观测指标间存在相关性,将导致信息的重叠与低效,我们倾向于用少量的.尽可 ...
- [转]7行Python代码的人脸识别
https://blog.csdn.net/wireless_com/article/details/64120516 随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域 ...
随机推荐
- 命令行方式调用winrar对文件夹进行zip压缩示例代码
调用winRAR进行压缩 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- 编译用到boost相关的东西,问题的解决;以及和googletest库
编译https://github.com/RAttab/reflect, 发现需要gcc4.7以上的版本才行.于是编译安装最新的gcc-6.2.0, 过程算顺利. http://www.linuxfr ...
- Response.Redirect & window.location.href
对接中信的微信H5支付时,对方(其实是微信)需要对我们的域名进行授权,即,我方需向渠道报备支付域名,微信只认可由此域名发起的支付交易. 支付中心只提供了一套支付接口供下游系统访问.因为给渠道报备的域名 ...
- python3学习笔记之安装
一.Python安装 1.下载地址: https://www.python.org/downloads/release/python-365/ 2. Linux系统自带Python2.7,如需安装3 ...
- docker tool
安装docker tool http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 安装1.8.3 有把vbox等服务器地址加 ...
- Hibarnate控制台打印不出sql,并且报出异常:org.hibernate.exception.JDBCConnectionException: Cannot open connection
1.认真查看hibarnate.cfg.xml文件中连接数据库的各个信息是否正确;如果正确看下一步; 2.MySQL版本>=5.6.X,对应的mysql-connector-java jar 的 ...
- Hotfix
http://group.jobbole.com/6311/ http://www.jianshu.com/p/6f0ae1e364d9 http://www.mamicode.com/info-de ...
- 六 js函数和this
js的所有代码都是由funtion组成,funtion即函数的类型. 一.函数有两种写法 -----1.定义式 function test() { //定义一个函数 console.log(" ...
- 09 查找列表中元素,移除每个元素的空格,并查找以a或A开头并且以c结尾的所有元素
li = ["alex"," aric","Alex","Tony","rain"]for i in ...
- jQuery选择器--#id、element和.class
#id 描述 根据给定的ID匹配一个元素.使用任何的元字符作为名称的文本部分, 它必须被两个反斜杠转义:\\ 参数 id 用于搜索的,通过元素的 id 属性中给定的值 element 概述 根 ...