人臉識別項目介紹

項目介紹
人臉識別
從Python或命令行中識別和操作面部
世界上最簡單的人臉識別庫。
使用dlib的最新人臉識別功能構建
建立在深度學習之上。該模型的精度爲99.38%。
Wild 基準中的標記面孔。
這也提供了一個簡單的face_recognition命令行工具,
您可以通過命令行在圖像文件夾上進行人臉識別!

特徵
在圖片中查找面孔
查找出現在圖片中的所有面孔:

image3

import face_recognition
image = face_recognition.load_image_file(“your_file.jpg”)
face_locations = face_recognition.face_locations(image)
查找和操縱圖片中的面部特徵
獲取每個人的眼睛,鼻子,嘴巴和下巴的位置和輪廓。

圖4
在這裏插入圖片描述
import face_recognition
image = face_recognition.load_image_file(“your_file.jpg”)
face_landmarks_list = face_recognition.face_landmarks(image)
查找面部特徵對於許多重要的東西超級有用。但是您也可以使用真正愚蠢的東西
像使用數碼彩妝 (想想“美圖”):
圖片5

識別圖片中的面孔
識別出現在每張照片中的人。

圖6

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庫一起使用以進行實時人臉識別:

image7

有關 https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py代碼,請參見此示例。

安裝
要求
Python 3.3+或Python 2.7
macOS或Linux(Windows不受官方支持,但可能可以使用)
在Mac或Linux上安裝
首先,確保已經安裝了帶有Python綁定的dlib:

如何在macOS或Ubuntu上從源代碼安裝dlib
https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
然後,使用安裝一封來自PyPI此模塊PIP3(或PIP2用於Python 2):

pip3安裝face_recognition
如果您在安裝時遇到問題,也可以嘗試
https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b
預先配置的VM。
在Raspberry Pi 2+上安裝
https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
Raspberry Pi 2+安裝說明
在Windows上安裝
雖然不正式支持Windows,但有幫助的用戶已經發布了有關如何安裝此庫的說明:

@masoudr的Windows 10安裝指南(dlib + face_recognition)
安裝預配置的虛擬機映像
https://github.com/ageitgey/face_recognition/issues/175#issue-257710508
下載預配置的VM映像 (用於VMware Player或VirtualBox)。
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是形象,並沒有任何人相匹配的臉
您的已知人物文件夾。
調整公差/靈敏度
如果同一個人獲得多個匹配項,則可能是
您照片中的人物看起來非常相似且容忍度較低
需要使面部比較更加嚴格。
您可以使用–tolerance參數來實現。默認公差
值是0.6,數字越小,臉部比較就越嚴格:
$ face_recognition-公差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,無
更多例子
如果您只是想知道每張照片中的人物姓名,但不知道
關心文件名,您可以這樣做:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | 切-d ‘,’- f2

巴拉克奧巴馬
不明身份的人
加快人臉識別
如果您的計算機具有以下功能,則可以並行完成人臉識別
多個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模塊,然後輕鬆進行操作
只需幾行代碼即可完成。超級容易!
API文檔:https : //face-recognition.readthedocs.io。

自動查找圖像中的所有面孔
導入 face_recognition

圖片 = face_recognition.load_image_file (“my_picture.jpg” )
face_locations = face_recognition.face_locations (圖片)

#face_locations現在是一個數組,列出了每個面孔的座標!
看這個例子https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
試試看。
您還可以選擇使用更爲精確的基於深度學習的面部檢測模型。

注意:GPU加速(通過nvidia的CUDA庫)是必需的
該模型的性能。您還需要啓用CUDA支持
編譯dlib時。
導入 face_recognition

image = face_recognition.load_image_file (“my_picture.jpg” )
face_locations = face_recognition 。face_locations (image , model = “ cnn” )

#face_locations現在是一個數組,列出了每個面孔的座標!
看這個例子
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
試試看。
識別圖像中的人臉並確定他們是誰
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!”)
看這個例子
https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_pictures.py
試試看。
Python代碼示例
所有示例均 在此處提供。https://github.com/ageitgey/face_recognition/tree/master/examples

人臉檢測
在照片中查找面孔
https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
在照片中查找面孔(使用深度學習)
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
面部特徵
識別照片中的特定面部特徵
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
通過數字面部距離比較面部,而不是僅通過True / False匹配
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
在帶有相機的Raspberry Pi上識別臉部
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_on_raspberry_pi.py
運行Web服務以通過HTTP識別人臉(需要安裝Flask)
https://github.com/ageitgey/face_recognition/blob/master/examples/web_service_example.py
使用K近鄰分類器識別人臉
https://github.com/ageitgey/face_recognition/blob/master/examples/face_recognition_knn.py
人臉識別的工作原理

如果您想了解人臉定位和識別的工作原理而不是
取決於黑匣子庫,請閱讀我的文章。https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78
注意事項
人臉識別模型是針對成人訓練的,對兒童而言效果不佳。使用默認比較閾值0.6時,往往會很容易混淆孩子。
部署到雲主機(Heroku,AWS等)
由於face_recognition依賴於用C ++編寫的dlib,因此部署應用程序可能很棘手
使用它到諸如Heroku或AWS的雲託管提供商。
爲了使事情變得更簡單,此倉庫中有一個示例Dockerfile(https://www.docker.com/),該示例顯示瞭如何運行使用
face_recognition在泊塢 容器。這樣,您應該可以部署
到支持Docker映像的任何服務。
常見問題
問題:使用face_recognition或運行示例時,指令無效(內核已轉儲)。

解決方案:dlib是使用SSE4或AVX支持編譯的,但是您的CPU太舊並且不支持它。
在完成此處概述的代碼更改之後,您將需要重新編譯dlib。https://github.com/ageitgey/face_recognition/issues/11#issuecomment-287398611
問題: RuntimeError:不支持的圖像類型,必須爲8位灰度或RGB圖像。 在運行網絡攝像頭示例時。

解決方案:您的網絡攝像頭可能未使用OpenCV正確設置。在這裏查看更多。https://github.com/ageitgey/face_recognition/issues/21#issuecomment-287779524

問題:運行pip2 install face_recognition時出現MemoryError

解決方案:face_recognition_models文件太大,無法使用可用的點緩存。代替,
嘗試使用pip2 --no-cache-dir安裝face_recognition可以避免此問題。
問題: AttributeError:‘模塊’對象沒有屬性’face_recognition_model_v1’

解決方案:您安裝的dlib版本太舊。您需要版本19.7或更高版本。升級dlib。

問題: 屬性錯誤:“模塊”對象沒有屬性“ cnn_face_detection_model_v1”

解決方案:您安裝的dlib版本太舊。您需要版本19.7或更高版本。升級dlib。

問題:TypeError:imread()獲得了意外的關鍵字參數’mode’

解決方案:您安裝的scipy版本太舊。您需要版本0.17或更高版本。升級祕訣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章