1. 程式人生 > >python dlib學習(五):比對人臉

python dlib學習(五):比對人臉

前言

在前面的部落格中介紹了,如何使用dlib標定人臉(python dlib學習(一):人臉檢測),提取68個特徵點(python dlib學習(二):人臉特徵點標定)。這次要在這兩個工作的基礎之上,將人臉的資訊提取成一個128維的向量空間。在這個向量空間上,同一個人臉的更接近,不同人臉的距離更遠。度量採用歐式距離,歐氏距離計算不算複雜。
二維情況下:

distance=(x1x2)2+(y1y2)2
三維情況下:
distance=(x1x2)2+(y1y2)2+(z1z2)2

將其擴充套件到128維的情況下即可。
通常使用的判別閾值是0.6,即如果兩個人臉的向量空間的歐式距離超過了0.6,即認定不是同一個人;如果歐氏距離小於0.6,則認為是同一個人。這個距離也可以由自己定,只要效果能更好。

實驗中使用了兩個模型:

資料夾目錄:
這裡寫圖片描述
兩個模型放在model資料夾中,測試圖片放在faces中,圖片自己隨便下幾張就行。

程式1

不說廢話了,直接上程式碼。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob

current_path = os.getcwd()  # 獲取當前路徑
# 模型路徑 predictor_path = current_path + "\\model\\shape_predictor_68_face_landmarks.dat" face_rec_model_path = current_path + "\\model\\dlib_face_recognition_resnet_model_v1.dat" #測試圖片路徑 faces_folder_path = current_path + "\\faces\\" # 讀入模型 detector = dlib.get_frontal_face_detector() shape_predictor = dlib.shape_predictor(predictor_path) face_rec_model = dlib.face_recognition_model_v1(face_rec_model_path) for
img_path in glob.glob(os.path.join(faces_folder_path, "*.jpg")): print("Processing file: {}".format(img_path)) # opencv 讀取圖片,並顯示 img = cv2.imread(img_path, cv2.IMREAD_COLOR) # opencv的bgr格式圖片轉換成rgb格式 b, g, r = cv2.split(img) img2 = cv2.merge([r, g, b]) dets = detector(img, 1) # 人臉標定 print("Number of faces detected: {}".format(len(dets))) for index, face in enumerate(dets): print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom())) shape = shape_predictor(img2, face) # 提取68個特徵點 for i, pt in enumerate(shape.parts()): #print('Part {}: {}'.format(i, pt)) pt_pos = (pt.x, pt.y) cv2.circle(img, pt_pos, 2, (255, 0, 0), 1) #print(type(pt)) #print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1))) cv2.namedWindow(img_path+str(index), cv2.WINDOW_AUTOSIZE) cv2.imshow(img_path+str(index), img) face_descriptor = face_rec_model.compute_face_descriptor(img2, shape) # 計算人臉的128維的向量 print(face_descriptor) k = cv2.waitKey(0) cv2.destroyAllWindows()

程式1結果

這裡寫圖片描述
部分列印結果:

F:\Python\my_dlib_codes\face_recognition>python my_face_recogniton.py
Processing file: F:\Python\my_dlib_codes\face_recognition\faces\jobs.jpg
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
-0.179784
0.15487
0.10509
-0.0973604
-0.19153
0.000418252
-0.0357536
-0.0206766
0.129741
-0.0628359
....

後面的那一堆數字就是人臉在128維向量空間上的值。

程式2

前面只是測試了一下,把要用的值給求到了。這裡我封裝了一下,把比對功能實現了。沒加多少東西,所以不做贅述了。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob
import numpy as np

def comparePersonData(data1, data2):
    diff = 0
    # for v1, v2 in data1, data2:
        # diff += (v1 - v2)**2
    for i in xrange(len(data1)):
        diff += (data1[i] - data2[i])**2
    diff = np.sqrt(diff)
    print diff
    if(diff < 0.6):
        print "It's the same person"
    else:
        print "It's not the same person"

def savePersonData(face_rec_class, face_descriptor):
    if face_rec_class.name == None or face_descriptor == None:
        return
    filePath = face_rec_class.dataPath + face_rec_class.name + '.npy'
    vectors = np.array([])
    for i, num in enumerate(face_descriptor):
        vectors = np.append(vectors, num)
        # print(num)
    print('Saving files to :'+filePath)
    np.save(filePath, vectors)
    return vectors

def loadPersonData(face_rec_class, personName):
    if personName == None:
        return
    filePath = face_rec_class.dataPath + personName + '.npy'
    vectors = np.load(filePath)
    print(vectors)
    return vectors

class face_recognition(object):
    def __init__(self):
        self.current_path = os.getcwd() # 獲取當前路徑
        self.predictor_path = self.current_path + "\\model\\shape_predictor_68_face_landmarks.dat"
        self.face_rec_model_path = self.current_path + "\\model\\dlib_face_recognition_resnet_model_v1.dat"
        self.faces_folder_path = self.current_path + "\\faces\\"
        self.dataPath = self.current_path + "\\data\\"
        self.detector = dlib.get_frontal_face_detector()
        self.shape_predictor = dlib.shape_predictor(self.predictor_path)
        self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)

        self.name = None
        self.img_bgr = None
        self.img_rgb = None
        self.detector = dlib.get_frontal_face_detector()
        self.shape_predictor = dlib.shape_predictor(self.predictor_path)
        self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)

    def inputPerson(self, name='people', img_path=None):
        if img_path == None:
            print('No file!\n')
            return 

        # img_name += self.faces_folder_path + img_name
        self.name = name
        self.img_bgr = cv2.imread(self.current_path+img_path)
        # opencv的bgr格式圖片轉換成rgb格式
        b, g, r = cv2.split(self.img_bgr)
        self.img_rgb = cv2.merge([r, g, b])

    def create128DVectorSpace(self):
        dets = self.detector(self.img_rgb, 1)
        print("Number of faces detected: {}".format(len(dets)))
        for index, face in enumerate(dets):
            print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))

            shape = self.shape_predictor(self.img_rgb, face)
            face_descriptor = self.face_rec_model.compute_face_descriptor(self.img_rgb, shape)
            # print(face_descriptor)
            # for i, num in enumerate(face_descriptor):
            #   print(num)
            #   print(type(num))

            return face_descriptor




程式2結果

測試程式碼1:

import face_rec as fc
face_rec = fc.face_recognition()   # 建立物件
face_rec.inputPerson(name='jobs', img_path='\\faces\\jobs.jpg')  # name中寫第一個人名字,img_name為圖片名字,注意要放在faces資料夾中
vector = face_rec.create128DVectorSpace()  # 提取128維向量,是dlib.vector類的物件
person_data1 = fc.savePersonData(face_rec, vector )   # 將提取出的資料儲存到data資料夾,為便於操作返回numpy陣列,內容還是一樣的

# 匯入第二張圖片,並提取特徵向量
face_rec.inputPerson(name='jobs2', img_path='\\faces\\jobs2.jpg')
vector = face_rec.create128DVectorSpace()  # 提取128維向量,是dlib.vector類的物件
person_data2 = fc.savePersonData(face_rec, vector )

# 計算歐式距離,判斷是否是同一個人
fc.comparePersonData(person_data1, person_data2)

如果data資料夾中已經有了模型檔案,可以直接匯入:

import face_rec as fc
face_rec = fc.face_recognition()   # 建立物件
person_data1 = fc.loadPersonData(face_rec , 'jobs')   # 建立一個類儲存相關資訊,後面還要跟上人名,程式會在data檔案中查詢對應npy檔案,比如這裡就是'jobs.npy'
person_data2 = fc.loadPersonData(face_rec , 'jobs2')  # 匯入第二張圖片
fc.comparePersonData(person_data1, person_data2) # 計算歐式距離,判斷是否是同一個人

程式2結果

Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import face_rec as fc
>>> face_rec = fc.face_recognition()
>>> face_rec.inputPerson(name='jobs', img_path='\\faces\\jobs.jpg')
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
>>> person_data1 = fc.savePersonData(face_rec, vector )
Saving files to :F:\Python\my_dlib_codes\face_recognition\data\jobs.npy
>>> face_rec.inputPerson(name='jobs2', img_path='\\faces\\jobs2.jpg')
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 124; top 39; right 253; bottom 168
>>> person_data2 = fc.savePersonData(face_rec, vector )
Saving files to :F:\Python\my_dlib_codes\face_recognition\data\jobs2.npy
>>> fc.comparePersonData(person_data1, person_data2)
0.490491048429
It's the same person

官方例程

#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#   This example shows how to use dlib's face recognition tool.  This tool maps
#   an image of a human face to a 128 dimensional vector space where images of
#   the same person are near to each other and images from different people are
#   far apart.  Therefore, you can perform face recognition by mapping faces to
#   the 128D space and then checking if their Euclidean distance is small
#   enough. 
#
#   When using a distance threshold of 0.6, the dlib model obtains an accuracy
#   of 99.38% on the standard LFW face recognition benchmark, which is
#   comparable to other state-of-the-art methods for face recognition as of
#   February 2017. This accuracy means that, when presented with a pair of face
#   images, the tool will correctly identify if the pair belongs to the same
#   person or is from different people 99.38% of the time.
#
#   Finally, for an in-depth discussion of how dlib's tool works you should
#   refer to the C++ example program dnn_face_recognition_ex.cpp and the
#   attendant documentation referenced therein.
#
#
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
#   You can install dlib using the command:
#       pip install dlib
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#   or
#       python setup.py install --yes USE_AVX_INSTRUCTIONS
#   if you have a CPU that supports AVX instructions, since this makes some
#   things run faster.  This code will also use CUDA if you have CUDA and cuDNN
#   installed.
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake and boost-python installed.  On Ubuntu, this can be done easily by
#   running the command:
#       sudo apt-get install libboost-python-dev cmake
#
#   Also note that this example requires scikit-image which can be installed
#   via the command:
#       pip install scikit-image
#   Or downloaded from http://scikit-image.org/download.html. 

import sys
import os
import dlib
import glob
from skimage import io

if len(sys.argv) != 4:
    print(
        "Call this program like this:\n"
        "   ./face_recognition.py shape_predictor_68_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces\n"
        "You can download a trained facial shape predictor and recognition model from:\n"
        "    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2\n"
        "    http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2")
    exit()

predictor_path = sys.argv[1]
face_rec_model_path = sys.argv[2]
faces_folder_path = sys.argv[3]

# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face, and finally the
# face recognition model.
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)

win = dlib.image_window()

# Now process all the images
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    print("Processing file: {}".format(f))
    img = io.imread(f)

    win.clear_overlay()
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))

    # Now process each face we found.
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = sp(img, d)
        # Draw the face landmarks on the screen so we can see what face is currently being processed.
        win.clear_overlay()
        win.add_overlay(d)
        win.add_overlay(shape)

        # Compute the 128D vector that describes the face in img identified by
        # shape.  In general, if two face descriptor vectors have a Euclidean
        # distance between them less than 0.6 then they are from the same
        # person, otherwise they are from different people. Here we just print
        # the vector to the screen.
        face_descriptor = facerec.compute_face_descriptor(img, shape)
        print(face_descriptor)
        # It should also be noted that you can also call this function like this:
        #  face_descriptor = facerec.compute_face_descriptor(img, shape, 100)
        # The version of the call without the 100 gets 99.13% accuracy on LFW
        # while the version with 100 gets 99.38%.  However, the 100 makes the
        # call 100x slower to execute, so choose whatever version you like.  To
        # explain a little, the 3rd argument tells the code how many times to
        # jitter/resample the image.  When you set it to 100 it executes the
        # face descriptor extraction 100 times on slightly modified versions of
        # the face and returns the average result.  You could also pick a more
        # middle value, such as 10, which is only 10x slower but still gets an
        # LFW accuracy of 99.3%.


        dlib.hit_enter_to_continue()

吐槽:
dlib的確很方便,不用花多少時間就能自己做到一些目標功能。官方文件講的很詳細,很容易入門。看這個文件(dlib python api)差不多就能學會用了。導師已經安排了研究生階段的學習任務了,後面也要忙起來了。dlib的學習雖然是我10月份才開的坑,為了善始善終我也要儘快整理完這些東西。以後要回到”泡館”生活了。
ヽ(・ω・。)ノ