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

简介:

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

环境要求

  • Ubuntu 17.10
  • Python 2.7.14

环境搭建

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

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

3、 安装 git 、cmake 、 python-pip

 
  1. # 安装 git 
  2. $ sudo apt-get install -y git 
  3. # 安装 cmake 
  4. $ sudo apt-get install -y cmake 
  5. # 安装 python-pip 
  6. $ sudo apt-get install -y python-pip  

4、 安装编译 dlib

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

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

5、 安装 face_recognition

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

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

实现人脸识别

示例一(1 行命令实现人脸识别):

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

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

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

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

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

识别成功!!!

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

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

用于识别的图片

 
  1. # 执行python文件 
  2. $ python find_faces_in_picture.py  

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

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

 
  1. # filename : find_facial_features_in_picture.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. print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) 
  15.  
  16. for face_landmarks in face_landmarks_list: 
  17.  
  18.    #打印此图像中每个面部特征的位置 
  19.     facial_features = [ 
  20.         'chin'
  21.         'left_eyebrow'
  22.         'right_eyebrow'
  23.         'nose_bridge'
  24.         'nose_tip'
  25.         'left_eye'
  26.         'right_eye'
  27.         'top_lip'
  28.         'bottom_lip' 
  29.     ] 
  30.  
  31.     for facial_feature in facial_features: 
  32.         print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature])) 
  33.  
  34.    #让我们在图像中描绘出每个人脸特征! 
  35.     pil_image = Image.fromarray(image) 
  36.     d = ImageDraw.Draw(pil_image) 
  37.  
  38.     for facial_feature in facial_features: 
  39.         d.line(face_landmarks[facial_feature], width=5) 
  40.  
  41.     pil_image.show() 

自动识别出人脸特征

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

 
  1. # filename : recognize_faces_in_pictures.py 
  2. # -*- conding: utf-8 -*- 
  3. # 导入face_recogntion模块,可用命令安装 pip install face_recognition 
  4. import face_recognition 
  5.  
  6. #将jpg文件加载到numpy数组中 
  7. babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg"
  8. Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg"
  9. unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg"
  10.  
  11. #获取每个图像文件中每个面部的面部编码 
  12. #由于每个图像中可能有多个面,所以返回一个编码列表。 
  13. #但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。 
  14. babe_face_encoding = face_recognition.face_encodings(babe_image)[0] 
  15. Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0] 
  16. unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0] 
  17.  
  18. known_faces = [ 
  19.     babe_face_encoding, 
  20.     Rong_zhu_er_face_encoding 
  21.  
  22. #结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果 
  23. results = face_recognition.compare_faces(known_faces, unknown_face_encoding) 
  24.  
  25. print("这个未知面孔是 Babe 吗? {}".format(results[0])) 
  26. print("这个未知面孔是 容祖儿 吗? {}".format(results[1])) 
  27. 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. #将jpg文件加载到numpy数组中 
  8. image = face_recognition.load_image_file("biden.jpg"
  9. #查找图像中所有面部的所有面部特征 
  10. face_landmarks_list = face_recognition.face_landmarks(image) 
  11. for face_landmarks in face_landmarks_list: 
  12.     pil_image = Image.fromarray(image) 
  13.     d = ImageDraw.Draw(pil_image, 'RGBA'
  14.     #让眉毛变成了一场噩梦 
  15.     d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128)) 
  16.     d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128)) 
  17.     d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5) 
  18.     d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5) 
  19.     #光泽的嘴唇 
  20.     d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128)) 
  21.     d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128)) 
  22.     d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8) 
  23.     d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8) 
  24.     #闪耀眼睛 
  25.     d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30)) 
  26.     d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30)) 
  27.     #涂一些眼线 
  28.     d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6) 
  29.     d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6) 
  30.     pil_image.show() 

美颜前后对比


本文作者:Kangvcar

来源:51CTO

相关文章
|
4月前
|
算法 安全 搜索推荐
深入浅出:使用Python实现人脸识别系统
在当今数字化时代,人脸识别技术已成为安全验证、个性化服务等领域的关键技术。本文将引导读者从零开始,逐步探索如何利用Python和开源库OpenCV来构建一个基础的人脸识别系统。本文不仅会详细介绍环境搭建、关键算法理解,还会提供完整的代码示例,帮助读者理解人脸识别的工作原理,并在实际项目中快速应用。通过本文,您将能够掌握人脸识别的基本概念、关键技术和实现方法,为进一步深入学习和研究打下坚实的基础。
|
11月前
|
机器学习/深度学习 监控 算法
人脸识别之三
人脸识别之三
57 1
|
11月前
|
机器学习/深度学习 存储 算法
人脸识别之二
人脸识别之二
64 1
|
机器学习/深度学习 自然语言处理 固态存储
【OpenCV图像处理15】人脸识别项目
【OpenCV图像处理15】人脸识别项目
183 0
|
API 开发工具 C#
C# 10分钟完成百度人脸识别——入门篇
C# 10分钟完成百度人脸识别——入门篇
|
机器学习/深度学习 监控 算法
门禁系统中人脸检测技术的原理剖析和使用教程
人脸检测 API 是一种基于深度学习技术的图像处理API,可以快速地检测出一张图片中的人脸,并返回人脸的位置和关键点坐标,在人脸识别系统、人脸情绪识别等多种场景下都有极大的应用。 本文将从人脸检测的发展历程、原理、特点等角度出发,一文带你看透人脸检测 API 。
218 0
|
人工智能 监控 算法
手把手带你体验Python实现人脸识别
前几天唠了一些Python的基础知识,确实无聊,但是无聊归无聊确实有用.今天来搞点有趣的事情--利用计算机视觉实现人脸检测和识别. 计算机视觉是人工智能的一个重要分支.计算机视觉中的人脸识别现在是我们生活最常出现的功能,比如刷脸打卡,这是每个打工人上班必备.人脸识别门禁,身份验证等等.看过钢铁侠的童鞋是否还记得,钢铁侠初次飞行时.他的人工智能系统头盔中显示了道路上的车辆信息.包括车辆的品牌和车中的人物,当时我们把它当成科幻来看,其实在现实中我们利用计算机视觉技术完全可以做到,而且我们目前已在运用,比如停车场系统车辆自动识别闸机.人流量检测,还有我们的无人驾驶汽车,都在使用计算机视觉技术中的人
809 1
|
存储 数据库 计算机视觉
人脸识别的案例实现 | 学习笔记
快速学习人脸识别的案例实现
314 0
人脸识别的案例实现 | 学习笔记
|
机器学习/深度学习 人工智能 JSON
教你用Python搭建人脸识别开放平台
教你用Python搭建人脸识别开放平台
433 0
教你用Python搭建人脸识别开放平台
|
JavaScript Java Go
阿里云新版人脸识别综述
针对之前对阿里云人脸识别的系列博客的编写,本文做一个系统的概述,方便使用者能够根据自己的使用需求,更快的查阅到需要的示例参考。
1865 0