1. 程式人生 > >開源 | 基於Python的人臉識別:識別準確率高達99.38%!

開源 | 基於Python的人臉識別:識別準確率高達99.38%!

該庫使用 dlib 頂尖的深度學習人臉識別技術構建,在戶外臉部檢測資料庫基準(Labeled Faces in the Wild benchmark)上的準確率高達 99.38%。這也提供了一個簡單的 face_recognition 命令列工具,你可以開啟命令列中任意影象資料夾,進行人臉識別!

特徵找出下面圖片中所有的人臉:


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)

找出臉部特徵對很多重要的事情都非常有用。但是你也可以用它來做一些「蠢事」,比如數字化妝(美圖):


識別圖片中的人臉:識別每張圖片中的人物。


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 庫執行實時人臉識別:


程式碼:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

安裝要求

  • Python 3+ 或 Python 2.7

  • macOS 或 Linux (Windows 未測試)

  • 還可在樹莓派 2+上執行(按照具體指令來安裝執行:https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65)

  • 預配置的 VM 影象同樣可用。

使用pin3從pypi安裝這一模組:

pip3 install face_recognition

重要提示:pip 嘗試編譯 dlib 依賴時很可能會遇到一些問題。如果遇到問題,前往該地址(https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf)從來源(而不是 pip)中安裝 dlib,從而修復該錯誤。

手動安裝 dlib 後,再次執行 pip3 install face_recognition,完成安裝。

如果安裝還有問題,你還可以試試預配置的 VM(https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b

用途:命令列介面

安裝 face_recognition 時,你會得到一個名為 face_recognition 的簡單命令列程式,該程式可用於識別照片或裝滿照片的資料夾中的人臉。

首先,你需要提供一個包含圖片的資料夾,且每張圖片中的每個人你都認識。每個人有一個影象檔案,檔名就是圖片中人物的名字:


然後,你需要再建一個資料夾,包含你想要識別的影象檔案:


然後,你僅需要在已知人物資料夾和未知人物資料夾(或單個影象)中執行 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_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2 Barack Obama unknown_person

如果你的電腦配有多核 CPU,你就可以同時執行多個人臉識別任務。例如,如果你的系統有 4 個 CPU 核,你可以同時使用這 4 個 CPU 核,那麼同樣時間內處理的影象數量是原來的四倍。

如果你使用 Python 3.4 或更新的版本,傳入--cpus <number_of_cpu_cores_to_use>引數:

$ face_recognition -cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

你還可以傳入--cpus -1,來使用系統中所有的 CPU 核。

Python 模組:使用 face_recognition 模組,幾行程式碼輕鬆控制人臉,so easy!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!

影象人臉識別

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!")

注意事項

該人臉識別模型基於成年人照片訓練,因此對兒童照片的識別效果不好。該模型預設比較閾值是 0.6,容易混淆兒童的面部。

將該模型配置到雲主機(Heroku、AWS 等)

face_recognition 賴以存在的 dlib 是用 C++語言寫的,因此將該內建該模型的 app 配置到 Heroku 或 AWS 等雲主機提供商就很複雜。在該 repo 中有一個 Dockerfile 示例,展示如何在 Docker 容器中執行內建 face_recognition 模型的 app(詳見該網址:https://www.docker.com/)。參考該示例,您能夠將該模型配置到任何支援 Docker 影象的服務。

常見問題

問題1:使用 face_recognition 或執行樣本時,出現 Illegal instruction (core dumped)。

解決方案:dlib 需要在 SSE4 或 AVX 支援下編譯,但是你的 CPU 太舊,無法支援編譯。你需要根據此處(https://github.com/ageitgey/face_recognition/issues/11#issuecomment-287398611)所示修改程式碼,然後對 dilb 進行重新編譯。

問題2:執行攝像頭樣本時,出現 RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

解決方案:你的攝像頭可能並未在 OpenCV 上正確設定。點選此處(https://github.com/ageitgey/face_recognition/issues/21#issuecomment-287779524)瞭解更多。

問題3:執行 pip2 install face_recognition 時出現 MemoryError。

解決方案:face_recognition_models 檔案太大,不適合你可用的 pip 快取記憶體。試一下 pip2 --no-cache-dir install face_recognition,解決該問題。

問題4:AttributeError: 'module' object has no attribute 'face_recognition_model_v1'

解決方案:你安裝的 dlib 版本過舊,需要 19.4 或者更新的版本。請升級 dlib 版本。

問題5:TypeError: imread() got an unexpected keyword argument 'mode'

解決方案:你安裝的 scipy 版本過舊,需要 0.17 或者更新的版本。請升級 scipy 版本。

開源地址:https://github.com/ageitgey/face_recognition#face-recognition

-END-