[雪峰磁针石博客]人脸识别工具:face_recognition

简介: 简介 face_recognition使用世界上最简单的人脸识别工具,在Python或命令行中识别和操作人脸。 使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。 该模型在Labeled Faces in the Wild基准中的准确率为99.38%。
+关注继续查看

简介

face_recognition使用世界上最简单的人脸识别工具,在Python或命令行中识别和操作人脸。

使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。 该模型在Labeled Faces in the Wild基准中的准确率为99.38%。

另外还提供了face_recognition命令行工具!

快速入门

本节我们基于ubuntu16.04,python3,使用如下图片:

640
image.png
  • 快速入门

face_recognition


import face_recognition

image = face_recognition.load_image_file("test0.jpg")
face_locations = face_recognition.face_locations(image,model="cnn")
print(face_locations)

执行结果:


$ python3 quick.py 
[(203, 391, 447, 147)]

model选择模型,默认为hog,该模式很多图片是无法识别的,为此一般用采用更精确但是速度更慢的cnn模型。

  • 显示图片:

quick2.py


import face_recognition
from PIL import Image

image = face_recognition.load_image_file("test0.jpg")
face_locations = face_recognition.face_locations(image,model="cnn")
top, right, bottom, left = face_locations[0]
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()
pil_image.save("quick2.jpg")

执行后会在当前目录生成quick2.jpg,并在屏幕显示美女头像。

244
image.png
  • 上口红

quick3.py


import face_recognition
from PIL import Image, ImageDraw

image = face_recognition.load_image_file("test1.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
print(face_landmarks_list)

for face_landmarks in face_landmarks_list:
    
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image, 'RGBA')
    
    # Gloss the lips
    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=3)
    d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=3)    
    
    pil_image.show()
    pil_image.save("quick3.jpg")
    

上口红之前:

700
image.png

上口红之后:

700
image.png

个人总是觉得没上口红的更好看,偏偏有那么多喜欢化成妖怪的女人。

  • 框选

下面代码把脸部框选出来,注意:face_locations返回的图片和PIL使用的坐标不同,为此需要一定的转换。

quick4.py


import face_recognition
from PIL import Image, ImageDraw

image = face_recognition.load_image_file("test1.jpg")
locations = face_recognition.face_locations(image)
print(locations)
pos = locations[0]

pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
d.rectangle((pos[3], pos[0], pos[1], pos[2]))
pil_image.show()
pil_image.save("quick4.jpg")

700
image.png

本文代码地址: https://github.com/china-testing/python-api-tesing/tree/master/python3_libraries/face_recognition

其他

  • 旋转

face_recognition只能识别头在上嘴在下的图片比较好,如果你的照片是横向的,有可能要旋转才能识别。


700
image.png

sleep.py


import face_recognition
from PIL import Image, ImageDraw

image = face_recognition.load_image_file("sleep.jpg")
locations = face_recognition.face_locations(image)
print(locations)
img = Image.open("sleep.jpg")
img = img.rotate(90,expand=1)
img.save("/tmp/tmp.jpg")
image = face_recognition.load_image_file("/tmp/tmp.jpg")
locations = face_recognition.face_locations(image)
print(locations)

pil_image = Image.fromarray(image)
pil_image.show()

执行结果:


[]
[(166, 424, 255, 335)]

当然此图使用cnn模式不用旋转也是可以识别的,但是我们实验中发现一些图片,比如戴墨镜的横向图片,还是要旋转才能识别。

注意旋转方向是逆时针的。

参考资料

相关文章
|
4月前
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
1221 340
|
6月前
|
存储 前端开发 Serverless
阿里云视觉智能平台提供了人脸识别和图像搜索的API接口
阿里云视觉智能平台提供了人脸识别和图像搜索的API接口
784 0
|
7月前
|
人工智能 达摩院 计算机视觉
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(上)
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(上)
512 0
|
7月前
|
人工智能 计算机视觉
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(下)
《阿里云AI产品必知必会系列电子书》——阿里云视觉智能开放平台——人脸识别QuickStart使用教程(下)
463 0
|
7月前
|
机器人 计算机视觉 Python
智能机器人项目,安装人脸识别face_recognition报错解决
智能机器人项目,安装人脸识别face_recognition报错解决
61 0
|
8月前
|
人工智能 计算机视觉
阿里云产品体系分为6大分类——人工智能——分为10种模块——人脸识别
阿里云产品体系分为6大分类——人工智能——分为10种模块——人脸识别自制脑图
70 1
|
人工智能 文字识别 开发工具
阿里云视觉智能开放平台离线人脸识别SDK开启邀测啦
阿里云视觉智能开放平台离线人脸识别SDK开启邀测,端侧SDK是一套完整的包含高性能检测、鲁棒性识别、多规格活体以及高速比对等功能的人脸识别全链路SDK。应用了多项顶会论文以及核心专利技术,在受限端侧计算资源下具备速度快、精度高等特点,典型应用场景有门禁、考勤等,注册好后可在无网条件下使用。端侧SDK支持Android、IOS终端系统。
404 0
阿里云视觉智能开放平台离线人脸识别SDK开启邀测啦
|
机器学习/深度学习 人工智能 计算机视觉
Python实现人脸识别功能,face_recognition的使用 | 机器学习
Python实现人脸识别功能,face_recognition的使用 | 机器学习
Python实现人脸识别功能,face_recognition的使用 | 机器学习
|
机器学习/深度学习 人工智能 Linux
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
|
API 开发工具 对象存储
阿里云视觉智能开放平台--人脸识别使用教程(使用本地图片)
前面在博客:阿里云视觉智能开放平台--人脸识别使用教程 介绍了如何在智能视觉开放平台使用人脸识别的接口,示例主要演示了1:N人脸查找的使用流程,使用的是OSS的图片,发现很多同学对本地图片的使用疑问较多,这里以人脸属性识别API为例演示如何使用本地图片。
2430 0
相关产品
机器翻译
推荐文章
更多