def Projection(trumpShapeModel, headMeanShape): '''ALIGN HEADSHAPE TO TRUMP''' transform = AlignmentAffine(headMeanShape, trumpShapeModel.model.mean()) normalized_shape = transform.apply(headMeanShape) '''HEAD P''' headWeights = trumpShapeModel.model.project(normalized_shape) return headWeights
def main(): path_to_neutral = Path( '/Users/lls/Documents/face/data/headpose/Angle/neutral0/') path_to_smile = Path( '/Users/lls/Documents/face/data/headpose/Angle/down30') path_to_source = Path('/Users/lls/Documents/face/data/trump/trump') # PDM shape neutral = PDMModel(path_to_neutral, 20)[0].model.mean() smile = PDMModel(path_to_smile, 20)[0].model.mean() source_shape, source_img = PDMModel(path_to_source, 20) p_smile = project(smile, source_shape) p_neutral = project(neutral, source_shape) delta = (p_smile - p_neutral) * 1.5 ptsPath = '/Users/lls/Documents/face/data/trump/trump/trump_13.pts' trumpShape = mio.import_landmark_file(ptsPath).lms p_i = project(trumpShape, source_shape) new_p_i = p_i + delta reconstructed_img_i = source_shape.model.instance(new_p_i) trans_reconstructed_img_i = AlignmentAffine(reconstructed_img_i, trumpShape) reconstructed_img_i_pc = trans_reconstructed_img_i.apply( reconstructed_img_i) plt.subplot(241) reconstructed_img_i_pc.view() plt.gca().set_title('reconstructed_img_i_pc') plt.subplot(242) trumpShape.view() plt.gca().set_title('trumpShape') plt.show() '''
def reconstructByPca(path_to_images): shape_model = pca(path_to_images) # Import shape shape = mio.import_builtin_asset.einstein_pts().lms # Find the affine transform that normalizes the shape # with respect to the mean shape transform = AlignmentAffine(shape, shape_model.model.mean()) # Normalize shape and project it normalized_shape = transform.apply(shape) weights = shape_model.model.project(normalized_shape) print("Weights: {}".format(weights)) # Reconstruct the normalized shape reconstructed_normalized_shape = shape_model.model.instance(weights) # Apply the pseudoinverse of the affine tansform reconstructed_shape = transform.pseudoinverse().apply( reconstructed_normalized_shape) # Visualize plt.subplot(121) shape.view(render_axes=False, axes_x_limits=0.05, axes_y_limits=0.05) plt.gca().set_title('Original shape') plt.subplot(122) reconstructed_shape.view(render_axes=False, axes_x_limits=0.05, axes_y_limits=0.05) plt.gca().set_title('Reconstructed shape')
def ProjectionAndReconstruction(trumpShapeModel, headMeanShape): '''ALIGN HEADSHAPE TO TRUMP''' transform = AlignmentAffine(headMeanShape, trumpShapeModel.model.mean()) normalized_shape = transform.apply(headMeanShape) '''HEAD P''' headWeights = trumpShapeModel.model.project(normalized_shape) '''RECONSTRUCTION''' reconstructed_normalized_shape = trumpShapeModel.model.instance( headWeights) reconstructed_shape = transform.pseudoinverse().apply( reconstructed_normalized_shape) return reconstructed_shape, headWeights
def Reconstruction(trumpShapeModel, deltaWeights, trumpShape): trumpWeights, transform = Projection(trumpShapeModel, trumpShape) trumpWeights = trumpWeights + deltaWeights # print trumpWeights reconstructed_normalized_shape = trumpShapeModel.model.instance( trumpWeights) # reconstructed_shape = transform.pseudoinverse().apply( # reconstructed_normalized_shape) trans_reconstructed_shape = AlignmentAffine(reconstructed_normalized_shape, trumpShape) reconstructed_img_shape = trans_reconstructed_shape.apply( reconstructed_normalized_shape) return reconstructed_img_shape
def project(target, source_model): # align the source and target face transform = AlignmentAffine(target, source_model.model.mean()) normalized_target = transform.apply(target) weights = source_model.model.project(normalized_target) return weights
def affine_enhance(path_to_images, save_dir=None, scales=[1], rotations=[0], translations=[[0, 0]], mean_shape=1): if save_dir is not None: mk_dir(save_dir, 0) # load training images train_images = [] for path_to_image in path_to_images: for img in print_progress( mio.import_images(path_to_image, verbose=True)): train_images.append(img) print 'sum of training data: %d' % len(train_images) # create pca model based on training set # shape_model = pca(path_train_images) shape_model = pca_image(train_images) excepted_num = len(scales) * len(rotations) * len(translations) * len( train_images) completed_num = 0 for train_img in train_images: if mean_shape: transform = AlignmentAffine(train_img.landmarks['PTS'], shape_model.model.mean()) [r1, s, r2, t] = transform.decompose() # transform = r2.compose_after(s.compose_after(r1)) transform = r2.compose_after(r1) rotation_shape = transform.apply(train_img.landmarks['PTS']) offset = train_img.landmarks['PTS'].centre( ) - rotation_shape.centre() t = compositions.Translation(offset, train_img.n_dims) transform = t.compose_after(r2.compose_after(r1)) normal_image = train_img.warp_to_shape(train_img.shape, transform.pseudoinverse(), warp_landmarks=True, order=1, mode='nearest', return_transform=False) else: normal_image = train_img for scale in scales: for rotation in rotations: for translation in translations: s = compositions.scale_about_centre( normal_image.landmarks['PTS'], scale) r = compositions.rotate_ccw_about_centre( normal_image, rotation) t = compositions.Translation(translation, normal_image.n_dims) transform = t.compose_after(s.compose_after(r)) # warp image new_image = normal_image.warp_to_shape( normal_image.shape, transform.pseudoinverse(), warp_landmarks=True, order=1, mode='nearest', return_transform=False) # plt.subplot(121) # normal_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('Original image') # plt.subplot(122) # new_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('Rescale image') # plt.close('all') # save enhanced image with lable img_suffix = new_image.path.suffix lb_suffix = '.pts' dataType = filter(lambda x: x in str(new_image.path), support_types)[0] new_image_name = '%s_' % dataType + new_image.path.name.split( '.')[0] + '_s%s_r%s_x%s_y%s' % ( str(scale), str(rotation), str( translation[0]), str(translation[1])) img_path = os.path.join(save_dir, new_image_name + img_suffix) lb_path = os.path.join(save_dir, new_image_name + lb_suffix) mio.export_image(new_image, img_path, overwrite=True) mio.export_landmark_file(new_image.landmarks['PTS'], lb_path, overwrite=True) # plt.subplot(121) # new_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('new image') # save_image = mio.import_image(img_path) # plt.subplot(122) # save_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('saved image') # plt.close('all') completed_num = completed_num + 1 print 'completed: %d/%d' % (completed_num, excepted_num)
def affine_test(path_to_images): # create pca model based on training set shape_model = pca(path_to_images) # create test image # testImage = mio.import_builtin_asset.takeo_ppm() testImage = mio.import_image( '/home/sean/workplace/221/py-R-FCN-test/data/DB/face/300-w_face/otherDB/aflw-full/testset/0_image00002_1.jpg' ) # Find the affine transform that normalizes the shape # with respect to the mean shape # shape = testImage.landmarks['PTS'] # transform = AlignmentAffine(shape, shape_model.model.mean()) # image change size to adapt the scale of kp to that of target_kp tmp_image = testImage.rescale_to_pointcloud(shape_model.model.mean(), group='PTS') transform = AlignmentAffine(testImage.landmarks['PTS'], tmp_image.landmarks['PTS']) new_shape = transform.apply( testImage.landmarks['PTS']) # equal to kp of tmp # warp image # new_image = testImage.warp_to_shape(tmp_image.shape, transform.pseudoinverse(), # warp_landmarks=True, order=1, # mode='nearest', # return_transform=False) # # plt.subplot(131) # testImage.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('Original image') # plt.subplot(132) # new_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('Rescale image') # plt.subplot(133) # tmp_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', # marker_size=4, render_axes=True) # plt.gca().set_title('Template image') # create a noise shape # noisy_shape = noisy_shape_from_shape(testImage.landmarks['PTS'], testImage.landmarks['PTS'], # noise_percentage=0.2, # allow_alignment_rotation=True) # transform = AlignmentAffine(testImage.landmarks['PTS'], noisy_shape) # similarity = AlignmentSimilarity(testImage.landmarks['PTS'], # testImage.landmarks['PTS'], # rotation=True) s = compositions.scale_about_centre(testImage.landmarks['PTS'], 1) r = compositions.rotate_ccw_about_centre(testImage, 90) t = compositions.Translation([0, 0], testImage.n_dims) # transform = similarity.compose_after(t.compose_after(s.compose_after(r))) transform = t.compose_after(s.compose_after(r)) # new_shape = transform.apply(testImage.landmarks['PTS']) # warp image new_image = testImage.warp_to_shape(testImage.shape, transform.pseudoinverse(), warp_landmarks=True, order=1, mode='nearest', return_transform=False) plt.subplot(121) testImage.view_landmarks(marker_face_colour='white', marker_edge_colour='black', marker_size=4, render_axes=True) plt.gca().set_title('Original image') plt.subplot(122) new_image.view_landmarks(marker_face_colour='white', marker_edge_colour='black', marker_size=4, render_axes=True) plt.gca().set_title('Rescale image') plt.close('all')