python基于dlib的人脸定位与人脸比对实现

一.dlib安装

pip install dlib

网址:https://pypi.org/project/dlib/

下载:人脸定位文件:shape_predictor_68_face_landmarks.dat

人脸关键点识别文件:dlib_face_recognition_resnet_model_v1.dat

二.dlib人脸定位代码

三.dlib人脸关键点代码

import dlib

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"

# 读入模型

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)

def comparePersonData(data1, data2):

    diff = 0

    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"

img = cv2.imread(img_path, cv2.IMREAD_COLOR)dets = detector(img, 1)

# 人脸标定

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)

    face_descriptor = face_rec_model.compute_face_descriptor(img2, shape) # 计算人脸的128维的向量



你可能感兴趣的:(深度学习,python)