face_recognition是一个可玩度很高的人脸识别开源框架,不仅安装方便,运行起来也很轻松。
官网:https://github.com/ageitgey/face_recognition.git
<https://github.com/ageitgey/face_recognition.git>

以下我将按标题分享以下我的畅玩经历:

1、安装

* cuda和cudnn
机器视觉必备,这个不过多赘述。

* anaconda2或3
anaconda在官网即可下载:https://www.anaconda.com/download/
<https://www.anaconda.com/download/>
分别自带python2.7和3.6版本。在此我用的是2.7版本
在某些博客会提及python3.6下Dlib无障碍编译,貌似是python35 36的版本不匹配原因。我尝试了下,不论27
36都无障碍编译成功了。因此我认为Dlib编译出问题与否很大可能跟你机器的python配置纯不纯有关系。

* 编译Dlib
前提需要boost和cmake环境,如果有配置过caffe就不用重复了。
github:https://github.com/davisking/dlib <https://github.com/davisking/dlib>
下载如图

解压后在cmd中运行
python setup.py install
cuda编译需要很长时间,大概30分钟,要耐心等待。

* 安装face_recognition
在cmd直接pip install!
pip install face_recognition
提示安装完成后就可以开玩啦!

* opencv
opencv是可选选项,安装后能够提高可玩性

点我直接下载opencv3.4.1
<https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.4.1/opencv-3.4.1-vc14_vc15.exe/download>


安装完成后,将opencv下的.\build\python\2.7\x64的cv2.pyd文件放置到.\Anaconda2\Lib\site-packages文件夹下
注:此处仅有python2.7的提供,其余版本参考百度
其余配置参考https://blog.csdn.net/weixin_39393712/article/details/79583274
<https://blog.csdn.net/weixin_39393712/article/details/79583274>

2、人脸识别尝试

先上代码:
# coding:utf-8 import face_recognition #输入已知图片biden.jpg known_image =
face_recognition.load_image_file("biden.jpg") #输入待识别的图片unknown.jpg
unknown_image = face_recognition.load_image_file("unknown.jpg") biden_encoding
= face_recognition.face_encodings(known_image)[0] unknown_encoding =
face_recognition.face_encodings(unknown_image)[0] results = face_recognition
.compare_faces([biden_encoding], unknown_encoding) #输出的results是一串Boolean值 print
results


若想要识别其余的人,自己动手编写python文件就好了,如此轻松,岂不美哉?
其余还有挺多功能的,可在官网查看

3、API详细

https://face-recognition.readthedocs.io/en/latest/face_recognition.html
<https://face-recognition.readthedocs.io/en/latest/face_recognition.html>
看不懂?没关系,下面我来一一介绍

1、人脸定位——locations
face_locations(img, number_of_times_to_upsample=1, model="hog")
利用CNN深度学习模型或方向梯度直方图(Histogram of Oriented Gradient, HOG)进行人脸提取。返回值是一个数组(top,
right, bottom, left)表示人脸所在边框的四条边的位置。
成员变量:

* img:图片名
* number_of_times_to_upsample=1:数值越大识别的人脸越小,耗时也越长
* model=”hog”:使用的人脸检测模型
此外,人脸定位的方法还有:
_raw_face_locations(img, number_of_times_to_upsample=1, model="hog") _
raw_face_locations_batched(images, number_of_times_to_upsample=1, batch_size=128
) batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128)
读者有兴趣可自行尝试,不一一赘述。

2、人脸解码——encodings
人脸识别的前菜。输入一张图片后,生成一个128维的特征向量,这是 人脸识别的依据。
face_encodings(face_image, known_face_locations=None, num_jitters=1)
成员变量:

* face_image:图片名
*
known_face_locations=None:这个是默认值,默认解码图片中的每一个人脸。若输入face_locations()[i]可指定人脸进行解码。
* num_jitters=1:池化操作。数值越高,精度越高,但耗时越长。
3、人脸比对——compare
人脸识别的核心,设置一个阈值,若两张人脸的特征向量的距离,在阈值范围之内,则认为其是同一个人,最后返回一个Boolean值的list。
compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)
成员变量:

* known_face_encodings:人脸解码方法中返回的特征向量,此处是已知人脸的特征向量。
* face_encoding_to_check:此处是未知人脸的特征向量。
* tolerance=0.6:比对阈值,即容差,默认为0.6。
4、人脸特征向量距离——distance
跟上面的方法同样原理,不过输出由对错改为数字。
face_distance(face_encodings, face_to_compare)
成员变量:

* face_encodings:人脸解码方法中返回的特征向量,此处是已知人脸的特征向量。
* face_to_compare:此处是未知人脸的特征向量。
5、人脸特征提取——landmark
face_landmarks(face_image, face_locations=None, model="large")
成员变量:

* face_image:输入图片。
* face_locations=None:这个是默认值,默认解码图片中的每一个人脸。若输入face_locations()[i]可指定人脸进行解码。
* model=”large”:输出的特征模型,默认为“large”,可选“small”,改为输出5点特征,但速度更快。
6、化妆(简直是恶搞)——think ‘Meitu’
这不是个API,但挺好玩的,就放上来。
think ‘Meitu’是作者自己说的,哈哈,看来天朝的美颜远销海外,连人脸识别专家都忍不住来过一把瘾。不过要是给妹子弄这个,不跪键盘,就是跪榴莲
上代码:
from PIL import Image, ImageDraw import face_recognition # Load the jpg file
into a numpy array image = face_recognition.load_image_file("biden.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: pil_image = Image.fromarray(image) d =
ImageDraw.Draw(pil_image,'RGBA') # Make the eyebrows into a nightmare
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) # Gloss the
lips 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) # Sparkle the eyes
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30)) # Apply some
eyeliner 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()
文章就到这里,本人只是机器视觉的小白,路还有很长,欢迎大家指教!

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信