手把手教你用Python实现人脸识别

姓名:尤学强 学号:17101223374

转载自:http://mp.weixin.qq.com/s/TAW2T0V-bXgnQgTqL55zOA

【嵌牛导读】: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()

美颜前后对比

经过6年多的发展,LSGO软件技术团队在地理信息系统、数据统计分析、计算机视觉领域积累了丰富的研发经验,也建立了人才培养的完备体系。

本团队希望能与其他科研团队进行交流合作,并共同成长进步。

本微信公众平台长期系统化提供有关机器学习、软件研发、教育及学习方法、数学建模的知识,并将以上知识转化为实践。拒绝知识碎片化、耐心打磨技能、解决实际问题是我们的宗旨和追求。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容