1. 程式人生 > >子豪兄教你從零開始實現人臉識別

子豪兄教你從零開始實現人臉識別

Github開源人臉識別專案face_recognition

譯者注:

本專案face_recognition是一個強大、簡單、易上手的人臉識別開源專案,並且配備了完整的開發文件和應用案例,特別是相容樹莓派系統。

為了便於中國開發者研究學習人臉識別、貢獻程式碼,我將本專案README檔案翻譯成中文

向本專案的所有貢獻者致敬。

英譯漢:同濟大學開源軟體協會 子豪兄Tommy

Translator’s note:

face_recognition is a powerful, simple and easy-to-use face recognition open source project with complete development documents and application cases, especially it is compatible with Raspberry Pi.

In order to facilitate Chinese software developers to learn, make progress in face recognition development and source code contributions, I translated README file into simplified Chinese.

Salute to all contributors to this project.

Translator: Tommy in Tongji Univerisity Opensource Association 子豪兄Tommy

本專案是世界上最簡潔的人臉識別庫,你可以使用Python和命令列工具提取、識別、操作人臉。

本專案的人臉識別是基於業內領先的C++開源庫 dlib中的深度學習模型,用Labeled Faces in the Wild人臉資料集進行測試,有高達99.38%的準確率。但對小孩和亞洲人臉的識別準確率尚待提升。

Labeled Faces in the Wild是美國麻省大學安姆斯特分校(University of Massachusetts Amherst)製作的人臉資料集,該資料集包含了從網路收集的13,000多張面部影象。

本專案提供了簡易的face_recognition命令列工具,你可以用它處理整個資料夾裡的圖片。

PyPI
Build Status
Documentation Status

特性

從圖片裡找到人臉

定點陣圖片中的所有人臉:

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]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

你也可以配合其它的Python庫(比如opencv)實現實時人臉檢測:

安裝

環境配置

  • Python 3.3+ or Python 2.7
  • macOS or Linux
  • Windows並不是我們官方支援的,但也許也能用

不同作業系統的安裝方法

在 Mac 或者 Linux上安裝本專案

First, make sure you have dlib already installed with Python bindings:

第一步,安裝dlib和相關Python依賴:

Then, install this module from pypi using pip3 (or pip2 for Python 2):

pip3 install face_recognition

在樹莓派上安裝

在Windows上安裝

雖然本專案官方並不支援Windows,但一些大神們摸索出了在Windows上執行本專案的方法:

使用Ubuntu虛擬機器映象檔案安裝配置虛擬機器,本專案已經包含在這個映象中

使用方法

命令列介面

當你安裝好了本專案,你可以使用兩種命令列工具:

  • face_recognition - 在單張圖片或一個圖片資料夾中認出是誰的臉。
  • face_detection - 在單張圖片或一個圖片資料夾中定位人臉位置。

face_recognition 命令列工具

face_recognition命令列工具可以在單張圖片或一個圖片資料夾中認出是誰的臉。

首先,你得有一個你已經知道名字的人臉圖片資料夾,一個人一張圖,圖片的檔名即為對應的人的名字:

known

然後,你需要第二個圖片資料夾,資料夾裡面是你希望識別的圖片:

unknown

然後,你在命令列中切換到這兩個資料夾所在路徑,然後使用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
更多的例子

如果你並不在乎圖片的檔名,只想知道資料夾中的圖片裡有誰,可以用這個管道命令:

$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2

Barack Obama
unknown_person
加速人臉識別運算

如果你的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的所有核心。

子豪兄批註:樹莓派3B有4個CPU核心,傳入多核引數可以顯著提升圖片識別的速度(親測)。

Python 模組:face_recognition

在Python中,你可以匯入face_recognition模組,呼叫我們提供的豐富的API介面,用幾行程式碼就可以輕鬆玩轉各種人臉識別功能!

在圖片中定位人臉的位置
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!

案例:定位拜登的臉

你也可以使用深度學習模型達到更加精準的人臉定位。

注意:這種方法需要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!
識別單張圖片中人臉的關鍵點
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.
識別圖片中的人是誰
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 me!")

Python 案例

人臉定位

人臉關鍵點識別

人臉識別

關於 face_recognition的文章和教程

人臉識別的原理

如果你想更深入瞭解人臉識別這個黑箱的原理 讀這篇文章

子豪兄批註:一定要看這篇文章,講的既有趣又有料。

警告說明

  • 本專案的人臉識別模型是基於成年人的,在孩子身上效果可能一般。如果圖片中有孩子的話,建議把臨界值設為0.6.
  • 不同人種的識別結果可能不同, 看wiki百科頁面 檢視更多細節。

把本專案部署在雲伺服器上 (Heroku, AWS等)

本專案是基於C++庫dlib的,所以把本專案部署在Heroku或者AWS的雲端伺服器上是很明智的。

為了簡化這個過程,有一個Dockerfile案例,教你怎麼把face_recognition開發的app封裝成Docker 容器檔案,你可以把它部署在所以支援Docker映象檔案的雲服務上。

出了么蛾子?

如果出了問題,請在Github提交Issue之前檢視 常見錯誤

鳴謝

  • 非常感謝 Davis King (@nulhom)建立了dlib庫,提供了響應的人臉關鍵點檢測和人臉編碼相關的模型,你可以檢視 blog post這個網頁獲取更多有關ResNet的資訊。
  • 感謝每一個相關Python模組(包括numpy,scipy,scikit-image,pillow等)的貢獻者。