Python3 利用face_recognition實現(xiàn)人臉識別的方法
前言
之前實踐了下face++在線人臉識別版本,這回做一下離線版本。github 上面有關(guān)于face_recognition的相關(guān)資料,本人只是做個搬運工,對其中的一些內(nèi)容進(jìn)行搬運,對其中一些例子進(jìn)行實現(xiàn)。
官方描述:
face_recognition是一個強大、簡單、易上手的人臉識別開源項目,并且配備了完整的開發(fā)文檔和應(yīng)用案例,特別是兼容樹莓派系統(tǒng)。本項目是世界上最簡潔的人臉識別庫,你可以使用Python和命令行工具提取、識別、操作人臉。本項目的人臉識別是基于業(yè)內(nèi)領(lǐng)先的C++開源庫 dlib中的深度學(xué)習(xí)模型,用Labeled Faces in the Wild人臉數(shù)據(jù)集進(jìn)行測試,有高達(dá)99.38%的準(zhǔn)確率。但對小孩和亞洲人臉的識別準(zhǔn)確率尚待提升。
(關(guān)于兼容樹莓派,以后有板子了再做一下)
下面兩個鏈接劃重點
https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md
https://face-recognition.readthedocs.io/en/latest/face_recognition.html
環(huán)境配置
- ubuntu16.04(其他環(huán)境的安裝可以參考第一個鏈接,官方有說明)
- pycharm(可忽略,怎么舒服怎么來)
- python3
- opencv(我的是4.1.2,三點幾的版本應(yīng)該也一樣)
實際上只需要安裝face_recognition,當(dāng)然,沒有opencv的也需要安裝一下opencv
pip3 install face_recognition
圖片準(zhǔn)備
由于需要做一些圖片的比對,因此需要準(zhǔn)備一些圖片,本文圖片取自以下鏈接
https://www.zhihu.com/question/314169580/answer/872770507
接下來開始操作
官方還有提供命令行的操作(這個沒去做),本文不做這個,我們只要是要在python中用face_recognition,因此定位到這一塊。

這個api文檔地址就是上面的第二個鏈接。進(jìn)去之后可以看到:

part1.識別圖片中的人是誰
代碼
# part1
# 識別圖片中的人是誰
import face_recognition
known_image = face_recognition.load_image_file("lyf1.jpg")
unknown_image = face_recognition.load_image_file("lyf2.jpg")
lyf_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([lyf_encoding], unknown_encoding)
# A list of True/False values indicating which known_face_encodings match the face encoding to check
print(type(results))
print(results)
if results[0] == True:
print("yes")
else:
print("no")
結(jié)果
<class 'list'>
[True]
yes
part2.從圖片中找到人臉
代碼
# part2
# 從圖片中找到人臉(定位人臉位置)
import face_recognition
import cv2
image = face_recognition.load_image_file("lyf1.jpg")
face_locations_useCNN = face_recognition.face_locations(image,model='cnn')
# model – Which face detection model to use. “hog” is less accurate but faster on CPUs.
# “cnn” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is “hog”.
face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因為返回值的順序是這樣子的,因此在后面的for循環(huán)里面賦值要注意按這個順序來
print("face_location_useCNN:")
print(face_locations_useCNN)
face_num1=len(face_locations_useCNN)
print(face_num1) # The number of faces
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2) # The number of faces
# 到這里為止,可以觀察兩種情況的坐標(biāo)和人臉數(shù),一般來說,坐標(biāo)會不一樣,但是檢測出來的人臉數(shù)應(yīng)該是一樣的
# 也就是說face_num1?。健ace_num2; face_locations_useCNN 和 face_locations_noCNN 不一樣
org = cv2.imread("lyf1.jpg")
img = cv2.imread("lyf1.jpg")
cv2.imshow("lyf1.jpg",img) # 原始圖片
# Go to get the data and draw the rectangle
# use CNN
for i in range(0,face_num1):
top = face_locations_useCNN[i][0]
right = face_locations_useCNN[i][1]
bottom = face_locations_useCNN[i][2]
left = face_locations_useCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.rectangle(img, start, end, color, thickness) # opencv 里面畫矩形的函數(shù)
# Show the result
cv2.imshow("useCNN",img)
# for face_location in face_locations_noCNN:
#
# # Print the location of each face in this image
# top, right, bottom, left = face_location
# # 等價于下面的這種寫法
for i in range(0,face_num2):
top = face_locations_noCNN[i][0]
right = face_locations_noCNN[i][1]
bottom = face_locations_noCNN[i][2]
left = face_locations_noCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
結(jié)果
face_location_useCNN:
[(223, 470, 427, 266)]
1
face_location_noCNN:
[(242, 489, 464, 266)]
1
圖片效果大致是這樣

part3.找到人臉并將其裁剪打印出來(使用cnn定位人臉)
代碼
# part3
# 找到人臉并將其裁剪打印出來(使用cnn定位人臉)
from PIL import Image
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")
face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
print("I found {} face(s) in this photograph.".format(len(face_locations)))
for face_location in face_locations:
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
結(jié)果
I found 1 face(s) in this photograph.
A face is located at pixel location Top: 205, Left: 276, Bottom: 440, Right: 512
圖片效果大致是這樣

part4.識別單張圖片中人臉的關(guān)鍵點
代碼
# part4 識別單張圖片中人臉的關(guān)鍵點
from PIL import Image, ImageDraw
import face_recognition
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
# Create a PIL imagedraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
# Print the location of each facial feature in this image
for facial_feature in face_landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
# Let's trace out each facial feature in the image with a line!
for facial_feature in face_landmarks.keys():
d.line(face_landmarks[facial_feature], width=5)
# Show the picture
pil_image.show()
結(jié)果
I found 1 face(s) in this photograph.
The left_eyebrow in this face has the following points: [(305, 285), (321, 276), (340, 277), (360, 281), (377, 288)]
The right_eye in this face has the following points: [(422, 313), (432, 303), (446, 302), (459, 305), (449, 312), (435, 314)]
The nose_bridge in this face has the following points: [(394, 309), (394, 331), (395, 354), (396, 375)]
The right_eyebrow in this face has the following points: [(407, 287), (424, 278), (442, 273), (461, 272), (478, 279)]
The bottom_lip in this face has the following points: [(429, 409), (419, 421), (408, 428), (398, 430), (389, 429), (377, 424), (364, 412), (370, 413), (389, 414), (398, 415), (407, 413), (423, 411)]
The chin in this face has the following points: [(289, 295), (291, 323), (296, 351), (303, 378), (315, 403), (332, 428), (353, 448), (376, 464), (400, 467), (422, 461), (441, 444), (459, 425), (473, 403), (484, 377), (490, 351), (493, 323), (493, 296)]
The top_lip in this face has the following points: [(364, 412), (377, 407), (389, 403), (397, 406), (406, 402), (417, 405), (429, 409), (423, 411), (406, 412), (397, 414), (389, 413), (370, 413)]
The left_eye in this face has the following points: [(327, 308), (339, 304), (353, 306), (364, 314), (352, 317), (338, 316)]
The nose_tip in this face has the following points: [(375, 383), (386, 387), (396, 390), (407, 385), (416, 381)]
圖片效果

到此這篇關(guān)于Python3 利用face_recognition實現(xiàn)人臉識別的方法的文章就介紹到這了,更多相關(guān)Python3 人臉識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python基于Opencv來快速實現(xiàn)人臉識別過程詳解(完整版)
- python實現(xiàn)人臉識別代碼
- Python 40行代碼實現(xiàn)人臉識別功能
- python實現(xiàn)人臉識別經(jīng)典算法(一) 特征臉法
- python+opencv實現(xiàn)的簡單人臉識別代碼示例
- Python人臉識別第三方庫face_recognition接口說明文檔
- python使用opencv進(jìn)行人臉識別
- 詳解如何用OpenCV + Python 實現(xiàn)人臉識別
- Python3利用Dlib19.7實現(xiàn)攝像頭人臉識別的方法
- 基于python+opencv調(diào)用電腦攝像頭實現(xiàn)實時人臉眼睛以及微笑識別
相關(guān)文章
python re的findall和finditer的區(qū)別詳解
這篇文章主要介紹了python re的findall和finditer的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python使用smtplib模塊發(fā)送電子郵件的流程詳解
Python中自帶的smtplib模塊可以進(jìn)行基于SMTP協(xié)議的郵件操作,這里我們便總結(jié)了Python使用smtplib模塊發(fā)送電子郵件的流程詳解,并對一些常見的問題給出了解決方法:2016-06-06
python編程使用PyQt創(chuàng)建UE藍(lán)圖
這篇文章主要為大家介紹了python編程中如何使用PyQt創(chuàng)建UE藍(lán)圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
使用Python的Dataframe取兩列時間值相差一年的所有行方法
今天小編就為大家分享一篇使用Python的Dataframe取兩列時間值相差一年的所有行方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
mac安裝pytorch及系統(tǒng)的numpy更新方法
今天小編就為大家分享一篇mac安裝pytorch及系統(tǒng)的numpy更新方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python實現(xiàn)處理mysql結(jié)果輸出方式
這篇文章主要介紹了python實現(xiàn)處理mysql結(jié)果輸出方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

