基於python的dlib庫的人臉識別

首先通過pip安裝cmake,只有安裝了cmake才能裝上dlib庫,建議裝dlib的時候關閉後臺的360殺毒軟件。
源代碼如下:

import dlib
import cv2 as cv

image_path = "xiaomin.jpg"

detector = dlib.get_frontal_face_detector()
image = dlib.load_rgb_image(image_path)
detection = detector(image, 1)

for i, dect in enumerate(detection):
    cv.rectangle(image, tuple([dect.left(), dect.top()]), tuple([dect.right(), dect.bottom()]), (237, 98, 205), 3)

cv.imshow("Park Hyo Min", image[:, :, ::-1])
cv.waitKey(0)
cv.destroyAllWindows()

這裏通過dlib.get_frontal_face_detector()生成一個人臉檢測器,在dlib.load_rgb_image()函數中傳入要檢測的圖像路徑,在生成好的人臉檢測器detector中傳入圖像,以及採樣次數,對於多個人臉識別採樣次數可以相對增大,但也不是越大越好。
在這裏插入圖片描述
程序裏的detection是<class 'dlib.rectangles'>類類型,detection中存放了檢測到的所有人臉矩形框的像素位置,我們可以通過enumerate()方法返回每個人臉矩形框的索引以及像素位置left,top,right,bottom,其相對人臉矩形框的位置信息如下:
在這裏插入圖片描述
下面是對多個人臉的檢測:

import dlib
import cv2 as cv

image_path = "people.jpg"

detector = dlib.get_frontal_face_detector()
image = dlib.load_rgb_image(image_path)
detection = detector(image, 1)

for i, dect in enumerate(detection):
    cv.rectangle(image, tuple([dect.left(), dect.top()]), tuple([dect.right(), dect.bottom()]), (0, 255, 0), 3)

cv.imshow("people", image[:, :, ::-1])
cv.waitKey(0)
cv.destroyAllWindows()

在這裏插入圖片描述
當採樣次數爲1時顯然有很多人臉檢測不出來,設置採樣次數爲3得到的結果更好一些,但是戴口罩的人臉以及部分被遮擋住的人臉仍然不能識別,需要用到深度學習的方法。
在這裏插入圖片描述
除此之外,可以利用shape_predictor_68_face_landmarks.dat官網訓練好的臉部的68個特徵來檢測人臉:

import dlib
import cv2 as cv

predictor_path = "shape_predictor_68_face_landmarks.dat"
image_path = "xiaomin.jpg"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

image = dlib.load_rgb_image(image_path)
detection = detector(image, 1)

for i, dect in enumerate(detection):
    face_mark = [(p.x, p.y) for p in predictor(image, dect).parts()]
    for index, point in enumerate(face_mark):
        cv.circle(image, point, 3, color=(210, 85, 232))
        font = cv.FONT_HERSHEY_COMPLEX_SMALL
        cv.putText(image, str(index), point, font, 0.5, color=(0, 255, 0), thickness=1, lineType=cv.LINE_AA)

cv.imshow("xiaomin", image[:, :, ::-1])
cv.waitKey(0)
cv.destroyAllWindows()

在這裏插入圖片描述
在圖像中檢測出人臉的68個特徵,並進行了標記。predictor(image, dect).parts()方法在圖像的每個人臉矩形框中捕獲68個人臉特徵,返回的是<class 'dlib.points'>類類型,返回值存放的是每個人臉矩形框中的68個特徵的像素位置即x和y座標值。

利用dlib官網訓練好的resnet分類網絡對未知人臉進行識別:
用來訓練的圖像:
在這裏插入圖片描述
用來進行測試的圖像:
在這裏插入圖片描述

import dlib
import os
import glob
import numpy as np

predictor_path = "shape_predictor_68_face_landmarks.dat"
recognition_path = "dlib_face_recognition_resnet_model_v1.dat"
train_image_path = "E://python//train"
test_image_path = "E://python//test"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
face_rec = dlib.face_recognition_model_v1(recognition_path)

train_image_names = ["樸孝敏", "樸孝敏", "樸孝敏", "林允兒", "宋智孝", "樸智妍", "樸智妍"]
descriptors = []
for path in glob.glob(os.path.join(train_image_path, "*.jpg")):
    image = dlib.load_rgb_image(path)
    detection_train = detector(image, 1)
    for i, det in enumerate(detection_train):
        face_mark_train = predictor(image, det)
        face_des_train = face_rec.compute_face_descriptor(image, face_mark_train)
        descriptors.append(np.array(face_des_train))

for path in glob.glob(os.path.join(test_image_path, "*.jpg")):
    dist = []
    image = dlib.load_rgb_image(path)
    detection_test = detector(image, 1)
    for i, det in enumerate(detection_test):
        face_mark_test = predictor(image, det)
        face_des_test = face_rec.compute_face_descriptor(image, face_mark_test)
        face_des_array = np.array(face_des_test)
    for v in descriptors:
        dist.append(np.sqrt(np.sum(np.square(face_des_array-v))))
    test_result = np.argsort(dist)
    if train_image_names[test_result[0]] in train_image_names:
        print("人臉識別的結果是:", train_image_names[test_result[0]])
    else:
        pass

測試結果:

人臉識別的結果是: 樸孝敏
人臉識別的結果是: 樸智妍
人臉識別的結果是: 林允兒
人臉識別的結果是: 樸孝敏
人臉識別的結果是: 宋智孝

測試結果中只有倒數第二張圖像的人臉識別的結果錯誤。 關於Resnet殘差網絡可以看一下這篇博客深度學習筆記(七)–ResNet(殘差網絡)

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