百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

如何用Python实现人脸识别?一文带你详细了解,几行代码就能搞定

off999 2024-09-14 07:06 20 浏览 0 评论

前言

Python中实现人脸识别功能有多种方法,依赖于python胶水语言的特性,我们通过调用包可以快速准确的达成这一目的,本文给大家分享使用Python实现简单的人脸识别功能的操作步骤,感兴趣的朋友一起看看吧!

摘要:一行代码实现人脸识别

  • 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名。
  • 接下来,你需要准备另一个文件夹,里面是你要识别的图片。
  • 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁,一行代码足以!!!

正文:

环境要求:

  • 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-Imaging
 from 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 requests库的安装和使用指南

简介requests库是Python中一款流行的HTTP请求库,用于简化HTTP请求的发送和处理,也是我们在使用Python做接口自动化测试时,最常用的第三方库。本文将介绍如何安装和使用request...

python3.8的数据可视化pyecharts库安装和经典作图,值得收藏

1.Deepin-linux下的python3.8安装pyecharts库(V1.0版本)1.1去github官网下载:https://github.com/pyecharts/pyecharts1...

我在安装Python库的时候一直出这个错误,尝试很多方法,怎么破?

大家好,我是皮皮。一、前言前几天在Python星耀群【我喜欢站在一号公路上】问了一个Python库安装的问题,一起来看看吧。下图是他的一个报错截图:二、实现过程这里【对不起果丹皮】提示到上图报错上面说...

自动化测试学习:使用python库Paramiko实现远程服务器上传和下载

前言测试过程中经常会遇到需要将本地的文件上传到远程服务器上,或者需要将服务器上的文件拉到本地进行操作,以前安静经常会用到xftp工具。今天安静介绍一种python库Paramiko,可以帮助我们通过代...

Python 虚拟环境管理库 - poetry(python虚拟环境virtualenv)

简介Poetry是Python中的依赖管理和打包工具,它允许你声明项目所依赖的库,并为你管理它们。相比于Pipev,我觉得poetry更加清爽,显示更友好一些,虽然它的打包发布我们一般不使...

pycharm(pip)安装 python 第三方库,时下载速度太慢咋办?

由于pip默认的官方软件源服务器在国外,所以速度慢,导致下载时间长,甚至下载会频繁中断,重试次数过多时会被拒绝。解决办法1:更换国内的pip软件源即可。pip指定软件源安装命令格式:pipinsta...

【Python第三方库安装】介绍8种情况,这里最全看这里就够了!

**本图文作品主要解决CMD或pycharm终端下载安装第三方库可能出错的问题**本作品介绍了8种安装方法,这里最全的python第三方库安装教程,简单易上手,满满干货!希望大家能愉快地写代码,而不要...

python关于if语句的运用(python中如何用if语句)

感觉自己用的最笨的方式来解这道题...

Python核心技术——循环和迭代(上)

这次,我们先来看看处理查找最大的数字问题上,普通人思维和工程师思维有什么不一样。例如:lst=[3,6,10,5,7,9,12]在lst列表中寻找最大的数字,你可能一眼能看出来,最大值为...

力扣刷题技巧篇|程序员萌新如何高效刷题

很多新手初刷力扣时,可能看过很多攻略,类似于按照类型来刷数组-链表-哈希表-字符串-栈与队列-树-回溯-贪心-动态规划-图论-高级数据结构之类的。可转念一想,即...

“千万别学我!从月薪3000到3万,我靠这3个笨方法逆袭”

3年前,我还在为房租而忧心忡忡,那时月薪仅有3000元;如今,我的月收入3万!很多人都问我是如何做到的,其实关键就在于3个步骤。今天我毫无保留地分享给大家,哪怕你现在工资低、缺乏资源,照着做也能够实...

【独家攻略】Anaconda秒建PyTorch虚拟环境,告别踩坑,小白必看

目录一.Pytorch虚拟环境简介二.CUDA简介三.Conda配置Pytorch环境conda安装Pytorch环境conda下载安装pytorch包测试四.NVIDIA驱动安装五.conda指令一...

入门扫盲:9本自学Python PDF书籍,让你避免踩坑,轻松变大神!

工作后在学习Python这条路上,踩过很多坑。今天给大家推荐9本自学Python,让大家避免踩坑。入门扫盲:让你不会从一开始就从入门到放弃1《看漫画学Python:有趣、有料、好玩、好用》2《Pyth...

整蛊大法传授于你,不要说是我告诉你的

大家好,我是白云。给大家整理一些恶搞代码,谨慎使用!小心没朋友。1.电脑死机打开无数个计算器,直到死机setwsh=createobject("wscript.shell")do...

python 自学“笨办法”7-9章(笨办法学python3视频)

笨办法这本书,只强调一点,就是不断敲代码,从中增加肌肉记忆,并且理解和记住各种方法。第7章;是更多的打印,没错就是更多的打印第八章;打印,打印,这次的内容是fomat的使用与否f“{}{}”相同第九...

取消回复欢迎 发表评论: