脸型匹配

方法1,基于曲率,计算曲率组成的特征向量的余弦距离

def cos_sim(vector_a, vector_b):
    """
    计算两个向量之间的余弦相似度
    :param vector_a: 向量 a 
    :param vector_b: 向量 b
    :return: sim
    """
    vector_a = np.mat(vector_a)
    vector_b = np.mat(vector_b)
    num = float(vector_a * vector_b.T)
    denom = np.linalg.norm(vector_a) * np.linalg.norm(vector_b)
    cos = num / denom
    sim = 0.5 + 0.5 * cos
    return sim


def compute_curvature_cov(input_numpy):
    #曲率越大,越弯曲
    #       |y''|
    #k= -----------
    #         3/2
    #   (1+y'2)
    #y’(i) = (y(i+1)-y(i))/h
    #y’’(i) = (y(i+1)+y(i-1)-2*y(i))/h^2

    input_numpy_yijie = (input_numpy[1:,1]-input_numpy[:-1,1])/(input_numpy[1:,0]-input_numpy[:-1,0])
    input_numpy_yijie = input_numpy_yijie[:-1]
    input_numpy_erjie = (input_numpy[2:,1]+input_numpy[:-2,1]-2*input_numpy[1:-1,1])/((input_numpy[2:,0]-input_numpy[:-2,0])*(input_numpy[2:,0]-input_numpy[:-2,0]))
    K = np.abs(input_numpy_erjie) /np.power((np.ones_like(input_numpy_yijie)+input_numpy_yijie*input_numpy_yijie),3.0/2.0)

    return K

脸型匹配_第1张图片

方法2,基于人脸关键点坐标的L1距离,

脸型匹配_第2张图片

方法3,基于对比的两个人脸的关键点组成区域的交集占整个图片的面积,

def area_sim(keypoints_Xs, keypoints_Xt):
    keypoints_Xt = keypoints_Xt.reshape(-1,2)
    process_num = 17
    keypoints_Xs = keypoints_Xs[:process_num]
    keypoints_Xt = keypoints_Xt[:process_num]

    scale = 256
    mask = np.zeros((scale,scale),np.uint8)
    for i in range(process_num-1):
        triangle = np.array([[[keypoints_Xs[i,0],keypoints_Xs[i,1]], [keypoints_Xs[i+1,0],keypoints_Xs[i+1,1]], [keypoints_Xt[i,0],keypoints_Xt[i,1]]]], dtype = np.int32)
        cv2.fillPoly(mask, triangle, 1)
        triangle = np.array([[[keypoints_Xs[i,0],keypoints_Xs[i,1]], [keypoints_Xs[i+1,0],keypoints_Xs[i+1,1]], [keypoints_Xt[i+1,0],keypoints_Xt[i+1,1]]]], dtype = np.int32)
        cv2.fillPoly(mask, triangle, 1)
        triangle = np.array([[[keypoints_Xt[i,0],keypoints_Xt[i,1]], [keypoints_Xt[i+1,0],keypoints_Xt[i+1,1]], [keypoints_Xs[i,0],keypoints_Xs[i,1]] ]], dtype = np.int32)
        cv2.fillPoly(mask, triangle, 1)
        triangle = np.array([[[keypoints_Xt[i,0],keypoints_Xt[i,1]], [keypoints_Xt[i+1,0],keypoints_Xt[i+1,1]], [keypoints_Xs[i+1,0],keypoints_Xs[i+1,1]] ]], dtype = np.int32)
        cv2.fillPoly(mask, triangle, 1)

    dis = 1- np.sum(mask)/(scale*scale)
    return dis,mask

脸型匹配_第3张图片

方法4,对上面的3种方法进行融合,


def total_method(keypoints_Xs, keypoints_Xt):
    #(68, 2) (54,)
    #一共68个关键点,取前17个人脸边缘轮廓点使用
    keypoints_Xt = keypoints_Xt.reshape(-1,2)
    process_num = 17

    #method 1,曲率
    qulv_Xs = compute_curvature_cov(keypoints_Xs[:process_num])
    qulv_Xt = compute_curvature_cov(keypoints_Xt[:process_num])

    weights_matrix = np.ones_like(qulv_Xt, np.float32)
    #weights_matrix[3:12] =2.0
    dis1 = cos_sim(qulv_Xs*weights_matrix , qulv_Xt*weights_matrix)


    #method 2,坐标L1
    scale  =256
    keypoints_Xs = keypoints_Xs/scale
    keypoints_Xt = keypoints_Xt/scale

    dis2 = 1 - np.mean(np.abs(keypoints_Xs[:process_num] - keypoints_Xt[:process_num]))#cos_sim(keypoints_Xs[:process_num].reshape(-1,1).squeeze(), keypoints_Xt[:process_num].reshape(-1,1).squeeze())

    #method 3,面积
    dis3 ,mask= area_sim(keypoints_Xs*scale, keypoints_Xt*scale )

    dis = 0.1*dis1+ 0.1*dis2 +0.8*dis3



    return dis

脸型匹配_第4张图片

调用,


def compare(points_Xs, face_npy):
    #输入:68个关键点numpy,68个人脸数据库,注意所有人脸都需要首先进行对齐操作,然后归一化到256*256
    #输出:最相似人脸轮廓的索引,人脸轮廓的相似度值,人脸轮廓的差异mask list
    indexs = None
    dis_ls = list()
    mask_list = []
    for j in range(len(face_npy)):  # len(face_npy)
        distz, mask = total_method(points_Xs, face_npy[j])
        dis_ls.append(distz)
        mask_list.append(mask)
    dis_ls_arr = np.array(dis_ls)
    print(dis_ls)
    euc_indexs = np.argsort(-1*dis_ls_arr)
    return euc_indexs, dis_ls ,mask_list

 

效果,

融合>面积>L1距离>曲率

 

 

 

 

你可能感兴趣的:(人脸增强)