Python3 利用face_recognition实现人脸识别的方法


Posted in Python onMarch 13, 2020

前言

之前实践了下face++在线人脸识别版本,这回做一下离线版本。github 上面有关于face_recognition的相关资料,本人只是做个搬运工,对其中的一些内容进行搬运,对其中一些例子进行实现。

官方描述:

face_recognition是一个强大、简单、易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,特别是兼容树莓派系统。本项目是世界上最简洁的人脸识别库,你可以使用Python和命令行工具提取、识别、操作人脸。本项目的人脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。

(关于兼容树莓派,以后有板子了再做一下)

下面两个链接划重点

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md
https://face-recognition.readthedocs.io/en/latest/face_recognition.html

环境配置

  • ubuntu16.04(其他环境的安装可以参考第一个链接,官方有说明)
  • pycharm(可忽略,怎么舒服怎么来)
  • python3
  • opencv(我的是4.1.2,三点几的版本应该也一样)

实际上只需要安装face_recognition,当然,没有opencv的也需要安装一下opencv

pip3 install face_recognition

图片准备

由于需要做一些图片的比对,因此需要准备一些图片,本文图片取自以下链接

https://www.zhihu.com/question/314169580/answer/872770507

接下来开始操作

官方还有提供命令行的操作(这个没去做),本文不做这个,我们只要是要在python中用face_recognition,因此定位到这一块。

Python3 利用face_recognition实现人脸识别的方法

这个api文档地址就是上面的第二个链接。进去之后可以看到:

Python3 利用face_recognition实现人脸识别的方法

part1.识别图片中的人是谁

代码

# part1
# 识别图片中的人是谁
import face_recognition
known_image = face_recognition.load_image_file("lyf1.jpg")
unknown_image = face_recognition.load_image_file("lyf2.jpg")

lyf_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([lyf_encoding], unknown_encoding)	
# A list of True/False values indicating which known_face_encodings match the face encoding to check

print(type(results))
print(results)

if results[0] == True:
  print("yes")
else:
  print("no")

结果

<class 'list'>
[True]
yes

part2.从图片中找到人脸

代码

# part2
# 从图片中找到人脸(定位人脸位置)

import face_recognition
import cv2

image = face_recognition.load_image_file("lyf1.jpg")

face_locations_useCNN = face_recognition.face_locations(image,model='cnn')
# model ? Which face detection model to use. “hog” is less accurate but faster on CPUs.
# “cnn” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is “hog”.

face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因为返回值的顺序是这样子的,因此在后面的for循环里面赋值要注意按这个顺序来

print("face_location_useCNN:")
print(face_locations_useCNN)
face_num1=len(face_locations_useCNN)
print(face_num1)    # The number of faces


print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)    # The number of faces
# 到这里为止,可以观察两种情况的坐标和人脸数,一般来说,坐标会不一样,但是检测出来的人脸数应该是一样的
# 也就是说face_num1 = face_num2; face_locations_useCNN 和 face_locations_noCNN 不一样


org = cv2.imread("lyf1.jpg")
img = cv2.imread("lyf1.jpg")
cv2.imshow("lyf1.jpg",img) # 原始图片

# Go to get the data and draw the rectangle
# use CNN
for i in range(0,face_num1):
  top = face_locations_useCNN[i][0]
  right = face_locations_useCNN[i][1]
  bottom = face_locations_useCNN[i][2]
  left = face_locations_useCNN[i][3]

  start = (left, top)
  end = (right, bottom)

  color = (0,255,255)
  thickness = 2
  cv2.rectangle(img, start, end, color, thickness)  # opencv 里面画矩形的函数

# Show the result
cv2.imshow("useCNN",img)


# for face_location in face_locations_noCNN:
#
#   # Print the location of each face in this image
#   top, right, bottom, left = face_location
# # 等价于下面的这种写法

for i in range(0,face_num2):
  top = face_locations_noCNN[i][0]
  right = face_locations_noCNN[i][1]
  bottom = face_locations_noCNN[i][2]
  left = face_locations_noCNN[i][3]

  start = (left, top)
  end = (right, bottom)

  color = (0,255,255)
  thickness = 2
  cv2.rectangle(org, start, end, color, thickness)

cv2.imshow("no cnn ",org)

cv2.waitKey(0)
cv2.destroyAllWindows()

结果

face_location_useCNN:
[(223, 470, 427, 266)]
1
face_location_noCNN:
[(242, 489, 464, 266)]
1

图片效果大致是这样

Python3 利用face_recognition实现人脸识别的方法

part3.找到人脸并将其裁剪打印出来(使用cnn定位人脸)

代码

# part3
# 找到人脸并将其裁剪打印出来(使用cnn定位人脸)

from PIL import Image
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")

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()

结果

I found 1 face(s) in this photograph.
A face is located at pixel location Top: 205, Left: 276, Bottom: 440, Right: 512

图片效果大致是这样

Python3 利用face_recognition实现人脸识别的方法

part4.识别单张图片中人脸的关键点

代码

# part4 识别单张图片中人脸的关键点

from PIL import Image, ImageDraw
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)

print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

# Create a PIL imagedraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)

for face_landmarks in face_landmarks_list:

  # Print the location of each facial feature in this image
  for facial_feature in face_landmarks.keys():
    print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

  # Let's trace out each facial feature in the image with a line!
  for facial_feature in face_landmarks.keys():
    d.line(face_landmarks[facial_feature], width=5)

# Show the picture
pil_image.show()

结果

I found 1 face(s) in this photograph.
The left_eyebrow in this face has the following points: [(305, 285), (321, 276), (340, 277), (360, 281), (377, 288)]
The right_eye in this face has the following points: [(422, 313), (432, 303), (446, 302), (459, 305), (449, 312), (435, 314)]
The nose_bridge in this face has the following points: [(394, 309), (394, 331), (395, 354), (396, 375)]
The right_eyebrow in this face has the following points: [(407, 287), (424, 278), (442, 273), (461, 272), (478, 279)]
The bottom_lip in this face has the following points: [(429, 409), (419, 421), (408, 428), (398, 430), (389, 429), (377, 424), (364, 412), (370, 413), (389, 414), (398, 415), (407, 413), (423, 411)]
The chin in this face has the following points: [(289, 295), (291, 323), (296, 351), (303, 378), (315, 403), (332, 428), (353, 448), (376, 464), (400, 467), (422, 461), (441, 444), (459, 425), (473, 403), (484, 377), (490, 351), (493, 323), (493, 296)]
The top_lip in this face has the following points: [(364, 412), (377, 407), (389, 403), (397, 406), (406, 402), (417, 405), (429, 409), (423, 411), (406, 412), (397, 414), (389, 413), (370, 413)]
The left_eye in this face has the following points: [(327, 308), (339, 304), (353, 306), (364, 314), (352, 317), (338, 316)]
The nose_tip in this face has the following points: [(375, 383), (386, 387), (396, 390), (407, 385), (416, 381)]

图片效果

Python3 利用face_recognition实现人脸识别的方法

到此这篇关于Python3 利用face_recognition实现人脸识别的方法的文章就介绍到这了,更多相关Python3 人脸识别内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python内置的字符串处理函数整理
Jan 29 Python
通过Python爬虫代理IP快速增加博客阅读量
Dec 14 Python
利用python程序生成word和PDF文档的方法
Feb 14 Python
python 性能优化方法小结
Mar 31 Python
Python虚拟环境virtualenv的安装与使用详解
May 28 Python
python购物车程序简单代码
Apr 18 Python
Django使用paginator插件实现翻页功能的实例
Oct 24 Python
python引用(import)某个模块提示没找到对应模块的解决方法
Jan 19 Python
python3去掉string中的标点符号方法
Jan 22 Python
Python 3 使用Pillow生成漂亮的分形树图片
Dec 24 Python
Python定时任务框架APScheduler原理及常用代码
Oct 05 Python
Python机器学习应用之基于线性判别模型的分类篇详解
Jan 18 Python
在django中使用post方法时,需要增加csrftoken的例子
Mar 13 #Python
python 安装教程之Pycharm安装及配置字体主题,换行,自动更新
Mar 13 #Python
详解用Python进行时间序列预测的7种方法
Mar 13 #Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
Mar 13 #Python
在django项目中导出数据到excel文件并实现下载的功能
Mar 13 #Python
Django choices下拉列表绑定实例
Mar 13 #Python
django model object序列化实例
Mar 13 #Python
You might like
一个php作的文本留言本的例子(六)
2006/10/09 PHP
php array_intersect()函数使用代码
2009/01/14 PHP
php根据分类合并数组的方法实例详解
2013/11/06 PHP
php单元测试phpunit入门实例教程
2017/11/17 PHP
PHP实现的超长文本分页显示功能示例
2018/06/04 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
Zero Clipboard js+swf实现的复制功能使用方法
2010/03/07 Javascript
用Juery网页选项卡实现代码
2011/06/13 Javascript
扩展Jquery插件处理mouseover时内部有子元素时发生样式闪烁
2011/12/08 Javascript
chrome浏览器不支持onmouseleave事件的解决技巧
2013/05/31 Javascript
深入理解JSON数据源格式
2014/01/10 Javascript
一行命令搞定node.js 版本升级
2014/07/20 Javascript
仿JQuery输写高效JSLite代码的一些技巧
2015/01/13 Javascript
jquery手风琴特效插件
2015/02/04 Javascript
JS实现新浪微博效果带遮罩层的弹出框代码
2015/10/12 Javascript
AngularJs Understanding the Model Component
2016/09/02 Javascript
JS触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
2016/10/30 Javascript
详解Vue微信公众号开发踩坑全记录
2017/08/21 Javascript
javascript实现超好看的3D烟花特效
2020/01/01 Javascript
python pdb调试方法分享
2014/01/21 Python
Python中的深拷贝和浅拷贝详解
2015/06/03 Python
python实现排序算法解析
2018/09/08 Python
python set内置函数的具体使用
2019/07/02 Python
python中for循环把字符串或者字典添加到列表的方法
2019/07/20 Python
Python语法之精妙的十个知识点(装B语法)
2020/01/18 Python
详解python tcp编程
2020/08/24 Python
一款利用纯css3实现的360度翻转按钮的实例教程
2014/11/05 HTML / CSS
免费获得微软MCSD证书赶快行动吧!
2012/11/13 HTML / CSS
应届生求职推荐信
2013/10/28 职场文书
公司领导九九重阳节发言稿2014
2014/09/25 职场文书
2014年单位工作总结范文
2014/11/27 职场文书
劳动纠纷调解协议书格式
2014/11/30 职场文书
《神奇的鸟岛》教学反思
2016/02/22 职场文书
Html5新增了哪些功能
2021/04/16 HTML / CSS
Dubbo+zookeeper搭配分布式服务的过程详解
2022/04/03 Java/Android
使用Python解决图表与画布的间距问题
2022/04/11 Python