导读:近日,一个名为 face_recognition 的人脸识别项目登上了 GitHub Trending 趋势榜,赚足了眼球。自开源至截稿,此项目在 Github 上的 Star 数已达 26500,Fork 数也达到了 7117。本文主要介绍了该项目的使用说明和使用方法,便于国内的开发者们进行研究学习。
face_recognition 宣称是史上最强大,最简单的人脸识别项目。据悉,该项目由软件工程开发师和咨询师 Adam Geitgey 开发,其强大之处在于不仅基于业内领先的 C++ 开源库 dlib 中的深度学习模型,采用的人脸数据集也是由美国麻省大学安姆斯特分校制作的 Labeled Faces in the Wild,它含有从网络收集的 13,000 多张面部图像,准确率高达 99.38%。此外,项目还配备了完整的开发文档和应用案例,特别是兼容树莓派系统。简单之处在于操作者可以直接使用 Python和命令行工具提取、识别、操作人脸。
目前,该项目的说明已有中文翻译版,本文已获得译者授权(GitHub ID:TommyZihao),引用译文对本项目进行简单介绍。
照例先奉上 GitHub 项目链接:
https://github.com/ageitgey/face_recognitio
特性
import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_locations = face_recognition.face_locations(image)
import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
本人脸识别项目除了在某些关键领域有重要作用外,还有一个可能会引起 “玩心” 的 digital make-up 自动化妆功能(类似美图秀秀)。
import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")
biden_encoding = face_recognition.face_encodings(known_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
4.配合其它的Python库(比如opencv),该项目还可实现实时人脸检测:
详细案例见:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
pip3 install face_recognition
https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b(需要电脑中安装VMWare Player 或者 VirtualBox)
export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
https://github.com/ageitgey/face_recognition/issues/175#issue-257710508
https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/
/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
$ face_detection ./folder_with_pictures/
examples/image1.jpg,65,215,169,112
examples/image2.jpg,62,394,211,244
examples/image2.jpg,95,941,244,792
$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/
/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person
人脸匹配的具体数值可以通过传入参数 --show-distance true 来查看
$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/
/unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
对识别速度不满意怎么办?
如果你的CPU是多核的,你可以通过并行运算加速人脸识别。例如,如果你的CPU有四个核心,那么你可以通过并行运算提升大概四倍的运算速度。
$ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2
Barack Obama
unknown_person
https://face-recognition.readthedocs.io如何定位人脸位置或者识别人脸身份?
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image)
# face_locations is now an array listing the co-ordinates of each face
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
import face_recognition
picture_of_me = face_recognition.load_image_file("me.jpg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
unknown_picture = face_recognition.load_image_file("unknown.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
# Now we can see the two face encodings are of the same person with `compare_faces`!
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
if results[0] == True:
print("It's a picture of me!")
else:
print("It's not a picture of
参考案例:
https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image, model="cnn")
# face_locations is now an array listing the co-ordinates of each face
参考案例:
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_batches.py如何识别单张图片中人脸的关键点?
import face_recognition
image = face_recognition.load_image_file("my_picture.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
# face_landmarks_list is now an array with the locations of each facial feature in each face.
# face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye
参考案例:
https://github.com/ageitgey/face_recognition/blob/master/examples/find_facial_features_in_picture.py识别奥巴马和拜登的人脸关键点
更多案例:
https://github.com/ageitgey/face_recognition/tree/master/examples
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py案例:使用卷积神经网络深度学习模型定位拜登的脸
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py案例:使用卷积神经网络深度学习模型批量识别图片中的人脸
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_batches.py案例:把来自网络摄像头视频里的人脸高斯模糊(需要安装OpenCV)
https://github.com/ageitgey/face_recognition/blob/master/examples/blur_faces_on_webcam.py
https://github.com/ageitgey/face_recognition/blob/master/examples/find_facial_features_in_picture.py案例:给美国副总统拜登涂美妆
https://github.com/ageitgey/face_recognition/blob/master/examples/digital_makeup.py
https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py案例:人脸识别之后在原图上画框框并标注姓名
https://github.com/ageitgey/face_recognition/blob/master/examples/identify_and_draw_boxes_on_faces.py案例:在不同精度上比较两个人脸是否属于一个人
https://github.com/ageitgey/face_recognition/blob/master/examples/face_distance.py案例:从摄像头获取视频进行人脸识别-较慢版(需要安装OpenCV)
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam.py
https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78
◆
精彩推荐
◆
社群福利
扫码添加小助手,回复:大会,加入2019 AI开发者大会福利群,每周一、三、五 更新学习资源、技术福利,还有抽奖活动~
推荐阅读
你点的每个“在看”,我都认真当成了喜欢