手把手教你用 1 行命令实现人脸识别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手把手教你用 1 行命令实现人脸识别相关的知识,希望对你有一定的参考价值。

手把手教你用 1 行命令实现人脸识别

转载 2017年11月01日 00:00:00
 

技术分享


技术分享人脸识别很难吗?-- Kangvcar


本文导航? 环境要求00%? 环境搭建03%? 实现人脸识别19%? 示例一(1 行命令实现人脸识别):19%? 示例二(识别图片中的所有人脸并显示出来):31%? 示例三(自动识别人脸特征):49%? 示例四(识别人脸鉴定是哪个人):65%? 示例五(识别人脸特征并美颜):81%转载自 | http://www.jianshu.com/p/281aa6a3823a
 作者 | 简书 / Kangvcar

环境要求

Ubuntu17.10[1]? Python 2.7.14[2]

环境搭建

1、 安装 Ubuntu17.10[1] > 安装步骤在这里[3]。

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_recognitionimport 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.pyface_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-Imagingfrom PIL import Image, ImageDraw# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport 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_recognitionimport 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))

技术分享

显示结果如图

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

  1. # filename : digital_makeup.py

  2. # -*- coding: utf-8 -*-

  3. # 导入pil模块 ,可用命令安装 apt-get install python-Imaging

  4. from PIL import Image, ImageDraw

  5. # 导入face_recogntion模块,可用命令安装 pip install face_recognition

  6. import face_recognition

  7.  

  8. #将jpg文件加载到numpy数组中

  9. image = face_recognition.load_image_file("biden.jpg")

  10.  

  11. #查找图像中所有面部的所有面部特征

  12. face_landmarks_list = face_recognition.face_landmarks(image)

  13.  

  14. for face_landmarks in face_landmarks_list:

  15.    pil_image = Image.fromarray(image)

  16.    d = ImageDraw.Draw(pil_image, ‘RGBA‘)

  17.  

  18.    #让眉毛变成了一场噩梦

  19.    d.polygon(face_landmarks[‘left_eyebrow‘], fill=(68, 54, 39, 128))

  20.    d.polygon(face_landmarks[‘right_eyebrow‘], fill=(68, 54, 39, 128))

  21.    d.line(face_landmarks[‘left_eyebrow‘], fill=(68, 54, 39, 150), width=5)

  22.    d.line(face_landmarks[‘right_eyebrow‘], fill=(68, 54, 39, 150), width=5)

  23.  

  24.    #光泽的嘴唇

  25.    d.polygon(face_landmarks[‘top_lip‘], fill=(150, 0, 0, 128))

  26.    d.polygon(face_landmarks[‘bottom_lip‘], fill=(150, 0, 0, 128))

  27.    d.line(face_landmarks[‘top_lip‘], fill=(150, 0, 0, 64), width=8)

  28.    d.line(face_landmarks[‘bottom_lip‘], fill=(150, 0, 0, 64), width=8)

  29.  

  30.    #闪耀眼睛

  31.    d.polygon(face_landmarks[‘left_eye‘], fill=(255, 255, 255, 30))

  32.    d.polygon(face_landmarks[‘right_eye‘], fill=(255, 255, 255, 30))

  33.  

  34.    #涂一些眼线

  35.    d.line(face_landmarks[‘left_eye‘] + [face_landmarks[‘left_eye‘][0]], fill=(0, 0, 0, 110), width=6)

  36.    d.line(face_landmarks[‘right_eye‘] + [face_landmarks[‘right_eye‘][0]], fill=(0, 0, 0, 110), width=6)

  37.  

  38.    pil_image.show()

技术分享

美颜前后对比






以上是关于手把手教你用 1 行命令实现人脸识别的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你实现人脸识别,有手就行

手把手教你用 CNN 识别验证码(干货!建议收藏) | 第12天

手把手教你如何用 OpenCV + Python 实现人脸识别

手把手教你运用深度学习构建视频人脸识别模型(Python实现)

手把手教你opencv做人脸识别(附源码+文档)

手把手教你用PaddleOCR与PyQT实现多语言文字识别的程序