Python 使用 face_recognition 人臉識別


Python 使用 face_recognition 人臉識別


官方說明:https://face-recognition.readthedocs.io/en/latest/readme.html

人臉識別

  face_recognition 是世界上最簡單的人臉識別庫。

  使用 dlib 最先進的人臉識別功能構建建立深度學習,該模型准確率在99.38%。

Python模塊的使用

  Python可以安裝導入 face_recognition 模塊輕松操作,對於簡單的幾行代碼來講,再簡單不過了。

  Python操作 face_recognition API 文檔:https://face-recognition.readthedocs.io/en/latest/face_recognition.html

自動查找圖片中的所有面部

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

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!

自動定位圖像中人物的面部特征

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

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 用法

要在項目中使用面部識別,首先導入面部識別庫,沒有則安裝:

import face_recognition

基本思路是首先加載圖片:

# 導入人臉識別庫
import face_recognition

# 加載圖片
image = face_recognition.load_image_file("1.jpg")

上面這一步會將圖像加載到 numpy 數組中,如果已經有一個 numpy 數組圖像則可以跳過此步驟。

然后對圖片進行操作,例如找出面部、識別面部特征、查找面部編碼:

例如對此照片進行操作

  

# 導入人臉識別庫
import face_recognition

# 加載圖片
image = face_recognition.load_image_file("1.jpg")

# 查找面部
face_locations = face_recognition.face_locations(image)
# 查找面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
# 查找面部編碼
list_of_face_encodings = face_recognition.face_encodings(image)

# 打印輸出
print(face_locations)
print(face_landmarks_list)
print(list_of_face_encodings)
/usr/bin/python3.6 /home/wjw/PycharmProjects/face_study/face0112/find_face.py
[(297, 759, 759, 297)]
[{'chin': [(280, 439), (282, 493), (283, 547), (290, 603), (308, 654), (340, 698), (380, 733), (427, 760), (485, 770), (544, 766), (592, 738), (634, 704), (668, 661), (689, 613), (701, 563), (712, 514), (722, 466)], 'left_eyebrow': [(327, 373), (354, 340), (395, 323), (442, 324), (487, 337)], 'right_eyebrow': [(560, 344), (603, 340), (647, 348), (682, 372), (698, 410)], 'nose_bridge': [(519, 410), (517, 444), (515, 477), (513, 512)], 'nose_tip': [(461, 548), (485, 554), (508, 561), (532, 558), (555, 556)], 'left_eye': [(372, 424), (399, 420), (426, 420), (451, 429), (424, 433), (397, 432)], 'right_eye': [(577, 440), (605, 437), (631, 442), (655, 451), (628, 454), (601, 449)], 'top_lip': [(415, 617), (452, 600), (484, 593), (506, 600), (525, 598), (551, 610), (579, 634), (566, 630), (524, 620), (504, 619), (482, 616), (428, 616)], 'bottom_lip': [(579, 634), (546, 636), (518, 636), (498, 635), (475, 632), (447, 626), (415, 617), (428, 616), (479, 605), (500, 610), (520, 610), (566, 630)]}]
[array([-0.14088562,  0.00503807,  0.00270613, -0.07196694, -0.13449337,
       -0.07765003, -0.03745099, -0.09381913,  0.12006464, -0.14438102,
        0.13404925, -0.06327219, -0.17859964, -0.05488868, -0.02019649,
        0.1671212 , -0.1643257 , -0.12276072, -0.03441665, -0.05535197,
        0.10760178,  0.04479133, -0.06407147,  0.0689199 , -0.11934121,
       -0.32660219, -0.07756624, -0.06931646,  0.04064362, -0.05714978,
       -0.0353414 ,  0.0762421 , -0.18261658, -0.07098956,  0.02025999,
        0.13947421, -0.00086442, -0.05380288,  0.17013952,  0.03612047,
       -0.24374251,  0.02234841,  0.06126914,  0.25475574,  0.11198805,
        0.01954928,  0.01119124, -0.10833667,  0.14647615, -0.14495029,
       -0.00890255,  0.12340544,  0.05062022,  0.07525564,  0.0184714 ,
       -0.0970083 ,  0.07874238,  0.09881058, -0.15751837,  0.02846039,
        0.0963228 , -0.07531998, -0.0176545 , -0.07000162,  0.25344211,
        0.03867894, -0.09201257, -0.1658347 ,  0.12261658, -0.1535762 ,
       -0.15940444,  0.04406216, -0.12239387, -0.10966937, -0.30615237,
       -0.00739088,  0.39348996,  0.108335  , -0.20034787,  0.08009379,
       -0.05592394, -0.0375729 ,  0.23610245,  0.16506384,  0.03575533,
        0.04828007, -0.04044699,  0.01277492,  0.25646573, -0.00142263,
       -0.04078939,  0.18071812,  0.0617944 ,  0.12697747,  0.02988701,
       -0.00425877, -0.07669616,  0.00568433, -0.10959606, -0.03289849,
        0.08964096, -0.00859835,  0.00752143,  0.14310959, -0.14807181,
        0.18848835,  0.03889544,  0.0564449 ,  0.03094865,  0.05897319,
       -0.11886788, -0.03628988,  0.09417973, -0.20971358,  0.22439443,
        0.18054837,  0.0444049 ,  0.06860743,  0.1211487 ,  0.02242998,
       -0.01343671, -0.00214755, -0.24110457, -0.03643485,  0.13142672,
       -0.05264375,  0.09808614,  0.00694137])]

Process finished with exit code 0

可以將面部編碼相互比較以查看面部是否匹配,注意:查找面部編碼有點慢,因此如果在以后還需對次圖片進行面部分析參考,建議將每個圖片的結果存留緩存或存儲進數據庫。

一旦得到面部編碼,便可以比較他們

# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_face_encodings, a_single_unknown_face_encoding)

就是這么簡單!

face_recognition 模塊內容

batch_face_locations 使用CNN人臉檢測器返回圖像中人臉邊界框的二維數組

face_recognition.batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128)

使用CNN人臉檢測器返回圖像中人臉邊界框的二維數組

如果您使用的是GPU,這可以提供更快的結果,因為GPU

可以一次處理一批圖像。如果你不使用GPU,你就不需要這個功能。

:param img:圖像列表(每個圖像都是一個numpy數組)

:param number_of_times_to_upsample:要對圖像進行多少次upsample以查找面。數字越大,面越小。

:param batch_size:每個GPU處理批中要包含多少圖像。

return:按css(上、右、下、左)順序找到的面位置的元組列表

compare_faces 將人臉編碼列表與候選編碼進行比較,看它們是否匹配。

face_recognition.compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

將人臉編碼列表與候選編碼進行比較,看它們是否匹配。 

:param known_face_encodings:已知人臉編碼列表

:param face_encoding_to_check:要與列表進行比較的單個面編碼

:param tolerance:面與面之間的距離應視為匹配。越低越嚴格。0.6是典型的最佳性能。

return:一個true/false值列表,指示要檢查的已知面部編碼與面部編碼匹配。

face_distance 給定面部編碼列表,將它們與已知的面部編碼進行比較,並獲得每個比較面部的歐氏距離。距離告訴您臉部的相似程度。

face_recognition.face_distance(face_encodings, face_to_compare)

給定面部編碼列表,將它們與已知的面部編碼進行比較,並獲得每個比較面部的歐氏距離。距離告訴您臉部的相似程度。

:param faces:要比較的面編碼列表

:param face_to_compare:要與之比較的人臉編碼

:返回:一個numpy ndarray,每個面的距離與“faces”數組的順序相同。

face_encodings 給定的圖像,返回的128維編碼每個臉對臉的形象。

 face_recognition.face_encodings(face_image, known_face_locations=None, num_jitters=1)

給定的圖像,返回的128維編碼每個臉對臉的形象。

參數:face_image:圖像是包含一個或多個面

參數:known_face_locations:可選的如果您已經知道每個面的邊界框。

參數:num_jitters計算編碼時重新采樣面的次數。更高更准確,但更慢(即100慢100倍)

參數:return:128維面部編碼列表(圖像中每個面部一個)

face_landmarks 給定圖像,返回圖像中每個面部的面部特征位置(眼睛,鼻子等)的字典。

 face_recognition.face_landmarksface_imageface_locations = Nonemodel ='large' 

給定圖像,返回圖像中每個面部的面部特征位置(眼睛,鼻子等)的字典。

face_image - 要搜索的圖像

face_locations - 可選擇提供要檢查的面部位置列表。

model - 可選 - 要使用的模型。“大”(默認)或“小”,只返回5分但速度更快。

return:面部特征位置(眼睛,鼻子等)的序列表

face_locations 返回圖像中人臉邊界框的數組。

 face_recognition.face_locationsimgnumber_of_times_to_upsample = 1model ='hog'  

返回圖像中人臉邊界框的數組。

img - 一個圖像(作為一個numpy數組)

number_of_times_to_upsample - 對圖像進行上采樣以查找面部的次數。數字越大,面部越小。

model - 使用哪種人臉檢測模型。“hog”不太准確,但在CPU上更快。“cnn”是一種更准確的深度學習模型,它是GPU / CUDA加速(如果可用)。默認為“hog”。

return:css(頂部,右側,底部,左側)順序中找到的面部位置的元組列表

load_image_file 將圖像文件(.jpg,.png等)加載到numpy數組中。

 face_recognition.load_image_filefilemode ='RGB' )

將圖像文件(.jpg,.png等)加載到numpy數組中。

file - 要加載的圖像文件名或文件對象

mode - 將圖像轉換為的格式。僅支持“RGB”(8位RGB,3個通道)和“L”(黑色和白色)

return:圖像內容為numpy數組。

 

完成!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM