模塊 face_recognition 人臉識別


face_recognition 人臉識別

api 說明
1 load_image_file 將img文件加載到numpy 數組中
2 face_locations 查找圖像中所有面部和所有面部特征的位置
3 batch_face_locations 批次人臉定位函數(GPU)
4 face_landmarks 人臉特征提取函數
5 face_encodings 圖像編碼轉為特征向量
6 compare_faces 特征向量比對
7 face_distance 計算特征向量差值

圖像載入函數——load_image_file

​ load_image_file(file, mode='RGB')

   加載一個圖像文件到一個numpy array類型的對象上。

   參數:

   file:待加載的圖像文件名字

   mode:轉換圖像的格式。只支持“RGB”(8位RGB, 3通道)和“L”(黑白)

   返回值:

   一個包含圖像數據的numpy array類型的對象

人臉編碼函數——face_encodings

​ face_encodings(face_image, known_face_locations=None, num_jitters=1)

   給定一個圖像,返回圖像中每個人臉的128臉部編碼(特征向量)。

   參數:

   face_image:輸入的人臉圖像

   known_face_locations:可選參數,如果你知道每個人臉所在的邊界框

   num_jitters=1:在計算編碼時要重新采樣的次數。越高越准確,但速度越慢(100就會慢100倍)

   返回值:

   一個128維的臉部編碼列表

人臉匹配函數——compare_faces

​ compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

   比較臉部編碼列表和候選編碼,看看它們是否匹配,設置一個閾值,若兩張人臉特征向量的距離,在閾值范圍之內,則認為其 是同一個人

   參數:

   known_face_encodings:已知的人臉編碼列表

   face_encoding_to_check:待進行對比的單張人臉編碼數據

   tolerance=0.6:兩張臉之間有多少距離才算匹配。該值越小對比越嚴格,0.6是典型的最佳值

   返回值:

   一個 True或者False值的列表,該表指示了known_face_encodings列表的每個成員的匹配結果

人臉定位函數——face_locations

​ face_locations(face_image,number_of_times_to_upsample=1,model="hog")

   利用CNN深度學習模型或方向梯度直方圖(Histogram of Oriented Gradient, HOG)進行人臉提取。返回值是一個數組(top, right, bottom, left)表示人臉所在邊框的四條邊的位置。

   參數:

   face_image:輸入的人臉圖像

   number_of_times_to_upsample=1:從圖片的樣本中查找多少次人臉,該參數的值越高的話越能發現更小的人臉

   model="hog":使用哪種人臉檢測模型。“hog” 准確率不高,但是在CPU上運行更快,“cnn” 更准確更深度(且GPU/CUDA加速,如果有GPU支持的話),默認是“hog”

   返回值:

   一個元組列表,列表中的每個元組包含人臉的四邊位置(top, right, bottom, left)

批次人臉定位函數(GPU)——batch_face_locations

​ batch_face_locations(face_images,number_of_times_to_upsample=1,batch_size=128)

   使用CNN人臉檢測器返回一個包含人臉特征的二維數組,如果使用了GPU,這個函數能夠更快速的返回結果;如果不使用GPU的話,該函數就沒必要使用

   參數:

   face_images:輸入多張人臉圖像組成的list

   number_of_times_to_upsample=1:從圖片的樣本中查找多少次人臉,該參數的值越高的話越能發現更小的人臉

   batch_size=128:每個GPU一次批處理多少個image

   返回值:

   一個元組列表,列表中的每個元組包含人臉的四邊位置(top, right, bottom, left)

人臉特征提取函數——face_landmarks

​ face_landmarks(face_image,face_locations=None,model="large")

   給定一個圖像,提取圖像中每個人臉的臉部特征位置

   參數:

   face_image:輸入的人臉圖片

   face_locations=None:可選參數,默認值為None,代表默認解碼圖片中的每一個人臉。若輸入face_locations()[i]可指定人臉進行解碼

   model="large":輸出的特征模型,默認為“large”,可選“small”。當選擇為"small"時,只提取左眼、右眼、鼻尖這三種臉部特征。

   返回值:

   返回值類型為:List[Dict[str,List[Tuple[Any,Any]]]],是由各個臉部特征關鍵點位置組成的字典記錄列表,一個Dict對象對應圖片中的一個人臉,其key為某個臉部特征(如輸出中的nose_bridge、left_eye等),value是由該臉部特征各個關鍵點位置組成的List,關鍵點位置是一個Tuple(如上輸出中,nose_bridge對應的關鍵點位置組成的列表為[(881L, 128L), (880L, 141L), (880L, 154L), (879L, 167L)]  )

計算特征相識度差值——face_distance

例子1-人臉識別和特征匹配

import face_recognition

# 加載2張已知面孔的圖片
known_obama_image = face_recognition.load_image_file("obama.jpg")
known_biden_image = face_recognition.load_image_file("biden.jpg")

# 計算圖片對應的編碼
obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]

known_encodings = [
    obama_face_encoding,
    biden_face_encoding
]

# 加載一張未知面孔的圖片
image_to_test = face_recognition.load_image_file("obama2.jpg")
# 計算圖片對應的編碼
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]

# 計算未知圖片與已知的2個面孔的距離
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)

for i, face_distance in enumerate(face_distances):
    print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
    print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
    print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
    print()

結果

 The test image has a distance of 0.35 from known image #0 (與#0面孔差異為0.35,在相似度閾值分別為 0.6和0.5的情形下,都可以認為與#0是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? True
- With a very strict cutoff of 0.5, would the test image match the known image? True
The test image has a distance of 0.82 from known image #1(與#1面孔差異為0.82,在相似度閾值分別為 0.6和0.5的情形下,都不認為與#1是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? False
- With a very strict cutoff of 0.5, would the test image match the known image? False

例子2-特征點檢測和美顏

import face_recognition
from PIL import Image, ImageDraw

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("tt3.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)

for face_landmarks in 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, 'RGBA')

    # 畫個濃眉
    d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
    d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
    d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
    d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)

    # 塗個性感的嘴唇
    d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
    d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
    d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
    d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)

    # 閃亮的大眼睛
    d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
    d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))

    # 畫眼線
    d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
    d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)

    pil_image.show()


免責聲明!

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



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