简介
face_recognition使用世界上最简单的人脸识别库,在Python或命令行中识别和操作人脸。
使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。 该模型在Labeled Faces in the Wild基准中的准确率为99.38%。
另外还提供了face_recognition命令行工具!
快速入门
本节我们基于ubuntu16.04,python3,使用如下图片:

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,并在屏幕显示美女头像。

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")
上口红之前:

上口红之后:

个人总是觉得没上口红的更好看,偏偏有那么多喜欢化成妖怪的女人。
下面代码把脸部框选出来,注意: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")

本文代码地址: https://github.com/xurongzhong/mobile_data/tree/master/python3_libraries/face_recognition
本文最新版本地址: http://sina.lt/fk5j
交流QQ群:python 测试开发 144081101
wechat: pythontesting
淘宝天猫可以把链接发给qq850766020,为你生成优惠券,降低你的购物成本!
快来领取支付宝跨年红包!1月1日起还有机会额外获得专享红包哦!复制此消息,打开最新版支付宝就能领取!2C56CV70sA
其他
face_recognition只能识别头在上嘴在下的图片比较好,如果你的照片是横向的,有可能要旋转才能识别。

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://github.com/ageitgey/face_recognition