当前位置: 首页 > 科技观察

简单而强大的人脸识别项目上GitHubTrending

时间:2023-03-21 23:16:10 科技观察

近日,一个名为face_recognition的人脸识别项目登上了GitHubTrending热搜榜,获得大量关注。从开源到截止,该项目Github上Star数达到26500,Fork数达到7117。本文主要介绍该项目的使用说明和使用方法,方便国内开发者进行研究学习。face_recognition号称是史上最强大最简单的人脸识别项目。据悉,该项目由软件工程开发人员兼顾问AdamGeitgey开发。其强大之处不仅在于业界领先的C++开源库dlib中的深度学习模型,还有马萨诸塞大学Ammonium所使用的人脸数据集。LabeledFacesintheWild,由斯图加特大学制作,包含从网络上收集的13,000多张面部图像,准确率为99.38%。此外,该项目还配备了完整的开发文档和应用案例,尤其是对树莓派系统的兼容。简单的是操作者可以直接使用Python和命令行工具来提取、识别和操作人脸。目前,该项目的说明已翻译成中文。本文已获得译者(GitHubID:TommyZihao)授权,译文用于简单介绍项目。和往常一样,这里是GitHub项目链接:https://github.com/ageitgey/face_recognitio特点1.查找并定位图片中的所有人脸:importface_recognitionimage=face_recognition.load_image_file("your_file.jpg")face_locations=face_recognition.face_locations(image)2.识别人脸的关键点(包括眼睛、鼻子、嘴巴和下巴)人脸识别项目除了重要的功能外,还有一个可能会引起“玩味”的数字化妆自动化妆功能(类似美图秀秀)。3、识别图片中的人是谁importface_recognitionknown_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_[biden_encoding],unknown_encoding)4.配合其他Python库(如opencv),本项目也可以实现实时人脸检测:详细案例见:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py安装环境配置:Python3.3+orPython2.7macOSorLinuxWindows(官方不支持,不过有高手想出了如何在Windows上运行)不同操作系统的安装方法:1.在Mac或Linux安装本项目:方法一:先安装dlib及相关Python依赖:https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf然后通过Python2的pip3或pip2用th安装以下代码。如果pip3installface_recognition安装不成功,可以用Ubuntu虚拟机安装,看这个教程:https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b(电脑需要安装VMWarePlayer或VirtualBox)方法二:修改你的pip镜像源为清华镜像,然后使用pipinstallface_recognition,可以自动为你安装各种依赖,包括dlib.只是在安装dlib的时候可能会出现问题,因为dlib需要编译,而问题一般是gcc或者g++版本的问题,所以在pipinstallface_recognition之前,可以在命令行敲一下。exportCC=/usr/local/bin/gccexportCXX=/usr/local/bin/g++指定你的gcc和g++对应的位置,(这两句会临时修改环境变量/usr/local/bin/gcc的当前终端对应你自己的gcc或g++所在目录)。2.在树莓派上安装:树莓派安装指南:https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df653。InstallonWindows:虽然这个项目没有正式支持Windows,但是有高手想出了HowtorunthisprojectonWindows:Howtoinstalldliblibraryandface_recognitionprojectonWin10systemby@masoudr教程:https://github.com/ageitgey/face_recognition/issues/175#issue-257710508UseUbuntuvirtualmachine镜像文件用于安装配置虚拟机,本镜像中已经包含该项目。https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b如何使用1.使用命令行工具项目完成后安装后,有两个命令行工具可用:face_recognition-识别单个图像或图像文件夹中的人脸。face_detection-在单个图像或图像文件夹中定位面部。face_recognition命令行工具:face_recognition命令行工具可以识别单张图片或图片文件夹中的人脸。首先你需要一个你已经知道名字的人脸图片文件夹,一个人一张,图片的文件名就是对应人的名字,然后你需要第二个图片文件夹,里面有你要的图片识别图片:然后,在命令行切换到这两个文件夹的路径,使用face_recognition命令行,传入这两个图片文件夹,然后输出未知图片中的人名:$face_recognition./pictures_of_people_i_know/./unknown_pictures//unknown_pictures/unknown.jpg,BarackObama/face_recognition_test/unknown_pictures/unknown.jpg,每行unknown_person输出结果对应图片中的一张人脸,图片名称和对应的人脸识别结果用逗号隔开。如果结果为unknown_person,则表示这张脸不对应已知人脸图片文件夹中的任何人。face_detection命令行工具face_detection命令行工具可以定位单张图片或图片文件夹中的人脸位置(输出像素坐标)。命令行使用face_detection传入图片文件夹或单个图片文件进行人脸位置检测:$face_detection./folder_with_pictures/examples/image1.jpg,65,215,169,112examples/image2.jpg,62,394,211,244examples/image2.jpg,95,941,244,792各输出结果的行对应图片中的人脸,输出坐标表示人脸的上下左右像素坐标。调整人脸识别的容错率和灵敏度。如果一张脸被识别出不止一个结果,就说明他长得太像其他人了(另外,本项目对儿童和亚洲人的人脸识别准确率有待提升)。您可以降低容错率,使识别结果更加严格。这个功能可以通过传入参数--tolerance来实现。默认容错率为0.6。容错率越低,识别越严格准确。$face_recognition--tolerance0.54./pictures_of_people_i_know/./unknown_pictures//unknown_pictures/unknown.jpg,BarackObama/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person人脸匹配的具体值可以在参数--show-distance中传入true查看:$face_recognition--show-distancetrue./pictures_of_people_i_know/./unknown_pictures//unknown_pictures/unknown.jpg,BarackObama,0.378542298956785/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person不满意怎么办识别速度?如果你的CPU是多核的,你可以通过并行计算来加速人脸识别。例如,如果您的CPU有四个内核,您可以通过并行化它们大致将操作速度提高四倍。如果你使用Python3.4或更新版本,你可以传入--cpus参数:$face_recognition--cpus4./pictures_of_people_i_know/./unknown_pictures/(你可以传入--cpus-1参数来调用cpu都是核心。)另外,子豪的弟弟Tommy说树莓派3B有4个CPU核心,传入多核参数可以显着提高图像识别的速度。更多案例如果你不关心图片的文件名,只想知道文件夹里的图片里有谁,可以使用这个管道命令:$face_recognition./pictures_of_people_i_know/./unknown_pictures/|cut-d','-f2BarackObamaunknown_person2。使用Python在Python中导入face_recognition模块,调用丰富的API接口,用几行代码轻松玩转各种人脸识别功能!API接口文档:https://face-recognition.readthedocs.io如何定位人脸位置或识别人脸身份?在Python中,可以通过如下代码定位人脸在图片中的位置:github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py识别图片中的人脸身份:importface_recognitionpicture_of_me=face_recognition.load_image_file("me.jpg")my_face_encoding=face_recognition.face_unicodings(picture_of_me)[0]#verdingmycontainface'encoding'我的面部特征可以与任何其他人脸图片进行比较!unknown_picture=face_recognition.load_image_file("unknown.jpg")unkknown_face_encoding=face_recognition.face_encodings(unknown_picture)[0]#Nowwecanseethetwofaceencodingsareofthesamepersonwith`compare_faces`!results=face_recognition.compare_faces([my_face_encoding],unknown_face_encoding)ifresults[0]==True:print("是我"不是图片参考案例:https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py对人脸识别有精准要求怎么办,可以使用深度学习模型来实现更准确的人脸定位,但要注意:这种方式需要GPU加速(由NVIDIA显卡的CUDA库驱动),编译安装dlib时也需要开启CUDA支持importface_recognitionimage=face_recognition.load_image_file("my_picture.jpg")face_locations=face_recognition.face_locations(image,model="cnn")#face_locationsnowanarraylistingtheco-ordinateso每个人脸参考案例:https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py如果有大量图片需要识别,同时有GPU,那么可以参考这个例子:https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_batches.py如何识别单张图片中人脸的关键点?importface_recognitionimage=face_recognition.load_image_file("my_picture.jpg")face_landmarks_list=face_recognition.face_landmarks(image)#face_landmarks_list现在是一个数组,其中包含每个面部特征的位置。#face_landmarks_list[0]['left_eye']wouldbethelocationandoutlineofthefirstperson'slefteye参考考例http://github/com/agesit示例:agesit/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案例:从摄像头获取视频进行人脸识别-更快版(需要安装OpenCV)https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py案例:从视频文件中识别人脸并将识别结果输出为新的视频文件(需要安装OpenCV)https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_video_file.py案例:通过树莓派统计人脸数量camera统计和人脸识别https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_on_raspberry_pi.py案例:通过HTTP浏览器访问web服务器进行人脸识别(需安装Flask后端开发框架)https://github.com/ageitgey/face_recognition/blob/master/examples/web_service_example.py案例:基于KNN分类算法的人脸识别https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py有关的文章和教程face_recognition这个项目ModernFaceRecognitionwithDeepLearning的作者写的一篇文章:https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78主要内容:基本算法和原理AdrianRosebrock使用OpenCV、Python和深度学习进行人脸识别:https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/主要内容:如何实际使用这个项目RaspberryPiFaceRecognitionbyAdrianRosebrockhttps://www.pyimagesearch.com/2018/06/25/raspberry-pi-face-recognition/主要内容:如何使用这个项目FaceclusteringwithPythonbyAdrianRosebrockontheRaspberryPihttps://www.pyimagesearch.com/2018/07/09/face-clustering-with-python/主要内容:利用无监督学习算法实现图片中高斯模糊人脸识别原理。如果想了解更多人脸识别原理,请阅读这篇文章:https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78warning本项目的人脸识别模型是基于成人的,对儿童效果可能一般如果图片中有儿童,建议将阈值设置为0.6。不同种族的识别结果可能不同,详见维基百科页面。在云服务器(Heroku、AWS等)上部署此项目本项目基于C++库dlib,因此将此项目部署在Heroku或AWS云服务器上是明智的。为了简化这个过程,有一个Dockerfile案例教大家如何将face_recognition开发的app打包成Docker容器文件,部署到所有支持Docker镜像文件的云服务上。发生了什么?如果出现问题,请在Github上提交Issue之前检查常见错误。