API 接口文檔: https://face-recognition.readthedocs.io
定位圖片中所有人臉

1 import face_recognition 2 image = face_recognition.load_image_file("your_file.jpg") 3 face_locations = face_recognition.face_locations(image) 4 #face_locations 以列表形式返回圖片中的所有人臉

1 import face_recognition 2 from PIL import Image 3 4 image = face_recognition.load_image_file("your_file.jpg") 5 face_locations = face_recognition.face_locations(image) 6 #face_locations 以列表形式返回圖片中的所有人臉 7 8 top, right, bottom, left = face_locations[0] 9 face_image = image[top:bottom, left:right] 10 pil_image = Image.fromarray(face_image) 11 pil_image.save(fp=“your_new_file_path.jpg”)
你也可以使用深度學習模型達到更加精准的人臉定位。
注意:這種方法需要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
image = face_recognition.load_image_file("my_picture.jpg") face_landmarks_list = face_recognition.face_landmarks(image)
face_landmarks源碼中五官對應的點"chin": points[0:17],
"left_eyebrow": points[17:22],
"right_eyebrow": points[22:27],
"nose_bridge": points[27:31],
"nose_tip": points[31:36],
"left_eye": points[36:42],
"right_eye": points[42:48],
"top_lip": points[48:55] + [points[64]] + [points[63]] + [points[62]] + [points[61]] + [points[60]],
"bottom_lip": points[54:60] + [points[48]] + [points[60]] + [points[67]] + [points[66]] + [points[65]] + [points[64]
top_lip --上嘴唇
face_landmarks_list[0]['top_lip'][0] --points[48] face_landmarks_list[0]['top_lip'][1] --points[49] face_landmarks_list[0]['top_lip'][2] --points[50] face_landmarks_list[0]['top_lip'][3] --points[51] face_landmarks_list3[0]['top_lip'][4] --points[52] face_landmarks_list3[0]['top_lip'][5] --points[53] face_landmarks_list3[0]['top_lip'][6] --points[54] face_landmarks_list3[0]['top_lip'][7] --points[64] face_landmarks_list3[0]['top_lip'][8] --points[63] face_landmarks_list3[0]['top_lip'][9] --points[62] face_landmarks_list3[0]['top_lip'][10] --points[61] face_landmarks_list3[0]['top_lip'][11] --points[60] bottom_lip --下嘴唇 face_landmarks_list[0]['bottom_lip'][0] --points[54] face_landmarks_list[0]['bottom_lip'][1] --points[55] face_landmarks_list[0]['bottom_lip'][2] --points[56] face_landmarks_list[0]['bottom_lip'][3] --points[57] face_landmarks_list[0]['bottom_lip'][4] --points[58] face_landmarks_list[0]['bottom_lip'][5] --points[59] face_landmarks_list[0]['bottom_lip'][6] --points[48] face_landmarks_list[0]['bottom_lip'][7] --points[60] face_landmarks_list[0]['bottom_lip'][8] --points[67] face_landmarks_list[0]['bottom_lip'][9] --points[66] face_landmarks_list[0]['bottom_lip'][10] --points[65] face_landmarks_list[0]['bottom_lip'][11] --points[64]
上圖是采用奧巴馬一張照片識別五官后 上嘴唇以及下嘴唇 對應點以及對應位置的標注,左眼1-6是返回列表1-6的數值對應的像素位置。
識別圖片人物
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!")
看這個案例 案例:是奧巴馬還是拜登?
調整人臉識別的容錯率和敏感度
如果一張臉識別出不止一個結果,那么這意味着他和其他人長的太像了(Face_Recognition對於小孩和亞洲人的人臉識別准確率有待提升)。你可以把容錯率調低一些,使識別結果更加嚴格。
通過傳入參數 tolerance 來實現這個功能,默認的容錯率是0.6,容錯率越低,識別越嚴格准確。
如果想要更准確的識別,可以在計算編碼時設定要重新采樣的次數,face_encodings傳入 num_jitters 來實現,默認0,范圍為0-100,越高越准確,但速度越慢,(100就會慢100倍)
import face_recognition picture_of_me = face_recognition.load_image_file("me.jpg") my_face_encoding = face_recognition.face_encodings(picture_of_me,num_jitters=100)[0] unknown_picture = face_recognition.load_image_file("unknown.jpg") unknown_face_encoding = face_recognition.face_encodings(unknown_picture,num_jitters=100)[0]
# 由於對亞洲人識別率不高tolerance一般設置在0.3-0.38之間可滿足大部分需求
results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding,tolerance=0.38)
Python 案例
人臉定位
人臉關鍵點識別
人臉識別
參考: https://github.com/ageitgey/face_recognition#face-recognition