Python 几行代码即可实现人脸识别

 更新时间:2022年02月14日 16:53:01   作者:迢迢x  
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 几行代码即可实现人脸识别 的文章就介绍到这了,更多相关Python 人脸识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python图形验证码识别教程详解

    Python图形验证码识别教程详解

    这篇文章主要介绍了Python图形验证码识别,目前,许多网站采取各种各样的措施来反爬虫,其中一个措施便是使用验证码。随着技术的发展,验证码的花样越来越多。验证码最初是几个数字组合的简单的图形验证码,后来加入了英文字母和混淆曲线
    2023-02-02
  • python中time tzset()函数实例用法

    python中time tzset()函数实例用法

    在本篇文章里小编给大家整理的是一篇关于python中time tzset()函数实例用法内容,有兴趣的朋友们可以学习下。
    2021-02-02
  • Python requests及aiohttp速度对比代码实例

    Python requests及aiohttp速度对比代码实例

    这篇文章主要介绍了Python requests及aiohttp速度对比代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Python实现乱序文件重新命名编号

    Python实现乱序文件重新命名编号

    这篇文章主要为大家详细介绍一下Python的一个神操作,那就是实现乱序文件重新命名编号功能,文中的示例代码讲解详细,感兴趣的可以尝试一下
    2022-08-08
  • pytorch .detach() .detach_() 和 .data用于切断反向传播的实现

    pytorch .detach() .detach_() 和 .data用于切断反向传播的实现

    这篇文章主要介绍了pytorch .detach() .detach_() 和 .data用于切断反向传播的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Python详解如何动态给对象增加属性和方法

    Python详解如何动态给对象增加属性和方法

    python是动态语⾔,动态编程语⾔是⾼级程序设计语⾔的⼀个类别,在计算机科学领域已被⼴泛应⽤。它是⼀类在 运⾏时可以改变其结构 的语⾔ :例如新的函数、对象、甚⾄代码可以被引进,已有的函数可以被删除或是其他结构上的变化
    2022-07-07
  • 使用Python脚本来控制Windows Azure的简单教程

    使用Python脚本来控制Windows Azure的简单教程

    这篇文章主要介绍了使用Python脚本来控制Windows Azure的简单教程,由于微软官方提供了Python SDK,使得用户自己用Python控制Azure成为了可能,需要的朋友可以参考下
    2015-04-04
  • python代码xml转txt实例

    python代码xml转txt实例

    这篇文章主要介绍了python代码xml转txt实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 一个计算身份证号码校验位的Python小程序

    一个计算身份证号码校验位的Python小程序

    闲着无事,便想写个实用点的Python小程序,如何计算机身份证号码的校验位,这样的文章在网上一抓一大把,这里仅简单介绍下吧
    2014-08-08
  • Python colorama 彩色打印实现代码

    Python colorama 彩色打印实现代码

    这篇文章主要介绍了Python colorama 彩色打印实现代码,将介绍的类为Back, 它实现了与 Fore 类相同的九个关键字:BLACK、RED、GREEN、YELLOW、BLUE、MAGENTA、CYAN、WHITE、RESET,感兴趣的朋友一起看看吧
    2022-04-04

最新评论