zoukankan      html  css  js  c++  java
  • 史上最简单的人脸识别项目登上GitHub趋势榜

    【导读】近日,一个名为 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_recognition

    特性

    1. 找到并定位图片中的所有人脸:
    史上最简单的人脸识别项目登上GitHub趋势榜

     

    import face_recognition
    image = face_recognition.load_image_file("your_file.jpg")
    face_locations = face_recognition.face_locations(image)

    2.识别人脸关键点(包括眼睛、鼻子、嘴和下巴)

    史上最简单的人脸识别项目登上GitHub趋势榜

     

    import face_recognition
    image = face_recognition.load_image_file("your_file.jpg")
    face_landmarks_list = face_recognition.face_landmarks(image)

    本人脸识别项目除了在某些关键领域有重要作用外,还有一个可能会引起 “玩心” 的 digital make-up 自动化妆功能(类似美图秀秀)。

    史上最简单的人脸识别项目登上GitHub趋势榜

     

    3.识别图片中的人是谁

    史上最简单的人脸识别项目登上GitHub趋势榜

     

    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

     

    史上最简单的人脸识别项目登上GitHub趋势榜

     

     

    安装

    环境配置:

    • Python 3.3+ or Python 2.7macOS or LinuxWindows(官方并不支持,但一些大神们摸索出了在Windows上运行的方法)

    不同操作系统的安装方法:

    1. 在 Mac 或者 Linux上安装本项目:

    方法一:

    首先安装dlib和相关Python依赖:

    https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

    然后通过 pip3 或者是 Python 2 的 pip2 用以下代码安装

    pip3 install face_recognition

    若未能安装成功,可以用Ubuntu虚拟机安装,参见这一教程:

    https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b

    (需要电脑中安装VMWare Player 或者 VirtualBox)

    方法二:

    修改你的 pip 镜像源为清华镜像,然后使用 pip install face_recognition,可以自动帮你安装各种依赖,包括dlib。只是在安装dlib的时候可能会出问题,因为dlib需要编译,出现的问题一般是gcc或者g++版本的问题,所以在pip install face_recognition之前,可以通过在命令行键入

    export CC=/usr/local/bin/gcc
    export CXX=/usr/local/bin/g++

    来指定你gcc和g++对应的位置,(这两句话会临时修改当前终端的环境变量/usr/local/bin/gcc对应你自己gcc或者g++所在目录)。

    2. 在树莓派上安装:

    树莓派安装指南:

    https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65

    3. 在Windows上安装:虽然本项目官方并不支持Windows,但一些大神们摸索出了在Windows上运行本项目的方法:@masoudr写的如何在Win10系统上安装 dlib库和 face_recognition项目的教程:

    https://github.com/ageitgey/face_recognition/issues/175#issue-257710508

    使用Ubuntu虚拟机镜像文件安装配置虚拟机,本项目已经包含在这个镜像中

    https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b

    使用方法

    1.使用命令行工具

    本项目安装完毕后,有两种命令行工具可供使用:

    • face_recognition - 在单张图片或一个图片文件夹中识别人脸身份。face_detection - 在单张图片或一个图片文件夹中定位人脸位置。
    史上最简单的人脸识别项目登上GitHub趋势榜

     

    face_recognition 命令行工具:

    face_recognition命令行工具可以在单张图片或一个图片文件夹中识别人脸身份。首先,需要一个你已经知道名字的人脸图片文件夹,一个人一张图,图片的文件名即为对应的人的名字,然后,需要第二个图片文件夹,文件夹里面是你希望识别的图片:

    史上最简单的人脸识别项目登上GitHub趋势榜

     

    然后,在命令行中切换到这两个文件夹所在路径,使用 face_recognition 命令行,传入这两个图片文件夹,然后就会输出未知图片中人的名字:

    $ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/
    /unknown_pictures/unknown.jpg,Barack Obama
    /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,112
    examples/image2.jpg,62,394,211,244
    examples/image2.jpg,95,941,244,792

    输出结果的每一行都对应图片中的一张脸,输出坐标代表着这张脸的上、右、下、左像素点坐标。

    调整人脸识别的容错率和敏感度

    如果一张脸识别出不止一个结果,那么这意味着他和其他人长的太像了(此外,本项目对于小孩和亚洲人的人脸识别准确率有待提升)。你可以把容错率调低一些,使识别结果更加严格。这个功能可通过传入参数 --tolerance 来实现,默认的容错率是0.6,容错率越低,识别越严格准确。

    $ 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有四个核心,那么你可以通过并行运算提升大概四倍的运算速度。

    如果你使用Python3.4或更新的版本,可以传入 --cpus <number_of_cpu_cores_to_use> 参数:

    $ face_recognition --cpus 4 ./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 ',' -f2
    Barack Obama
    unknown_person

    2.使用Python

    在 Python 中导入 face_recognition模块,调用丰富的API接口,用几行代码就可以轻松玩转各种人脸识别功能!API 接口文档:

    https://face-recognition.readthedocs.io

    如何定位人脸位置或者识别人脸身份?

    在 Python 中可以分别通过以下代码来实现在图片中定位人脸的位置

    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

    对人脸识别有精准要求怎么办?

    可以使用深度学习模型以达到更加精准的人脸定位,但要注意:这种方法需要GPU加速(通过英伟达显卡的CUDA库驱动),在编译安装dlib的时候也需要开启CUDA支持。

    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

    如果有大量图片需要识别,同时又有GPU,那么你可以参考这个例子:

    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

    史上最简单的人脸识别项目登上GitHub趋势榜

     

    识别奥巴马和拜登的人脸关键点

    更多案例:

    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

    • 案例:通过树莓派摄像头进行人脸个数统计及人脸身份识别

    https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_on_raspberry_pi.py

    • 案例:通过浏览器HTTP访问网络服务器进行人脸识别(需要安装Flask后端开发框架)

    https://github.com/ageitgey/face_recognition/blob/master/examples/web_service_example.py

    • 案例:基于K最近邻KNN分类算法进行人脸识别

    https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py

    关于 face_recognition的文章和教程

    • 本项目作者写的一篇文章 Modern Face Recognition with Deep Learning:

    https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78

    • 主要内容:基本算法和原理
    • Face recognition with OpenCV, Python, and deep learning by Adrian Rosebrock:

    https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/

    • 主要内容:如何实际使用本项目
    • Raspberry Pi Face Recognition by Adrian Rosebrock

    https://www.pyimagesearch.com/2018/06/25/raspberry-pi-face-recognition/

    • 主要内容:如何在树莓派上使用本项目
    • Face clustering with Python by Adrian Rosebrock

    https://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-c3cffc121d78

    警告说明

    本项目的人脸识别模型是基于成年人的,在孩子身上效果可能一般。如果图片中有孩子的话,建议把临界值设为0.6.不同人种的识别结果可能不同, 看wiki百科页面查看更多细节。

    把本项目部署在云服务器上 (Heroku, AWS等)

    本项目是基于C++库dlib的,所以把本项目部署在Heroku或者AWS的云端服务器上是很明智的。

    为了简化这个过程,有一个Dockerfile案例,教你怎么把face_recognition开发的app封装成Docker 容器文件,你可以把它部署在所以支持Docker镜像文件的云服务上。

    出了幺蛾子?

    如果出了问题,请在Github提交Issue之前查看 常见错误 。

  • 相关阅读:
    Android Studio自定义模板代码
    Android Studio自定义模板代码
    Android Studio自定义模板代码
    android studio 首字母提示 设置 大小写敏感
    android studio 首字母提示 设置 大小写敏感
    新入公司 问问题 ,快速了解代码的方法
    新入公司 问问题 ,快速了解代码的方法
    gradle编译很慢解决方法
    gradle编译很慢解决方法
    被relativeLayout的grivate center 折腾死了
  • 原文地址:https://www.cnblogs.com/wuhh123/p/11340481.html
Copyright © 2011-2022 走看看