本文的模型使用了C++工具箱 dlib 基於深度學習的最新人臉識別方法,基於戶外臉部數據測試庫Labeled Faces in the Wild 的基准水平來說,達到了99.38%的准確率。
dlib :dlib C++ Library
數據測試庫Labeled Faces in the Wild:LFW Face Database : Main
模型提供了一個簡單的 face_recognition 命令行工具讓用戶通過命令就能直接使用圖片文件夾進行人臉識別操作。
特征
在圖片中捕捉人臉
在一張圖片中捕捉到所有的人臉
找到並處理圖片中人臉的特征
找到每個人眼睛、鼻子、嘴巴和下巴的位置和輪廓。
import face_recognition
image = face_recognition.load_image_file("your_file.jpg")
face_locations = face_recognition.face_locations(image)
捕捉臉部特征有很重要的用途,當然也可以用來進行圖片的數字美顏digital make-up(例如美圖秀秀)
digital make-up:https://github.com/ageitgey/face_recognition/blob/master/examples/digital_makeup.py
識別圖片中的人臉
識別誰出現在照片里
安裝步驟
本方法支持Python3/python2,我們只在macOS和Linux中測試過,還不知是否適用於Windows。
使用 pypi 的 pip3 安裝此模塊(或是Python 2的pip2)
重要提示:在編譯 dlib 時可能會出問題,你可以通過安裝來自源(而不是pip)的dlib來修復錯誤,請見安裝手冊How to install dlib from source
https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
通過手動安裝 dlib,運行pip3 install face_recognition來完成安裝。
使用方法
命令行界面
當你安裝face_recognition,你能得到一個簡潔的叫做face_recognition的命令行程序,它能幫你識別一張照片或是一個照片文件夾中的所有人臉。
首先,你需要提供一個包含一張照片的文件夾,並且你已經知道照片中的人是誰,每個人都要有一張照片文件,且文件名需要以該人的姓名命名;
然后你需要准備另外一個文件夾,里面裝有你想要識別人臉照片;
接下來你只用運行face_recognition命令,程序能夠通過已知人臉的文件夾識別出未知人臉照片中的人是誰;
針對每個人臉都要一行輸出,數據是文件名加上識別到的人名,以逗號分隔。
如果你只是想要知道每個照片中的人名而不要文件名,可以進行如下操作:
Python模塊
你可以通過引入face_recognition 就能完成人臉識別操作:
在圖片中自動識別所有人臉
請參照此案例this example: https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
識別圖片中的人臉並告知姓名
請參照此案例this example: https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py
Python代碼案例
所有例子在此 here.
· 找到照片中的人臉Find faces in a photograph
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
· 識別照片中的面部特征Identify specific facial features in a photograph
https://github.com/ageitgey/face_recognition/blob/master/examples/find_facial_features_in_picture.py
· 使用數字美顏Apply (horribly ugly) digital make-up
https://github.com/ageitgey/face_recognition/blob/master/examples/digital_makeup.py
· 基於已知人名找到並識別出照片中的未知人臉Find and recognize unknown faces in a photograph based on photographs of known people
https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py