环境要求:

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

美颜前后对比

来源:Kangvcar(简书)

手把手教你用1行代码实现人脸识别 --Python Face_recognition的更多相关文章

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

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

  2. 用Python20行代码实现人脸识别

    OpenCV 是最流行的计算机视觉库,原本用 C 和 C++ 开发,现在也支持 Python.注意:很多人学Python过程中会遇到各种烦恼问题,没有人帮答疑.为此小编建了个Python全栈免费答疑交 ...

  3. 简单机器学习人脸识别工具face-recognition python小试,一行代码实现人脸识别

    摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片.其中每个人一张图片,图片以人的名字命名.2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片.3 ...

  4. 手把手教你调试Linux C++ 代码(一步到位包含静态库和动态库调试)

    手把手教你调试Linux C++ 代码 软件调试本身就是一项相对复杂的活动,他不仅要求调试者有着清晰的思路,而且对调试者本身的技能也有很高的要求.Windows下Visual Studio为我们做了很 ...

  5. 《zw版·Halcon-delphi系列原创教程》简单的令人发指,只有10行代码的车牌识别脚本

    <zw版·Halcon-delphi系列原创教程>简单的令人发指,只有10行代码的车牌识别脚本 简单的令人发指,只有10行代码的车牌识别脚本      人脸识别.车牌识别是opencv当中 ...

  6. 用 20 行 python 代码实现人脸识别!

    点击上方"Python编程与实战",选择"置顶公众号" 第一时间获取 Python 技术干货! 阅读文本大概需要 11分钟. 今天给大家介绍一个世界上最简洁的人 ...

  7. [转]7行Python代码的人脸识别

    https://blog.csdn.net/wireless_com/article/details/64120516 随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域 ...

  8. 25 行 Python 代码实现人脸识别——OpenCV 技术教程

    OpenCV OpenCV 是最流行的计算机视觉库,原本用 C 和 C++ 开发,现在也支持 Python. 它使用机器学习算法在图像中搜索人的面部.对于人脸这么复杂的东西,并没有一个简单的检测能对是 ...

  9. 7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

随机推荐

  1. idea 最新破解亲测有效

    选择License serverLicense server address: 填:http://active.chinapyg.com/ (不能用了) 2018-5-7更新 新增一个 http:// ...

  2. 解决ssh ltt3.bg.cn 'jps' bash: jps: command not found 问题

    >>提君博客原创  http://www.cnblogs.com/tijun/  << linux 上我用hadoop用户配置好SSH后,运行 ssh ltt3.bg.cn ' ...

  3. 重启iis命令

    iisreset

  4. 2017 Python最新面试题及答案16道题

    1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...

  5. 安装MongoDB(做成Windows服务)并加载C#驱动程序

    一 Mongodb简介: 通过查询网上的一些信息来介绍一下Mongodb的优势:MongoDB是一个面向文档的数据库,目前由10gen开发并维护,它的功能丰富,齐全,完全可以替代MySQL.在使用Mo ...

  6. ASP.NET MVC和Web API中的Angular2 - 第1部分

    下载源码 - 903.5 KB 内容 第1部分:Visual Studio 2017中的Angular2设置,基本CRUD应用程序,第三方模态弹出控件 第2部分:使用Angular2管道进行过滤/搜索 ...

  7. 找工作的程序员必懂的Linux

    一.为什么要学习Linux 首先,我想先说一下:“为什么要学习Linux”?Linux 是什么,它是一款操作系统,是一个支持多用户.多任务.支持多线程和多CPU的操作系统:32位和64位的硬件可以在L ...

  8. ubuntu系统备份与恢复-remastersys方法

    http://blog.csdn.net/yaoqi_isee/article/details/73730010 http://blog.csdn.net/handsome_sheng/article ...

  9. C语言itoa()函数和atoi()函数

    以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio.h> # include <stdlib.h> void main (void) { ...

  10. 基于OpenCv和swing的图片/视频展示Java实现

    基于OpenCv和swing实现图片/视频的展示 图片的展示 swing展示图片,多为操作BufferedImage,这里要关注的核心是将Mat转为BufferedImage. 代码如下: publi ...