原标题:手把手教你用Python实现人脸识别,辨别真假!

环境要求:

Ubuntu17.10

Python 2.7.14

环境搭建:

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-Imagingfrom 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()

责任编辑:

python 人脸识别活体检测_手把手教你用Python实现人脸识别,辨别真假!相关推荐

  1. python处理时间序列非平稳_手把手教你用Python处理非平稳时间序列

    简介 预测一个家庭未来三个月的用电量,估计特定时期道路上的交通流量,预测一只股票在纽约证券交易所交易的价格--这些问题都有什么共同点? 它们都属于时间序列数据的范畴!如果没有"时间" ...

  2. python正确的输入语句_手把手教你在python中如何使用while True语句

    在学习过程中,经常能遇到采用while True的用法.下面以一个例子进行说明: 建立一个用户登录系统,用户输入用户名和密码,如果正确就可以进入系统. 1.我自己最开始的写法:d = {} #数据库字 ...

  3. python爬虫爬取房源_手把手教你用Python网络爬虫爬取新房数据

    项目背景 大家好,我是J哥. 新房数据,对于房地产置业者来说是买房的重要参考依据,对于房地产开发商来说,也是分析竞争对手项目的绝佳途径,对于房地产代理来说,是踩盘前的重要准备. 今天J哥以「惠民之家」 ...

  4. 如何用python抓取qq音乐_手把手教你使用Python抓取QQ音乐数据(第三弹)-阿里云开发者社区...

    [一.项目目标] 通过手把手教你使用Python抓取QQ音乐数据(第一弹)我们实现了获取 QQ 音乐指定歌手单曲排行指定页数的歌曲的歌名.专辑名.播放链接. 通过手把手教你使用Python抓取QQ音乐 ...

  5. python网页爬虫循环获取_手把手教你用 Python 搞定网页爬虫

    原标题:手把手教你用 Python 搞定网页爬虫 编译:欧剃 作为数据科学家的第一个任务,就是做网页爬取.那时候,我对使用代码从网站上获取数据这项技术完全一无所知,它偏偏又是最有逻辑性并且最容易获得的 ...

  6. python时间序列预测不连续怎么办_手把手教你用Python处理非平稳时间序列(附代码)...

    本文约3600字,建议阅读10分钟. 本文将重点介绍时间序列数据的平稳性检验方法. 简介 预测一个家庭未来三个月的用电量,估计特定时期道路上的交通流量,预测一只股票在纽约证券交易所交易的价格--这些问 ...

  7. python图片分类技术介绍_手把手教你用Python构建你的第一个多标签图像分类模型(附案例)!...

    介绍 你正在处理图像数据吗?我们可以使用计算机视觉算法来做很多事情:对象检测 图像分割 图像翻译 对象跟踪(实时),还有更多-- 这让我思考--如果一个图像中有多个对象类别,我们该怎么办?制作一个图像 ...

  8. python热搜排行功能_手把手教你用Python+Pyecharts让微博热搜榜动起来

    今天教大家如何用pyecharts制作微博热搜榜动态展示视频,先上视频看看效果: 教程主要有2部分: 一是python爬取微博热搜内容 二是用pyecharts制作动态视频 下面给大家详细介绍一下 一 ...

  9. python 函数修饰器 父类_手把手教你学python第十四讲(函数装饰器,super用法和时间处理)...

    文中有些字在图中是因为每篇文章最多100张图片,我把有的小图片和文字一起截图了,文中所有的引用都会标出原文网址,除此以外都是作者原创. 有时候会在文章最前或者最后补充一些知识或者把前面说的有问题的地方 ...

  10. python 桑基图 地理坐标_手把手教你用Python绘制酷炫的桑基图

    最近 ,不止一次收到小伙伴的截图追问: "这个图叫什么 ? ? ?" "这个图真好看 ! ! !怎么画啊 ?" ...... 笔者本没有干货 ,问的人多了 ,也 ...

最新文章

  1. C#分析数据库结构,使用XSL模板自动生成代码
  2. oracle多表查询
  3. Ubuntu使用——23(dock的美化)
  4. 06.delete_by_query操作
  5. nssl1254-A(林下风气)【树形dp】
  6. JS_17 ES5,ES6
  7. flink开发案例_为什么说 Flink + AI 值得期待?
  8. postman响应html,Postman工具——请求与响应(示例代码)
  9. python如何下载库_python中如何下载库
  10. 2018 ACM/ICPC 南京站小结
  11. Activity 横竖屏切换
  12. Struts2的OGNL表达式
  13. 05_家谱管理系统(小钱版)[数据结构][2012-02-03]
  14. 水木周平戏说中国网络黑幽默
  15. 迅雷下载边下边播设置为本地安装的浏览器
  16. CentOS 7 Shipyard启动时一直显示省略号的解决办法
  17. Centos7操作系统搭建Snipe-IT资产管理系统
  18. STM32内部参考电压的使用
  19. PDCP 功能与基本概念
  20. Jmeter中唯一值-UUID取值(截取指定长度、去除“-”)

热门文章

  1. VALSE2022内容总结
  2. win10下装黑苹果双系统_黑苹果macOS10.15.4单硬盘三系统安装教程
  3. 关闭”xx程序已停止工作”提示窗口
  4. springcloud学习-Eureka、Eureka高可用集群、Ribbon客户端负载均衡策略
  5. H5微信分享、自定义微信分享
  6. 如何在Kali Linux 2018上安装GeForce GTX 1060显卡驱动
  7. PS抠图小技巧-【色彩范围】
  8. 论文阅读-Face X-ray for More General Face Forgery Detection
  9. python给批量图片添加文字_Python之利用PIL批量给图片添加文字
  10. python接入图灵机器人_Python使用微信接入图灵机器人过程解析