您现在的位置是:首页 > 文章详情

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

日期:2018-08-15点击:345

简介

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模式不用旋转也是可以识别的,但是我们实验中发现一些图片,比如戴墨镜的横向图片,还是要旋转才能识别。

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

参考资料

原文链接:https://yq.aliyun.com/articles/625859
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章