Python dlib 之 實作簡單的人臉中識别特定臉的 簡單 人臉識别
目錄
Python dlib 之 實作簡單的人臉中識别特定臉的 簡單 人臉識别
一、簡單介紹
二、環境搭建
三、注意事項
四、結果預覽
五、實作步驟
六、關鍵代碼
一、簡單介紹
Python是一種跨平台的計算機程式設計語言。是一種面向對象的動态類型語言,最初被設計用于編寫自動化腳本(shell),随着版本的不斷更新和語言新功能的添加,越多被用于獨立的、大型項目的開發。Python是一種解釋型腳本語言,可以應用于以下領域: Web 和 Internet開發、科學計算和統計、人工智能、教育、桌面界面開發、軟體開發、後端開發、網絡爬蟲。
本節介紹,通過使用 dlib 進行簡單的臉群中尋找特定臉的簡單demo示範。
Dlib是一個包含機器學習算法的C++開源工具包。Dlib可以幫助您建立很多複雜的機器學習方面的軟體來幫助解決實際問題。目前Dlib已經被廣泛的用在行業和學術領域,包括機器人,嵌入式裝置,行動電話和大型高性能計算環境。
二、環境搭建
1、安裝 dlib :pip install dlib
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL9EERPlXRq5keRRVT3V1MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL4ETN0EjNykDM5ADNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、安裝 scikit-image:pip install scikit-image
3、訓練好的人臉關鍵點檢測器(shape_predictor_68_face_landmarks.dat),以及訓練好的ResNet人臉識别模型(dlib_face_recognition_resnet_model_v1.dat)下載下傳,并解壓從壓縮包中拖出來
下載下傳網址:http://dlib.net/files/
4、準備好臉群資料庫,和要識别的目标臉
三、注意事項
1、如果dlib 庫 pip 下載下傳安裝較慢,可以 網址 http://dlib.net/files/ 下載下傳安裝包下載下傳
2、訓練好的人臉關鍵點檢測器(shape_predictor_68_face_landmarks.dat),以及訓練好的ResNet人臉識别模型(dlib_face_recognition_resnet_model_v1.dat)解壓可能會報損壞,可以打開壓縮包,用滑鼠拖出來即可
四、結果預覽
五、實作步驟
1、根據以上開始編寫代碼,打開 Pycharm,建立工程,如下圖
2、導入訓練好的資料,以及人臉資料
3、建立腳本,編寫代碼
4、編寫好代碼,編譯正确,運作腳本即可
5、結果如上面圖示
六、關鍵代碼
import os,dlib,glob,numpy
from skimage import io
def faceNorm(descriptors, detector,sp_face,facerec_model, need_recognise_face_path):
"""
計算目标臉與所有人群臉的歐式距離
:param descriptors:
:param detector:
:param sp_face:
:param facerec_model:
:param need_recognise_face_path:
:return: 傳回目标臉與所有人群臉的歐式距離清單
"""
# 對需識别人臉進行同樣處理
# 提取描述子,不再注釋
img = io.imread(need_recognise_face_path)
dets = detector(img, 1)
dist = []
for k, d in enumerate(dets):
shape = sp_face(img, d)
face_descriptor = facerec_model.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)
# 計算歐式距離
for i in descriptors:
dist_ = numpy.linalg.norm(i - d_test)
dist.append(dist_)
return dist
def faceModel(detector,sp_face,facerec_model,faces_folder_path):
"""
建構人群臉描述庫資料
:param detector:
:param sp_face:
:param facerec_model:
:param faces_folder_path:
:return: 人群臉描述庫清單
"""
descriptors = []
# 對檔案夾下的每一個人臉進行:
# 1.人臉檢測
# 2.關鍵點檢測
# 3.描述子提取
print(len(glob.glob(os.path.join(faces_folder_path, "*.jpg"))))
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
# 候選人臉描述子list
print("Processing file: {}".format(f))
img = io.imread(f)
# 1.人臉檢測
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
# 2.關鍵點檢測
shape = sp_face(img, d)
# 3.描述子提取,128D向量
face_descriptor = facerec_model.compute_face_descriptor(img, shape)
# 轉換為numpy array
v = numpy.array(face_descriptor)
descriptors.append(v)
return descriptors
def main():
#人臉關鍵點檢測器路徑
shape_predictor_face_path = "./Facedat/shape_predictor_68_face_landmarks.dat"
#人臉識别模型路徑
face_recognition_resnet_model_path = "./Facedat/dlib_face_recognition_resnet_model_v1.dat"
# 3.人臉庫路徑
faces_folder_path = "FaceImages"
# 4.需識别的人臉路徑
need_recognise_face_path = "./NeedRecogniseFace/Szly.jpg"
#need_recognise_face_path = "./FaceImages/zly.jpg"
# 1.加載正臉檢測器
detector = dlib.get_frontal_face_detector()
# 2.加載人臉關鍵點檢測器
sp_face = dlib.shape_predictor(shape_predictor_face_path)
# 3. 加載人臉識别模型
facerec_model = dlib.face_recognition_model_v1(face_recognition_resnet_model_path)
# 人臉群的資料描述子list
descriptors = faceModel(detector,sp_face,facerec_model,faces_folder_path)
dist = faceNorm( descriptors, detector,sp_face, facerec_model,need_recognise_face_path)
# 人臉群的對應人名清單
candidate = ['dlrb', 'man1', 'man2', 'woman1', 'woman2', 'zly']
# 人群和距離組成一個dict
c_d = dict(zip(candidate, dist))
# 排序,取舉例最近的作為是目标人(這個reverse=false,預設從小到大)
cd_sorted = sorted(c_d.items(), key=lambda d: d[1])
print('\n The person is: %s' % (cd_sorted[0][0]))
if __name__=="__main__":
main()