def AlignTwoFaceWithRandomPoints(FirstFace_verts, SecondFace_verts, numberof_points=7): """ 将第二个人脸对齐到第一个人脸, 通过计算旋转矩阵, 平移矩阵等等 注意第二个像第一个人脸对齐 即要对齐的人脸是第二个参数 :param FirstFace: 第一个人脸顶点数据 n x 3 :param SecondFace: 第二个人脸顶点数据 第一个人脸和第二个人最好定数一样 n x 3 :param numberof_points:选用多少点进行刚体变换 :return: 返回第二个人脸对齐后的数据 """ Number_points = len(FirstFace_verts) select_index = np.random.randint(low=0, high=Number_points, size=numberof_points) Face1 = np.array(FirstFace_verts) Face2 = np.array(SecondFace_verts) Face1_select = Face1[select_index, :].T Face2_selcet = Face2[select_index, :].T s, R, t, _ = align_sim3(Face1_select, Face2_selcet) # print("align error is {}".format(error)) model_aligned = s * R.dot(Face2.T) + t alignment_error = model_aligned - Face1.T t_error = np.sqrt(np.sum(np.multiply(alignment_error, alignment_error), 0)) print("model aligned error is {}".format(np.mean(t_error))) return model_aligned.T.tolist()
def AlignTwoFaceWithFixedPoints(FirstFace_verts, SecondFace_verts, pointsindex, non_linear_align=False, return_sRt=False): """ 非线性对齐要求对齐点数大于8 将第二个人脸对齐到第一个人脸, 通过计算旋转矩阵, 平移矩阵等等 注意第二个像第一个人脸对齐 即要对齐的人脸是第二个参数 :param FirstFace: 第一个人脸顶点数据 n x 3 :param SecondFace: 第二个人脸顶点数据 第一个人脸和第二个人最好点数一样 n x 3 :param pointsindex: 需要對齊所使用的點序 一維list :return: 返回第二个人脸对齐后的数据 """ if non_linear_align: select_index = np.array(pointsindex) Face1 = np.array(FirstFace_verts) Face2 = np.array(SecondFace_verts) Face1_select = Face1[select_index, :] Face2_selcet = Face2[select_index, :] R, t, s, res = caculate_transform(Face1_select, Face2_selcet) t = t[:, np.newaxis] model_aligned = s * R.dot(Face2.T) + t alignment_error = model_aligned - Face1.T t_error = np.sqrt(np.sum(np.multiply(alignment_error, alignment_error), 0)) res = np.mean(t_error) print("model aligned error is {}".format(res)) model_aligned = model_aligned.T else: select_index = np.array(pointsindex) Face1 = np.array(FirstFace_verts) Face2 = np.array(SecondFace_verts) Face1_select = Face1[select_index, :].T Face2_selcet = Face2[select_index, :].T # s, R, t, _ = align_sim3(Face1_select, Face2_selcet) # demo from web s, R, t, _ = align_sim3(Face1_select, Face2_selcet) # print("align error is {}".format(error)) model_aligned = s * R.dot(Face2.T) + t # print("R is {}".format(R)) # print("t is {}".format(t)) alignment_error = model_aligned - Face1.T t_error = np.sqrt(np.sum(np.multiply(alignment_error, alignment_error), 0)) res = np.mean(t_error) print("model aligned error is {}".format(res)) model_aligned = model_aligned.T if return_sRt: return model_aligned.tolist(), s, R, t, res else: return model_aligned.tolist()
Face2_selcet = Face2[select_index, :] R, t, s, res = caculate_transform(Face1_select, Face2_selcet) t = t[:, np.newaxis] model_aligned = s * R.dot(Face2.T) + t alignment_error = model_aligned - Face1.T t_error = np.sqrt(np.sum(np.multiply(alignment_error, alignment_error), 0)) res = np.mean(t_error) print("model aligned error is {}".format(res)) model_aligned = model_aligned.T else: select_index = np.array(pointsindex) Face1 = np.array(FirstFace_verts) Face2 = np.array(SecondFace_verts) Face1_select = Face1[select_index, :].T Face2_selcet = Face2[select_index, :].T s, R, t, _ = align_sim3(Face1_select, Face2_selcet) # print("align error is {}".format(error)) model_aligned = s * R.dot(Face2.T) + t alignment_error = model_aligned - Face1.T t_error = np.sqrt(np.sum(np.multiply(alignment_error, alignment_error), 0)) print("model aligned error is {}".format(np.mean(t_error))) model_aligned = model_aligned.T if return_sRt: return model_aligned.tolist(), s, R, t else: return model_aligned.tolist() def BatchAlignFacewithRandomPoints(FaceToalignPath, FaceAligned_verts, SavePath): """ 批量稳定人脸