摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名。2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片。3. 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁,1行代码足以!!!

环境要求:

环境搭建:

1. 安装 Ubuntu17.10 > 安装步骤在这里

2. 安装 Python2.7.14 (Ubuntu17.10 默认Python版本为2.7.14)

3. 安装 git 、cmake 、 python-pip

# 安装 git
$ sudo apt-get install -y git
# 安装 cmake
$ sudo apt-get install -y cmake
# 安装 python-pip
$ sudo apt-get install -y python-pip

4. 安装编译dlib

安装face_recognition这个之前需要先安装编译dlib

# 编译dlib前先安装 boost
$ sudo apt-get install libboost-all-dev # 开始编译dlib
# 克隆dlib源代码
$ git clone https://github.com/davisking/dlib.git
$ cd dlib
$ mkdir build
$ cd build
$ cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
$ cmake --build .(注意中间有个空格)
$ cd ..
$ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

5. 安装 face_recognition

# 安装 face_recognition
$ pip install face_recognition
# 安装face_recognition过程中会自动安装 numpy、scipy 等

环境搭建完成后,在终端输入 face_recognition 命令查看是否成功

实现人脸识别:


示例一(1行代码实现人脸识别):

1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名:

known_people文件夹下有babe、成龙、容祖儿的照片

2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片:

unknown_pic文件夹下是要识别的图片,其中韩红是机器不认识的

3. 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁:

识别成功!!!


示例二(识别图片中的所有人脸并显示出来):

# filename : find_faces_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition # 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg") # 使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
# 另请参见: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image) # 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn") # 打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations))) # 循环找到的所有人脸
for face_location in face_locations: # 打印每张脸的位置信息
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # 指定人脸的位置信息,然后显示人脸图片
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()

如下图为用于识别的图片

# 执行python文件
$ python find_faces_in_picture.py

从图片中识别出7张人脸,并显示出来,如下图


示例三(自动识别人脸特征):

# filename : find_facial_features_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition # 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("biden.jpg") #查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image) print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) for face_landmarks in face_landmarks_list: #打印此图像中每个面部特征的位置
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
] for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature])) #让我们在图像中描绘出每个人脸特征!
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image) for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=5) pil_image.show()

自动识别出人脸特征(轮廓)


示例四(识别人脸鉴定是哪个人):

# filename : recognize_faces_in_pictures.py
# -*- conding: utf-8 -*-
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition #将jpg文件加载到numpy数组中
babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")
Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg")
unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg") #获取每个图像文件中每个面部的面部编码
#由于每个图像中可能有多个面,所以返回一个编码列表。
#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。
babe_face_encoding = face_recognition.face_encodings(babe_image)[0]
Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0] known_faces = [
babe_face_encoding,
Rong_zhu_er_face_encoding
] #结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果
results = face_recognition.compare_faces(known_faces, unknown_face_encoding) print("这个未知面孔是 Babe 吗? {}".format(results[0]))
print("这个未知面孔是 容祖儿 吗? {}".format(results[1]))
print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

显示结果下如图


示例五(识别人脸特征并美颜):

# filename : digital_makeup.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition #将jpg文件加载到numpy数组中
image = face_recognition.load_image_file("biden.jpg") #查找图像中所有面部的所有面部特征
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') #让眉毛变成了一场噩梦
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()

美颜前后对比如下图

简单机器学习人脸识别工具face-recognition python小试,一行代码实现人脸识别的更多相关文章

  1. Python人脸识别最佳教材典范,40行代码搭建人脸识别系统!

    Face Id是一款高端的人脸解锁软件,官方称:"在一百万张脸中识别出你的脸."百度.谷歌.腾讯等各大企业都花费数亿来鞭策人工智能的崛起,而实际的人脸识别技术是否有那么神奇? 绿帽 ...

  2. 10 种机器学习算法的要点(附 Python 和 R 代码)

    本文由 伯乐在线 - Agatha 翻译,唐尤华 校稿.未经许可,禁止转载!英文出处:SUNIL RAY.欢迎加入翻译组. 前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关 ...

  3. python的__slots__节约内存的魔法;检查python每一行代码内存占用情况的工具

    在Python中,每个类都有实例属性.默认情况下Python用一个字典来保存一个对象的实例属性.这非常有用,因为它允许我们在运行时去设置任意的新属性. 然而,对于有着已知属性的小类来说,它可能是个瓶颈 ...

  4. python:一行代码实现局域网共享文件

    其实就是使用python内置的一个模块http server 在python2中是下面这样的 python -m SimpleHTTPServer 80 解释下上面的参数,-m表示让python使用一 ...

  5. python实战===一行代码就能搞定的事情!

    打印9*9乘法表: >>> print( '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) ...

  6. Python之一行代码将网址URL转换成动态彩色二维码

    先在 pycharm 安装 myqr.或者,Python3 必装,然后命令行 pip install myqr  也可. 将我的微信公众号网址:http://weixin.qq.com/r/hRMQC ...

  7. python截图+百度ocr(图片识别)+ 百度翻译

    一直想用python做一个截图并自动翻译的工具,恰好最近有时间就在网上找了资料,根据资料以及自己的理解做了一个简单的截图翻译工具.整理一下并把代码放在github给大家参考.界面用python自带的G ...

  8. gotoscan:CMS指纹识别工具

    gotoscan 前言 项目地址 https://github.com/newbe3three/gotoscan 结合自己学习到的Go相关知识,通过实现这个简易的CMS指纹识别工具来锻炼一下自己写代码 ...

  9. 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】

    [写在前面] 有时候会与同事共享文件,正常人的操作是鼠标右键,点击共享.其实有个装逼的方法,用python的一行代码快速实现基于http服务的共享方式. [效果如下] [示例代码] 在cmd窗口进入想 ...

随机推荐

  1. Python学习笔记(三十四)—内置模块(3)base64

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431954588 ...

  2. js、php本周第一天和本周最后一天

    PHP:本周一 echo date('Y-m-d',(time()-((date('w')==0?7:date('w'))-1)*24*3600)); //w为星期几的数字形式,这里0为周日 本周日 ...

  3. Price(洛谷P4109 [HEOI2015]定价)

    题目 思路: 按照我的思路这一题应该是这样子的 剔除+判断 剔除 因为后面的0要越多越好,所以我们判断0出现的情况,当2个数之间的差大与10时,证明2个之间会存在一个0,所以这一位我们可以把它去掉,相 ...

  4. CF148A Insomnia cure

    公主睡前数龙, 每隔k, l, m, n只都会用不同的技能攻击龙. 假定共数了d只龙, 问共有多少龙被攻击了. 思路: 用一个visit数组记录被攻击过的dragon, 最后遍历visit数组统计被攻 ...

  5. L - Looking for Taste Gym - 101991L 二进制枚举/思维

    方法一:因为最多是10的六次方,所以可以直接枚举二进制上的每一位来得到最优结果. AC代码: #include<iostream> #include<stack> #inclu ...

  6. 阿里云一键web环境包

    下载地址:https://files.cnblogs.com/files/wordblog/af3a48ef-3a13-479e-85c9-ead61173126c.zip 先把安装包传到服务器上用w ...

  7. caffe Python API 之Accuracy

    net.acc = caffe.layers.Accuracy(net.fc3,net.label) 输出: layer { name: "acc" type: "Acc ...

  8. NFS生产场景优化

    1.硬件上多块网卡bond,增加吞吐量,至少千兆.sas/ssd磁盘组raid5或raid10 2.服务端配置:/data 172.16.1.0/24(rw,sync,all_squash,anonu ...

  9. Nginx常见错误及处理方法

    转载:https://www.cnblogs.com/liyongsan/p/6795851.html 404 bad request 一般原因:请求的Header过大 解决方法:配置nginx.co ...

  10. UFT12.续期的操作方法

    安装完毕UFT后,页面中报install错误,此时报此错误的原因是因为UFT的许可证过期了,解决方法如下: 方法是找到C:\ProgramData目录下的SafeNet Sentinel文件夹将其删除 ...