1. 程式人生 > 其它 >人臉識別庫 face_recognition

人臉識別庫 face_recognition

face_recognition

Windows系統環境下安裝

預設環境:anaconda的python3.7版本,win10環境

第一步:安裝dlib

  1. 從網路上下載:
http://dlib.net/ 或者 https://github.com/davisking/dlib
  1. 下載完成後解壓

  2. 編譯:開啟終端,是使用anaconda的python環境定位到dlib資料夾,使用 pyhton setup.py install 安裝檔案。

  3. 在安裝過程遇到的問題:

    1. 找不到cmake的問題

      官網下載安裝包:[https://cmake.org/download/]

      下載完成之後,解壓縮,然後將cmake的bin資料夾新增在系統環境路徑中。

  4. 從新使用 pyhton setup.py install 安裝檔案。

第二步:安裝face_recognition

使用 pip install face_recognition 安裝即可。

用法

  1. 匯入face_recognition
import face_recognition
  1. 載入影象

影象載入到一個 numpy 陣列中

image = face_recognition.load_image_file("your_file.jpg")
  1. 對圖片進行操作

batch_face_locations

使用 cnn 人臉檢測器返回影象中人臉邊界框的二維陣列 ,使用GPU批量處理影象

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

引數:

  • images:影象列表
  • number_of_times_to_upsample: 在影象上尋找人臉的次數 , 數字越大,查詢精細
  • batch_size: 每個 GPU 處理批次中包含多少影象

返回:

​ 以 css(上、右、下、左)順序找到的人臉位置的元組列表

compare_faces

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

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

引數:

  • known_face_encodings: 已知人臉編碼列表
  • face_encoding_to_check: 要與列表進行比較的單個人臉編碼
  • tolerance: 人臉之間的距離多少才算是匹配。越低越嚴格。0.6 是典型的最佳效能

返回:

​ True/False 值列表

face_distance

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

face_recognition.api.face_distance( face_encodings , face_to_compare )

引數:

​ face_encodings: 要比較的人臉編碼列表

​ face_to_compare: 要比較的人臉編碼

返回:

​ 一個 numpy ndarray,每個面的距離與 'faces' 陣列的順序相同

face_encodings

給定一個影象,返回影象中每個人臉的 128 維人臉編碼。

face_recognition.api.face_encodings( face_image , known_face_locations=None , num_jitters=1 , model='small' )

引數:

​ face_image: 包含一張或多張人臉的影象

​ known_face_locations: 可選 - 已經知道每個人臉的邊界框。

​ num_jitters: 計算編碼時對人臉重新取樣的次數。更高更準確,但更慢(即 100 慢 100 倍)

​ model: 可選 - 要使用的模型。“ large ”或“ small ”(預設)僅返回 5 個點,但速度更快。

返回:

​ 128 維人臉編碼列表(影象中的每個人臉編碼一個)

face_landmarks

給定一個影象,返回影象中每個人臉特徵位置(眼睛、鼻子等)的字典

face_recognition.api.face_landmarks( face_image , face_locations=None , model='large' )

引數:

​ face_image: 要搜尋的影象

​ face_locations: 可選擇提供要檢查的人臉位置列表。

​ model: 可選 - 要使用的模型。“large ”(預設)或“small”僅返回 5 個點但速度更快。

返回:

​ 面部特徵位置(眼睛、鼻子等)的字典列表

face_locations

返回影象中人臉的邊界框陣列

face_recognition.api.face_locations( img , number_of_times_to_upsample=1 , model='hog' )

引數:

​ img: 影象(作為 numpy 陣列)

​ number_of_times_to_upsample:在影象上尋找人臉的次數。較高的數字會發現較小的面孔。

​ model: 要使用的人臉檢測模型。“hog”在 CPU 上不太準確但速度更快。“cnn”是一種更準確的深度學習模型,它經過 GPU/CUDA 加速(如果可用)。 預設為“hog”

返回:

​ 以 css(上、右、下、左)順序找到的人臉位置的元組列表

load_image_file

將影象檔案(.jpg、.png 等)載入到 numpy 陣列中

face_recognition.api.load_image_file(file,model='RGB')

引數:

​ file: 要載入的影象檔名或檔案物件

​ model: 將影象轉換為的格式。僅支援“RGB”(8 位 RGB,3 通道)和“L”(黑白)。

返回:

​ 影象內容作為 numpy 陣列