介紹
Deepface 是一個用于 python 的輕量級人臉識別和人臉屬性分析(年齡、性別、情感和種族)框架。它是一個混合人臉識別框架。
deepface包含最先進的模型:VGG-Face、Google FaceNet、OpenFace、Facebook DeepFace和DeepIDArcFaceDlib。所有這些模型都封裝在一起,Deepface 的人臉識別準(zhǔn)確率高達 97%,并且已被證明在人臉檢測方面比一般的人臉識別框架更成功。Facebook 使用 Deepface 來防止其平臺上的假冒和身份盜用。
一個圖表示:(來源官網(wǎng))
每個模型的評分:
環(huán)境搭建
我的軟件環(huán)境:
- pycharm2021
- python3.9.6
模塊安裝:
pip install deepface
人臉驗證
對兩張圖片進行比對,看是否同一個人,圖片為:
以下三行代碼即可實現(xiàn),運行它會自動給你下載相關(guān)權(quán)重文件vgg_face_weights.h5:
from deepface import DeepFace
verification = DeepFace.verify(img1_path = "img1.jpg", img2_path = "img2.jpg")
print(verification)
結(jié)果如下:
{'verified': True, 'distance': 0.1600321561950978,
'threshold': 0.4, 'model': 'VGG-Face',
'detector_backend': 'opencv', 'similarity_metric': 'cosine'}
類型是個字典,我們來解析以下含義:
- verified=True:表示同一個人。這是驗證結(jié)果。
- distance參數(shù):這個值越小,代表相似度越高,越大則相似度越低。
- model:默認(rèn)用的模型VGG-Face。你也可以試試別的模型。
- detector_backend:要有opencv支持的意思
你也可以自己設(shè)置模型,比如設(shè)置為OpenFace:
rom deepface import DeepFace
models=["VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib"]
verification = DeepFace.verify(img1_path = "img.png", img2_path = "img_1.png",model_name=models[3])
print(verification)
運行如下:
看清楚:
第一個,下載到的位置:
To: C:\Users\hp\.deepface\weights\openface_weights.h5
第二個是結(jié)果:
{'verified': False, 'distance': 0.17080708434087122,
'threshold': 0.1, 'model': 'OpenFace',
'detector_backend': 'opencv',
'similarity_metric': 'cosine'}
人臉查找
from deepface import DeepFace
img_path1 = r'img.png'
img_path2 = r'img_1.png'
img_path1 = img_path1.replace('\\', '/')
img_path2 = img_path2.replace('\\', '/')
models=["VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace", "DeepID", "ArcFace", "Dlib"]
db_path = r'data'
db_path = db_path.replace('\\', '/')
recognition = DeepFace.find(img_path = img_path2, db_path = db_path, model_name = models[0], enforce_detection=False)
print(recognition)
輸出如下:
ind function lasts 4.07146143913269 seconds
identity VGG-Face_cosine
0 data/img_2.png 0.160032
1 data/img.png 0.265129
identity表示身份的意思,VGG-Face_cosine表示的是相似度的意思應(yīng)該,它會得到相似最高的圖。
為什么要設(shè)enforce_detection= False?不設(shè)置會報錯檢測不到人臉,因為Deepface會將db_path指定的相片folder中所有的相片,取得每張相片特征后儲存于相同路徑下(副檔名為pkl),下次再取用比對時便不需要重新跑模型取得特征了。但如果其中有無法偵測出臉孔的相片,此時若enforce_detection=False,執(zhí)行時便會產(chǎn)生錯誤并停止執(zhí)行,以提醒我們資料夾中有無法檢測臉孔的相片,請重新review。待確認(rèn)folder中的相片無誤后,我們便可設(shè)定enforce_detection=False,告知Deepface不需要提醒,以提升后續(xù)執(zhí)行的速度。
人臉屬性(年齡,標(biāo)簽,性別,種族)識別
DeepFace 帶有強大的面部屬性分析模塊,用于年齡、性別、情緒和種族/民族預(yù)測。雖然 DeepFace 的面部識別模塊封裝了現(xiàn)有的最先進模型,但其面部屬性分析有自己的模型。目前,年齡預(yù)測模型的平均絕對誤差為 +/- 4.6 歲;性別預(yù)測模型的準(zhǔn)確率達到 97%。
Deepface 還提供面部屬性分析,包括、、age(包括憤怒、恐懼、中性、悲傷、厭惡、快樂和驚訝)和(包括亞洲人、白人、中東人、印度人、拉丁裔和黑人)預(yù)測。
來測試以下男神的年齡大小吧:
from deepface import DeepFace
analysis = DeepFace.analyze(img_path="img.png", actions=["age", "gender", "emotion", "race"])
print(analysis)
輸出為:
說明:年齡,21;性別,男;表情,自然;種族:亞洲人(官方只能提供到是哪個洲的人)
補充:如果你遇到報錯OSError: Unable to open file (truncated file: eof = 5865472, sblock->base_addr = 0, stored_eof = 588,你就到weights路徑下把原來的刪除,重新執(zhí)行代碼下載文件
比如我報錯:
重新執(zhí)行代碼下載即可,有時候會遇到下載失?。河捎谶B接方在一段時間后沒有正確答復(fù)或連接的主機沒有反應(yīng),連接嘗試失敗,可以多嘗試幾次(甚至十幾次),畢竟github有些同學(xué)本來就諒解不穩(wěn)定。實在不行還是報錯,那就手動下載吧,我用wegt命令下載,比如:
使用如下命令下載到本地(或者直接點擊鏈接去下載):
wget -c https://github.com/serengil/deepface_models/releases/download/v1.0/gender_model_weights.h5
下載好后放到weights文件夾下一樣:
視頻流識別
from deepface import DeepFace
a=DeepFace.stream()
print(a)
文章來源:http://www.zghlxwxcb.cn/news/detail-443186.html
參考
https://pypi.org/project/deepface/
https://www.cs.toronto.edu/~ranzato/publications/taigman_cvpr14.pdf文章來源地址http://www.zghlxwxcb.cn/news/detail-443186.html
到了這里,關(guān)于deepface:最先進輕量級人臉識別和人臉屬性分析框架講解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!