GITHUB地址:https://github.com/ageitgey/face_recognition
安装
环境配置
- Python 3.3+ or Python 2.7
- macOS or Linux
- Windows并不是我们官方支持的,但也许也能用
不同操作系统的安装方法
在 Mac 或者 Linux上安装本项目
First, make sure you have dlib already installed with Python bindings:
第一步,安装dlib和相关Python依赖:
Then, install this module from pypi using pip3
(or pip2
for Python 2):
pip3 install face_recognition
如果你遇到了幺蛾子,可以用Ubuntu虚拟机安装本项目,看下面这个教程。 如何使用Adam Geitgey大神提供的Ubuntu虚拟机镜像文件安装配置虚拟机,本项目已经包含在镜像中.
在 Mac 或者 Linux上安装本项目 2
修改你的pip镜像源为清华镜像,然后使用pip install face_recognition
,可以自动帮你安装各种依赖,包括dlib。只是在安装dlib的时候可能会出问题,因为dlib需要编译,出现的问题一般是gcc
或者g++
版本的问题,所以在pip install face_recognition
之前,可以通过在命令行键入
export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
来指定你gcc和g++对应的位置,(这两句话会临时修改当前终端的环境变量/usr/local/bin/gcc对应你自己gcc或者g++所在目录)。
在树莓派上安装
在Windows上安装
虽然本项目官方并不支持Windows,但一些大神们摸索出了在Windows上运行本项目的方法:
使用Ubuntu虚拟机镜像文件安装配置虚拟机,本项目已经包含在这个镜像中
- 如何使用Adam Geitgey大神提供的Ubuntu虚拟机镜像文件安装配置虚拟机,本项目已经包含在镜像中(需要电脑中安装VMWare Player 或者 VirtualBox)
使用方法
命令行界面
当你安装好了本项目,你可以使用两种命令行工具:
face_recognition
- 在单张图片或一个图片文件夹中认出是谁的脸。face_detection
- 在单张图片或一个图片文件夹中定位人脸位置。
face_recognition
命令行工具
face_recognition
命令行工具可以在单张图片或一个图片文件夹中认出是谁的脸。
首先,你得有一个你已经知道名字的人脸图片文件夹,一个人一张图,图片的文件名即为对应的人的名字:

然后,你需要第二个图片文件夹,文件夹里面是你希望识别的图片:

然后,你在命令行中切换到这两个文件夹所在路径,然后使用face_recognition
命令行,传入这两个图片文件夹,然后就会输出未知图片中人的名字:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
输出结果的每一行对应着图片中的一张脸,图片名字和对应人脸识别结果用逗号分开。
如果结果输出了unknown_person
,那么代表这张脸没有对应上已知人脸图片文件夹中的任何一个人。
face_detection
命令行工具
face_detection
命令行工具可以在单张图片或一个图片文件夹中定位人脸位置(输出像素点坐标)。
在命令行中使用face_detection
,传入一个图片文件夹或单张图片文件来进行人脸位置检测:
$ face_detection ./folder_with_pictures/ examples/image1.jpg,65,215,169,112 examples/image2.jpg,62,394,211,244 examples/image2.jpg,95,941,244,792
输出结果的每一行都对应图片中的一张脸,输出坐标代表着这张脸的上、右、下、左像素点坐标。
调整人脸识别的容错率和敏感度
如果一张脸识别出不止一个结果,那么这意味着他和其他人长的太像了(本项目对于小孩和亚洲人的人脸识别准确率有待提升)。你可以把容错率调低一些,使识别结果更加严格。
通过传入参数 --tolerance
来实现这个功能,默认的容错率是0.6,容错率越低,识别越严格准确。
$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
如果你想看人脸匹配的具体数值,可以传入参数 --show-distance true
:
$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785 /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
更多的例子
如果你并不在乎图片的文件名,只想知道文件夹中的图片里有谁,可以用这个管道命令:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2 Barack Obama unknown_person
加速人脸识别运算
如果你的CPU是多核的,你可以通过并行运算加速人脸识别。例如,如果你的CPU有四个核心,那么你可以通过并行运算提升大概四倍的运算速度。
如果你使用Python3.4或更新的版本,可以传入 --cpus <number_of_cpu_cores_to_use>
参数:
$ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
你可以传入 --cpus -1
参数来调用cpu的所有核心。
子豪兄批注:树莓派3B有4个CPU核心,传入多核参数可以显著提升图片识别的速度(亲测)。
Python 模块:face_recognition
在Python中,你可以导入face_recognition
模块,调用我们提供的丰富的API接口,用几行代码就可以轻松玩转各种人脸识别功能!
API 接口文档: https://face-recognition.readthedocs.io
在图片中定位人脸的位置
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!
你也可以使用深度学习模型达到更加精准的人脸定位。
注意:这种方法需要GPU加速(通过英伟达显卡的CUDA库驱动),你在编译安装dlib
的时候也需要开启CUDA支持。
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!
如果你有很多图片需要识别,同时又有GPU,那么你可以参考这个例子:案例:使用卷积神经网络深度学习模型批量识别图片中的人脸.
识别单张图片中人脸的关键点
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!")
看这个案例 案例:是奥巴马还是拜登?
Python 案例
所有案例都在这个链接中 也就是examples文件夹.
人脸定位
人脸关键点识别
人脸识别
- 案例:是奥巴马还是拜登?
- 案例:人脸识别之后在原图上画框框并标注姓名
- 案例:在不同精度上比较两个人脸是否属于一个人
- 案例:从摄像头获取视频进行人脸识别-较慢版(需要安装OpenCV)
- 案例:从摄像头获取视频进行人脸识别-较快版(需要安装OpenCV)
- 案例:从视频文件中识别人脸并把识别结果输出为新的视频文件(需要安装OpenCV)
- 案例:通过树莓派摄像头进行人脸个数统计及人脸身份识别
- 案例:通过浏览器HTTP访问网络服务器进行人脸识别(需要安装Flask后端开发框架))
- 案例:基于K最近邻KNN分类算法进行人脸识别
常用函数说明
face_recognition 人脸识别
api | 说明 |
---|---|
1 load_image_file | 将img文件加载到numpy 数组中 |
2 face_locations | 查找图像中所有面部和所有面部特征的位置 |
3 batch_face_locations | 批次人脸定位函数(GPU) |
4 face_landmarks | 人脸特征提取函数 |
5 face_encodings | 图像编码转为特征向量 |
6 compare_faces | 特征向量比对 |
7 face_distance | 计算特征向量差值 |
图像载入函数——load_image_file
load_image_file(file, mode='RGB')
加载一个图像文件到一个numpy array类型的对象上。
参数:
file:待加载的图像文件名字
mode:转换图像的格式。只支持“RGB”(8位RGB, 3通道)和“L”(黑白)
返回值:
一个包含图像数据的numpy array类型的对象
人脸编码函数——face_encodings
face_encodings(face_image, known_face_locations=None, num_jitters=1)
给定一个图像,返回图像中每个人脸的128脸部编码(特征向量)。
参数:
face_image:输入的人脸图像
known_face_locations:可选参数,如果你知道每个人脸所在的边界框
num_jitters=1:在计算编码时要重新采样的次数。越高越准确,但速度越慢(100就会慢100倍)
返回值:
一个128维的脸部编码列表
人脸匹配函数——compare_faces
compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)
比较脸部编码列表和候选编码,看看它们是否匹配,设置一个阈值,若两张人脸特征向量的距离,在阈值范围之内,则认为其 是同一个人
参数:
known_face_encodings:已知的人脸编码列表
face_encoding_to_check:待进行对比的单张人脸编码数据
tolerance=0.6:两张脸之间有多少距离才算匹配。该值越小对比越严格,0.6是典型的最佳值
返回值:
一个 True或者False值的列表,该表指示了known_face_encodings列表的每个成员的匹配结果
人脸定位函数——face_locations
face_locations(face_image,number_of_times_to_upsample=1,model="hog")
利用CNN深度学习模型或方向梯度直方图(Histogram of Oriented Gradient, HOG)进行人脸提取。返回值是一个数组(top, right, bottom, left)表示人脸所在边框的四条边的位置。
参数:
face_image:输入的人脸图像
number_of_times_to_upsample=1:从图片的样本中查找多少次人脸,该参数的值越高的话越能发现更小的人脸
model="hog":使用哪种人脸检测模型。“hog” 准确率不高,但是在CPU上运行更快,“cnn” 更准确更深度(且GPU/CUDA加速,如果有GPU支持的话),默认是“hog”
返回值:
一个元组列表,列表中的每个元组包含人脸的四边位置(top, right, bottom, left)
批次人脸定位函数(GPU)——batch_face_locations
batch_face_locations(face_images,number_of_times_to_upsample=1,batch_size=128)
使用CNN人脸检测器返回一个包含人脸特征的二维数组,如果使用了GPU,这个函数能够更快速的返回结果;如果不使用GPU的话,该函数就没必要使用
参数:
face_images:输入多张人脸图像组成的list
number_of_times_to_upsample=1:从图片的样本中查找多少次人脸,该参数的值越高的话越能发现更小的人脸
batch_size=128:每个GPU一次批处理多少个image
返回值:
一个元组列表,列表中的每个元组包含人脸的四边位置(top, right, bottom, left)
人脸特征提取函数——face_landmarks
face_landmarks(face_image,face_locations=None,model="large")
给定一个图像,提取图像中每个人脸的脸部特征位置
参数:
face_image:输入的人脸图片
face_locations=None:可选参数,默认值为None,代表默认解码图片中的每一个人脸。若输入face_locations()[i]可指定人脸进行解码
model="large":输出的特征模型,默认为“large”,可选“small”。当选择为"small"时,只提取左眼、右眼、鼻尖这三种脸部特征。
返回值:
返回值类型为:List[Dict[str,List[Tuple[Any,Any]]]],是由各个脸部特征关键点位置组成的字典记录列表,一个Dict对象对应图片中的一个人脸,其key为某个脸部特征(如输出中的nose_bridge、left_eye等),value是由该脸部特征各个关键点位置组成的List,关键点位置是一个Tuple(如上输出中,nose_bridge对应的关键点位置组成的列表为[(881L, 128L), (880L, 141L), (880L, 154L), (879L, 167L)] )
计算特征相识度差值——face_distance
例子1-人脸识别和特征匹配
import face_recognition
# 加载2张已知面孔的图片
known_obama_image = face_recognition.load_image_file("obama.jpg")
known_biden_image = face_recognition.load_image_file("biden.jpg")
# 计算图片对应的编码
obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]
known_encodings = [
obama_face_encoding,
biden_face_encoding
]
# 加载一张未知面孔的图片
image_to_test = face_recognition.load_image_file("obama2.jpg")
# 计算图片对应的编码
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
# 计算未知图片与已知的2个面孔的距离
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
for i, face_distance in enumerate(face_distances):
print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
print()
结果
The test image has a distance of 0.35 from known image #0 (与#0面孔差异为0.35,在相似度阈值分别为 0.6和0.5的情形下,都可以认为与#0是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? True
- With a very strict cutoff of 0.5, would the test image match the known image? True
The test image has a distance of 0.82 from known image #1(与#1面孔差异为0.82,在相似度阈值分别为 0.6和0.5的情形下,都不认为与#1是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? False
- With a very strict cutoff of 0.5, would the test image match the known image? False
例子2-特征点检测和美颜
import face_recognition
from PIL import Image, ImageDraw
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("tt3.jpg")
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
# Create a PIL imageDraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
# 画个浓眉
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
# 涂个性感的嘴唇
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
# 闪亮的大眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
# 画眼线
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
pil_image.show()
Comments | NOTHING