Github開源人臉識別項目face_recognition


Github開源人臉識別項目face_recognition

原文:https://www.jianshu.com/p/0b37452be63e

譯者注:

本項目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命令行工具,你可以用它處理整個文件夾里的圖片。

特性

從圖片里找到人臉

定位圖片中的所有人臉:

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

識別人臉關鍵點

識別人臉關鍵點,包括眼睛、鼻子、嘴和下巴。

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

識別人臉關鍵點在很多領域都有用處,但同樣你也可以把這個功能玩壞,比如本項目的 digital make-up自動化妝案例(就像美圖秀秀一樣)。

 
image

識別圖片中的人是誰

 
image
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)實現實時人臉檢測:

 
image

看這個案例 實時人臉檢測

安裝

環境配置

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

不同操作系統的安裝方法

在 Mac 或者 Linux上安裝本項目

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

然后,用pip命令從pypi社區上下載本項目的pyhon模塊:

pip3命令下載的是python3對應的版本,pip命令下載的是python2對應的版本

pip3 install face_recognition

如果你遇到了幺蛾子,可以用Ubuntu虛擬機安裝本項目,看下面這個教程。
如何使用Adam Geitgey大神提供的Ubuntu虛擬機鏡像文件安裝配置虛擬機,本項目已經包含在鏡像中.

在樹莓派上安裝

在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接口,用幾行代碼就可以輕松玩轉各種人臉識別功能!

API 接口文檔: 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! 

案例:定位拜登的臉

 
案例:定位拜登的臉

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

注意:這種方法需要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! 

案例:使用卷積神經網絡深度學習模型定位拜登的臉

如果你有很多圖片需要識別,同時又有GPU,那么你可以參考這個例子:案例:使用卷積神經網絡深度學習模型批量識別圖片中的人臉.

識別單張圖片中人臉的關鍵點
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 案例

所有案例都在這個鏈接中 也就是examples文件夾.

人臉定位

人臉關鍵點識別

人臉識別

關於 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等)的貢獻者。
  • 感謝 Cookiecutteraudreyr/cookiecutter-pypackage 項目模板,使得Python的打包方式更容易接受。



作者:人工智能小技巧
鏈接:https://www.jianshu.com/p/0b37452be63e
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM