简单20行python代码_用 20 行 python 代码实现人脸识别!Python实现就是这么简单!...
今天给大家介绍一个世界上最简洁的人脸识别库 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 文件来安装
下载地址:https://pypi.org/simple/dlib/
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:已知的面部编码
face_to_compare:要比较的面部编码
本次图片前面两张没有变化,第三张换成了科比和乔丹的合影,最终运行之后结果如下:
左边是原图,右边是识别后自动标注出来的图片。
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!
简单20行python代码_用 20 行 python 代码实现人脸识别!Python实现就是这么简单!...相关推荐
- 人脸识别开源库face_recognition的简单介绍
人脸识别开源库face_recognition的简单介绍 原文出处: https://blog.xugaoxiang.com/ai/face-recognition-cnn.html 软硬件环境 ub ...
- 模式识别PCA方法实现人脸识别-Python
模式识别PCA方法实现人脸识别-Python 一.学习目标 二.学习内容 三.学习时间 四.学习产出 五.正文部分 1.什么是PCA? 2.使用PCA方法进行人脸识别的步骤 1)前期尝试及准备 (1) ...
- 阿里巴巴python库_年薪20万阿里巴巴Python工程师面试题曝光
作为Python工程师,进入大公司是开启职业新起点的关键,在阿里巴巴面试Python工程师的题目和经历,希望对广大Python工程师的求职者有一个帮助. 首先我们来看下阿里巴巴对Python工程师招聘 ...
- python画人脸代码_[转]7行Python代码的人脸识别
随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...
- python实现人脸识别代码_手把手教你用1行代码实现人脸识别——Python Face_recogni...
摘要: 1行代码实现人脸识别,1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片.其中每个人一张图片,图片以人的名字命名.2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片.3 ...
- python怎么输入程序代码_学习用 Thonny 写代码: 一个面向初学者的Python IDE
学习编程很难.即使当你最终怎么正确使用你的冒号和括号,但仍然有很大的可能你的程序不会如果所想的工作. 通常,这意味着你忽略了某些东西或者误解了语言结构,你需要在代码中找到你的期望与现实存在分歧的地方. ...
- 手把手教你用1行代码实现人脸识别 -- Python Face_recognition
2019独角兽企业重金招聘Python工程师标准>>> 环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装 ...
- 学习—吴恩达《机器学习》—手敲代码_准备工作之基于Ubuntu系统的 Anaconda(python环境)搭建
题记--初听不识曲中意,再听已是曲中人. 序曲 一直以来想找个机会与时间去了解一下机器学习.与此同时,吴恩达博士的名字一直在耳边回响,却不知为何如此响彻.后来,在couresa上看到了吴恩达博士的&l ...
- python 功能 代码_挑战“不可能”的代码:你不知道的Python功能
本文转载自公众号"读芯术"(ID:AI_Discovery) Python看似简单,但实则变化万千,笔者总都能看到一些代码以近乎不可能的方式呈现.这些功能很精妙,以至于我无法想象没 ...
最新文章
- opencv学习笔记(二)
- 关于server 2008 r2中文版安装多国语言用户界面包(英文)
- js jquery操作的方面xml dom html dom css dom
- Django--视图函数
- 一次失败的项目经理招聘经验
- 信息学奥赛一本通(1211:判断元素是否存在)
- 【C语言】输入5个整数并按输入顺序逆序输出
- stm32f407wet6安装linux,elua移植到STM32F407VET6流程
- 前端JS:判断list(数组)中的json对象是否重复
- python入门经典代码-【python】编程语言入门经典100例--11
- web切图怎么做_网站制作的切图技巧 网页设计中的切图技巧介绍(图文)
- 软件工程 毕业论文 文献引用 中英文文献整合
- 2022好用的手机库存管理软件排行榜前十名 手机库存管理app
- Java项目:外卖订餐管理系统(java+SSM+JSP+jQuery+Ajax+mysql)
- 颜色是如何混合出来的
- 深度学习与自然语言处理(1)_斯坦福cs224d Lecture 1
- 解读《肖申克的救赎》
- Gazebo物理仿真环境搭建 实例
- css图片压缩不变形
- 孙溟㠭篆刻作品《叶》