Python dlib 之 實現簡單的人臉中識別特定臉的 簡單 人臉識別

Python dlib 之 實現簡單的人臉中識別特定臉的 簡單 人臉識別

 

目錄

Python dlib 之 實現簡單的人臉中識別特定臉的 簡單 人臉識別

一、簡單介紹

二、環境搭建

三、注意事項

四、結果預覽

五、實現步驟

六、關鍵代碼


 

一、簡單介紹

Python是一種跨平臺的計算機程序設計語言。是一種面向對象的動態類型語言,最初被設計用於編寫自動化腳本(shell),隨着版本的不斷更新和語言新功能的添加,越多被用於獨立的、大型項目的開發。Python是一種解釋型腳本語言,可以應用於以下領域: Web 和 Internet開發、科學計算和統計、人工智能、教育、桌面界面開發、軟件開發、後端開發、網絡爬蟲。

本節介紹,通過使用 dlib 進行簡單的臉羣中尋找特定臉的簡單demo演示。

Dlib是一個包含機器學習算法的C++開源工具包。Dlib可以幫助您創建很多複雜的機器學習方面的軟件來幫助解決實際問題。目前Dlib已經被廣泛的用在行業和學術領域,包括機器人,嵌入式設備,移動電話和大型高性能計算環境。

 

二、環境搭建

1、安裝 dlib :pip install dlib

 

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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章