点击上方“Python编程与实战”,选择“置顶公众号”

第一时间获取 Python 技术干货!

阅读文本大概需要 11分钟。

今天给大家介绍一个世界上最简洁的人脸识别库 face_recognition,你可以使用 Python 和命令行工具进行提取、识别、操作人脸。

基于业内领先的 C++ 开源库 dlib 中的深度学习模型,用 Labeled Faces in the Wild 人脸数据集进行测试,有高达99.38%的准确率。

1.安装

最好是使用 Linux 或 Mac 环境来安装,Windows 下安装会有很多问题。在安装 face_recognition 之前你需要先安装以下几个库,注意顺序!

1.1 先安装 cmake 和 boost

pip  install  cmake
pip install boost

1.2 安装 dlib

pip install dlib

此处安装可能要几分钟。如安装出错,建议使用 whl 文件来安装

1.3 安装 face_recognition

face_recongnition 一般要配合 opencv 一起使用

pip install face_recognition
pip install opencv-python

2. 人脸识别

比如这里总共有三张图片,其中有两张已知,第三张是需要识别的图片

首先获取人脸中的信息

kobe_image = face_recognition.load_image_file("kobe.jpg")  # 已知科比照片
jordan_image = face_recognition.load_image_file("jordan.jpeg")  # 已知乔丹照片
unknown_image = face_recognition.load_image_file("unkown.jpeg")  # 未知照片 kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

代码中前三行分别是加载三张图片文件并返回图像的 numpy 数组,后三行返回图像中每个面部的人脸编码

然后将未知图片中的人脸和已知图片中的人脸进行对比,使用 compare_faces() 函数, 代码如下:

known_faces = [
    kobe_face_encoding,
    jordan_face_encoding
]
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)  # 识别结果列表
print("这张未知照片是科比吗? {}".format(results[0]))
print("这张未知照片是乔丹吗? {}".format(results[1]))

运行结果如下:

不到二十行代码,就能识别出人脸是谁,是不是 so easy!

3. 人脸标注

仅仅识别图片中的人脸总是感觉差点什么,那么将识别出来的人脸进行姓名标注是不是更加有趣~

已知图片的识别和前面代码基本是一样的,未知图片代码多了人脸位置的识别,并使用了face_locations() 函数。代码如下:

face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

函数传入两个参数,返回以上,右,下,左固定顺序的脸部位置列表的作用是将已知脸部位置和未知面部编码进行比较,得到欧式距离~~~具体是什么我也不知道,距离就相当于相识度。

函数说明:face_distance(face_encodings, face_to_compare)

face_encodings:已知的面部编码

本次图片前面两张没有变化,第三张换成了科比和乔丹的合影,最终运行之后结果如下:

左边是原图,右边是识别后自动标注出来的图片。

import face_recognition
from PIL import Image, ImageDraw
import numpy as np def draws():
    kobe_image = face_recognition.load_image_file("kobe.jpg")
    kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]     jordan_image = face_recognition.load_image_file("jordan.jpeg")
    jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]     known_face_encodings = [
        kobe_face_encoding,
        jordan_face_encoding
    ]
    known_face_names = [
        "Kobe",
        "Jordan"
    ]     unknown_image = face_recognition.load_image_file("two_people.jpeg")     face_locations = face_recognition.face_locations(unknown_image)
    face_encodings = face_recognition.face_encodings(unknown_image, face_locations)     pil_image = Image.fromarray(unknown_image)
    draw = ImageDraw.Draw(pil_image)     for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)         name = "Unknown"         face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = known_face_names[best_match_index]         draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))         text_width, text_height = draw.textsize(name)
        draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
        draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))     del draw
    pil_image.show()
    pil_image.save("image_with_boxes.jpg")

4. 给人脸美妆

这个功能需要结合 PIL 一起使用。用法都差不多,首先就是将图片文件加载到 numpy 数组中,然后将人脸中的面部所有特征识别到一个列表中

image = face_recognition.load_image_file("bogute.jpeg")
face_landmarks_list = face_recognition.face_landmarks(image)

遍历列表中的元素,修改眉毛

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), wid=6)

根据以上代码做了,我用实力不行,打球又脏的 "大嘴" 博格特来做演示!

左边是原图,右边是加了美妆后的效果

你打球的样子像极了 cxk!

推荐阅读:

THANDKS

- End -

用 20 行 python 代码实现人脸识别!的更多相关文章

  1. 20行Python代码检测人脸是否佩戴口罩

    最近,口罩成为绝对热门的话题,在疫情之下,出门不戴口罩不仅对自己不负责,对他人而言也是一种潜在的威胁.所以许多小区都有保安在门口守着,谁要是不戴口罩就吼回去(吓死我了). 很多人学习python,不知 ...

  2. [转]7行Python代码的人脸识别

    https://blog.csdn.net/wireless_com/article/details/64120516 随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域 ...

  3. 25 行 Python 代码实现人脸识别——OpenCV 技术教程

    OpenCV OpenCV 是最流行的计算机视觉库,原本用 C 和 C++ 开发,现在也支持 Python. 它使用机器学习算法在图像中搜索人的面部.对于人脸这么复杂的东西,并没有一个简单的检测能对是 ...

  4. 7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

  5. 20行Python代码开发植物识别 app

    这篇文章介绍如何用Python快速实现一个植物识别的app,家里养了几盆多肉还叫不上名字,正好拿来识别一下.实现这样一个app只需要20行左右的代码,先来看下效果: 另外,我也开发了微信小程序版本,大 ...

  6. 15行python代码实现人脸识别

    方法一:face_recognition import cv2 import face_recognition img_path = "C:/Users/CJK/Desktop/1.jpg& ...

  7. 30行Python代码实现人脸检测

    参考OpenCV自带的例子,30行Python代码实现人脸检测,不得不说,Python这个语言的优势太明显了,几乎把所有复杂的细节都屏蔽了,虽然效率较差,不过在调用OpenCV的模块时,因为模块都是C ...

  8. 20行Python代码爬取王者荣耀全英雄皮肤

    引言王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. 准备工作 ...

  9. 用Python在25行以下代码实现人脸识别

    在本文中,我们将看到一种使用Python和开放源码库开始人脸识别的非常简单的方法. OpenCV OpenCV是最流行的计算机视觉库.最初是用C/C++编写的,现在它提供了Python的API. Op ...

随机推荐

  1. 第1章 MYSQL 体系结构和存储引擎

    一.定义数据库和实例 在集群的条件下,存在单个数据库对应多个实例 二.Mysql 体系结构 三.Mysql 存储引擎及各存储引擎之间的比较 命令:show engines; 四.连接 MySQL 4. ...

  2. 洛谷 P5017 摆渡车

    题目传送门 解题思路: 个人感觉DP这东西,只可意会,不可言传 AC代码: #include<iostream> #include<cstdio> #include<cs ...

  3. 嵌入式开发为什么选择C语言作为开发语言?

    了解嵌入式开发的朋友们都非常的清楚其核心的开发语言为C语言,C语言在嵌入式开发的过程中占有十分重要的地位,可以说两者之间“你中有我,我中有你”.但是有很多人会想,有那么多的开发语言为什么会单单的选择C ...

  4. C语言入门基础整理

    学习计算机技术,C语言可以说是必备的,他已经成为现在计算机行业人学习必备的,而且应用也是十分的广泛,今天就来看看拥有几年c语言工作经验的大神整理的C语言入门基础知识,没有学不会,只有不肯学. 结构化程 ...

  5. oauth2 Spring Security

    oauth2四种授权方式小结 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html 密码模式(resource owner pas ...

  6. 实现hashmap

    /**数组下面挂着链表*/ #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include< ...

  7. Clairaut 定理 证明

    (Clairaut 定理)设 $E$ 是 $\mathbf{R}^n$ 的开子集合,并设 $f:\mathbf{E}\to \mathbf{R}^{m}$ 是 $E$ 上的二次连续可微函数.那么对于一 ...

  8. Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-2

    1 MATLAB并行计算-从个人桌面到远程集群和云(陈伟/魏奋)视频摘录笔记 https://cn.mathworks.com/videos/parallel-computing-with-matla ...

  9. 迅为iTop开发板使用buildroot构建opencv文件系统

    这次我们来介绍使用buildroot构建opencv开发环境,buildroot 是 Linux平台上一个构建嵌入式Linux系统的框架.整个buildroot是由 Makefile脚本和Kconfi ...

  10. linux_nano命令

    nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano. nano命令可以打开指定 ...