From ba08ae03d67f0fe584bc01ff4d946532491f935d Mon Sep 17 00:00:00 2001 From: Youssef Alnemr <20484975+Nnemr@users.noreply.github.com> Date: Tue, 19 Feb 2019 18:25:48 +0200 Subject: [PATCH 1/5] Add files via upload Run across AFW --- examples/8_generate_posmap_300WLP.py | 48 +++++++++++++++++----------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/examples/8_generate_posmap_300WLP.py b/examples/8_generate_posmap_300WLP.py index 3d48579..bc02de4 100644 --- a/examples/8_generate_posmap_300WLP.py +++ b/examples/8_generate_posmap_300WLP.py @@ -7,8 +7,8 @@ from skimage import io import skimage.transform from time import time +import cv2 import matplotlib.pyplot as plt - sys.path.append('..') import face3d from face3d import mesh @@ -23,10 +23,10 @@ def process_uv(uv_coords, uv_h = 256, uv_w = 256): def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w = 256, image_h = 256, image_w = 256): # 1. load image and fitted parameters - image_name = image_path.strip().split('/')[-1] - image = io.imread(image_path)/255. + image_name = image_path.strip().split('\\')[-1] + image = io.imread(image_path)/255; [h, w, c] = image.shape - + info = sio.loadmat(mat_path) pose_para = info['Pose_Para'].T.astype(np.float32) shape_para = info['Shape_Para'].astype(np.float32) @@ -65,8 +65,7 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w src_pts = np.array([[center[0]-size/2, center[1]-size/2], [center[0] - size/2, center[1]+size/2], [center[0]+size/2, center[1]-size/2]]) DST_PTS = np.array([[0, 0], [0, image_h - 1], [image_w - 1, 0]]) tform = skimage.transform.estimate_transform('similarity', src_pts, DST_PTS) - cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w)) - + cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w)); # transform face position(image vertices) along with 2d facial image position = image_vertices.copy() position[:, 2] = 1 @@ -76,20 +75,22 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w # 4. uv position map: render position in uv space uv_position_map = mesh.render.render_colors(uv_coords, bfm.full_triangles, position, uv_h, uv_w, c = 3) - + #cv2.imshow('image', cropped_image); + #cv2.waitKey(0); + #cv2.destroyAllWindows(); # 5. save files - io.imsave('{}/{}'.format(save_folder, image_name), np.squeeze(cropped_image)) - np.save('{}/{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map) - io.imsave('{}/{}'.format(save_folder, image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map)/max(image_h, image_w)) # only for show + io.imsave('{}\{}'.format(save_folder, image_name), np.squeeze(cropped_image)) + np.save('{}\{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map) + #io.imsave('{}\{}'.format(save_folder, image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map)/max(image_h, image_w)) # only for show # --verify # import cv2 - # uv_texture_map_rec = cv2.remap(cropped_image, uv_position_map[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT,borderValue=(0)) - # io.imsave('{}/{}'.format(save_folder, image_name.replace('.jpg', '_tex.jpg')), np.squeeze(uv_texture_map_rec)) + uv_texture_map_rec = cv2.remap(cropped_image, uv_position_map[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR,borderMode= cv2.BORDER_CONSTANT,borderValue=(0)); + io.imsave('{}\{}'.format(save_folder, image_name.replace('.jpg', '_tex.jpg')), np.squeeze(uv_texture_map_rec)); if __name__ == '__main__': - save_folder = 'results/posmap_300WLP' + save_folder = 'results\posmap_300WLP' if not os.path.exists(save_folder): os.mkdir(save_folder) @@ -99,14 +100,23 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w # load uv coords global uv_coords - uv_coords = face3d.morphable_model.load.load_uv_coords('Data/BFM/Out/BFM_UV.mat') # + uv_coords = face3d.morphable_model.load.load_uv_coords('Data\BFM\Out\BFM_UV.mat') # uv_coords = process_uv(uv_coords, uv_h, uv_w) # load bfm - bfm = MorphabelModel('Data/BFM/Out/BFM.mat') - + bfm = MorphabelModel('Data\BFM\Out\BFM.mat') # run - image_path = 'Data/IBUG_image_008_1_0.jpg' - mat_path = 'Data/IBUG_image_008_1_0.mat' - run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) + previous_file= ''; + for folder in os.listdir("Data\\300W_LP\\AFW"): + folder = folder[:-4]; + if previous_file == '': + previous_file = folder; + elif folder==previous_file: + continue + else: + previous_file = folder; + print(folder); + image_path = 'Data\\300W_LP\\AFW\\'+folder + '.jpg'; + mat_path = 'Data\\300W_LP\\AFW\\'+folder+'.mat' + run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) From eb522eeb1832c3aaff07bbaf7702ad048761e823 Mon Sep 17 00:00:00 2001 From: Youssef Alnemr <20484975+Nnemr@users.noreply.github.com> Date: Wed, 20 Feb 2019 10:23:38 +0200 Subject: [PATCH 2/5] Add files via upload --- examples/8_generate_posmap_300WLP.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/8_generate_posmap_300WLP.py b/examples/8_generate_posmap_300WLP.py index bc02de4..db705c3 100644 --- a/examples/8_generate_posmap_300WLP.py +++ b/examples/8_generate_posmap_300WLP.py @@ -107,7 +107,7 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w bfm = MorphabelModel('Data\BFM\Out\BFM.mat') # run previous_file= ''; - for folder in os.listdir("Data\\300W_LP\\AFW"): + for folder in os.listdir("Dataset\300W_LP\AFW"): folder = folder[:-4]; if previous_file == '': previous_file = folder; @@ -116,7 +116,7 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w else: previous_file = folder; print(folder); - image_path = 'Data\\300W_LP\\AFW\\'+folder + '.jpg'; - mat_path = 'Data\\300W_LP\\AFW\\'+folder+'.mat' + image_path = 'Dataset\\300W_LP\\AFW\\'+folder + '.jpg'; + mat_path = 'Dataset\\300W_LP\\AFW\\'+folder+'.mat' run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) From 883d0c9e5b16e39bd33ec792321152376570e4fd Mon Sep 17 00:00:00 2001 From: Youssef Alnemr <20484975+Nnemr@users.noreply.github.com> Date: Thu, 21 Feb 2019 09:23:21 +0200 Subject: [PATCH 3/5] Add files via upload Edited to run for Drive --- examples/8_generate_posmap_300WLP.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/8_generate_posmap_300WLP.py b/examples/8_generate_posmap_300WLP.py index db705c3..65f6c6c 100644 --- a/examples/8_generate_posmap_300WLP.py +++ b/examples/8_generate_posmap_300WLP.py @@ -100,14 +100,15 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w # load uv coords global uv_coords - uv_coords = face3d.morphable_model.load.load_uv_coords('Data\BFM\Out\BFM_UV.mat') # + uv_coords = face3d.morphable_model.load.load_uv_coords('/content/gdrive/My Drive/Dataset/BFM/BFM_UV.mat') # uv_coords = process_uv(uv_coords, uv_h, uv_w) # load bfm - bfm = MorphabelModel('Data\BFM\Out\BFM.mat') + bfm = MorphabelModel('/content/gdrive/My Drive/Dataset/BFM/BFM.mat') # run previous_file= ''; - for folder in os.listdir("Dataset\300W_LP\AFW"): + ''' + for folder in os.listdir("/content/gdrive/My Drive/Dataset/300W_LP/AFW"): folder = folder[:-4]; if previous_file == '': previous_file = folder; @@ -116,7 +117,8 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w else: previous_file = folder; print(folder); - image_path = 'Dataset\\300W_LP\\AFW\\'+folder + '.jpg'; - mat_path = 'Dataset\\300W_LP\\AFW\\'+folder+'.mat' + ''' + image_path = folder + '.jpg'; + mat_path = folder+'.mat' run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) From b86b8345005623737d8472334117308930da426f Mon Sep 17 00:00:00 2001 From: Youssef Alnemr <20484975+Nnemr@users.noreply.github.com> Date: Thu, 21 Feb 2019 09:43:33 +0200 Subject: [PATCH 4/5] Add files via upload --- examples/8_generate_posmap_300WLP.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/8_generate_posmap_300WLP.py b/examples/8_generate_posmap_300WLP.py index 65f6c6c..51f5eaf 100644 --- a/examples/8_generate_posmap_300WLP.py +++ b/examples/8_generate_posmap_300WLP.py @@ -107,7 +107,6 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w bfm = MorphabelModel('/content/gdrive/My Drive/Dataset/BFM/BFM.mat') # run previous_file= ''; - ''' for folder in os.listdir("/content/gdrive/My Drive/Dataset/300W_LP/AFW"): folder = folder[:-4]; if previous_file == '': @@ -117,7 +116,6 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w else: previous_file = folder; print(folder); - ''' image_path = folder + '.jpg'; mat_path = folder+'.mat' run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) From 99942774d42f1f43f97c35639920071d9df8502e Mon Sep 17 00:00:00 2001 From: Youssef Alnemr <20484975+Nnemr@users.noreply.github.com> Date: Fri, 4 Oct 2019 10:37:55 +0200 Subject: [PATCH 5/5] Update 8_generate_posmap_300WLP.py The generated position maps are now augmented --- examples/8_generate_posmap_300WLP.py | 117 ++++++++++++++++----------- 1 file changed, 71 insertions(+), 46 deletions(-) diff --git a/examples/8_generate_posmap_300WLP.py b/examples/8_generate_posmap_300WLP.py index 51f5eaf..b9da447 100644 --- a/examples/8_generate_posmap_300WLP.py +++ b/examples/8_generate_posmap_300WLP.py @@ -4,7 +4,9 @@ import os, sys import numpy as np import scipy.io as sio -from skimage import io +import random as ran +from skimage.transform import SimilarityTransform +from skimage import io, util import skimage.transform from time import time import cv2 @@ -23,22 +25,21 @@ def process_uv(uv_coords, uv_h = 256, uv_w = 256): def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w = 256, image_h = 256, image_w = 256): # 1. load image and fitted parameters - image_name = image_path.strip().split('\\')[-1] + image_name = image_path.strip().split('/')[-1] image = io.imread(image_path)/255; - [h, w, c] = image.shape + [h, w, c] = image.shape; - info = sio.loadmat(mat_path) - pose_para = info['Pose_Para'].T.astype(np.float32) - shape_para = info['Shape_Para'].astype(np.float32) - exp_para = info['Exp_Para'].astype(np.float32) - - # 2. generate mesh + info = sio.loadmat(mat_path); + pose_para = info['Pose_Para'].T.astype(np.float32); + shape_para = info['Shape_Para'].astype(np.float32); + exp_para = info['Exp_Para'].astype(np.float32); + # 2. generate mesh; # generate shape - vertices = bfm.generate_vertices(shape_para, exp_para) + vertices = bfm.generate_vertices(shape_para, exp_para); # transform mesh - s = pose_para[-1, 0] - angles = pose_para[:3, 0] - t = pose_para[3:6, 0] + s = pose_para[-1, 0]; + angles = pose_para[:3, 0]; + t = pose_para[3:6, 0]; transformed_vertices = bfm.transform_3ddfa(vertices, s, angles, t) projected_vertices = transformed_vertices.copy() # using stantard camera & orth projection as in 3DDFA image_vertices = projected_vertices.copy() @@ -58,65 +59,89 @@ def run_posmap_300W_LP(bfm, image_path, mat_path, save_folder, uv_h = 256, uv_w marg = old_size*0.1 t_x = np.random.rand()*marg*2 - marg t_y = np.random.rand()*marg*2 - marg - center[0] = center[0]+t_x; center[1] = center[1]+t_y + center[0] = center[0]+t_x; + center[1] = center[1]+t_y size = size*(np.random.rand()*0.2 + 0.9) # crop and record the transform parameters - src_pts = np.array([[center[0]-size/2, center[1]-size/2], [center[0] - size/2, center[1]+size/2], [center[0]+size/2, center[1]-size/2]]) - DST_PTS = np.array([[0, 0], [0, image_h - 1], [image_w - 1, 0]]) - tform = skimage.transform.estimate_transform('similarity', src_pts, DST_PTS) - cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w)); + src_pts = np.array([[center[0]-size/2, center[1]-size/2], [center[0] - size/2, center[1]+size/2], [center[0]+size/2, center[1]-size/2]]); + DST_PTS = np.array([[0, 0], [0, image_h - 1], [image_w - 1, 0]]); + tform = skimage.transform.estimate_transform('similarity', src_pts, DST_PTS); # transform face position(image vertices) along with 2d facial image + angle = np.random.rand() * 90 - 45; + rows, cols = image.shape[0], image.shape[1]; + # rotation around center + center = np.array((cols, rows)) / 2. - 0.5; + #Augmenting the dataset + tform1 = SimilarityTransform(translation=center); + tform2 = SimilarityTransform(rotation=np.deg2rad(angle)); + tform3 = SimilarityTransform(translation=-center); + rotate_transform = tform3 + tform2 + tform1; + tform = rotate_transform + tform; + #end of augmentation + opt = ran.randint(1,2); + cropped_image = skimage.transform.warp(image, tform.inverse, output_shape=(image_h, image_w)); position = image_vertices.copy() position[:, 2] = 1 position = np.dot(position, tform.params.T) position[:, 2] = image_vertices[:, 2]*tform.params[0, 0] # scale z position[:, 2] = position[:, 2] - np.min(position[:, 2]) # translate z - # 4. uv position map: render position in uv space uv_position_map = mesh.render.render_colors(uv_coords, bfm.full_triangles, position, uv_h, uv_w, c = 3) #cv2.imshow('image', cropped_image); #cv2.waitKey(0); #cv2.destroyAllWindows(); # 5. save files - io.imsave('{}\{}'.format(save_folder, image_name), np.squeeze(cropped_image)) - np.save('{}\{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map) - #io.imsave('{}\{}'.format(save_folder, image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map)/max(image_h, image_w)) # only for show - + io.imsave('{}\{}'.format(save_folder, image_name), np.squeeze(cropped_image)); + np.save('{}\{}'.format(save_folder, image_name.replace('jpg', 'npy')), uv_position_map); + io.imsave('{}\{}'.format('results/uv_maps/', image_name.replace('.jpg', '_posmap.jpg')), (uv_position_map)/abs(uv_position_map.max())); # only for show + #cv2.imwrite(image_name[:-4]+'_posmap.jpg',uv_position_map); # --verify - # import cv2 + #import cv2 uv_texture_map_rec = cv2.remap(cropped_image, uv_position_map[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR,borderMode= cv2.BORDER_CONSTANT,borderValue=(0)); - io.imsave('{}\{}'.format(save_folder, image_name.replace('.jpg', '_tex.jpg')), np.squeeze(uv_texture_map_rec)); + #io.imsave('{}\{}'.format(save_folder, image_name.replace('.jpg', '_tex.jpg')), np.squeeze(uv_texture_map_rec)); #Save fitted face on position map (texture). if __name__ == '__main__': - save_folder = 'results\posmap_300WLP' + save_folder = 'results/' if not os.path.exists(save_folder): os.mkdir(save_folder) - # set para uv_h = uv_w = 256 - image_h = image_w = 256 - + image_h = image_w = 256; # load uv coords global uv_coords - uv_coords = face3d.morphable_model.load.load_uv_coords('/content/gdrive/My Drive/Dataset/BFM/BFM_UV.mat') # + uv_coords = face3d.morphable_model.load.load_uv_coords('Data/BFM/Out/BFM_UV.mat') uv_coords = process_uv(uv_coords, uv_h, uv_w) - + ''' + Save LFPW Testing as well, only the first 8. + ''' # load bfm - bfm = MorphabelModel('/content/gdrive/My Drive/Dataset/BFM/BFM.mat') + bfm = MorphabelModel('Data/BFM/Out/BFM.mat') # run - previous_file= ''; - for folder in os.listdir("/content/gdrive/My Drive/Dataset/300W_LP/AFW"): - folder = folder[:-4]; - if previous_file == '': - previous_file = folder; - elif folder==previous_file: - continue - else: - previous_file = folder; - print(folder); - image_path = folder + '.jpg'; - mat_path = folder+'.mat' - run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) - + content = []; + print('Running'); + s =0; + f=0; + types = ['AFW', 'HELEN', 'LFPW', 'IBUG', 'LFPW_Test']; + for i in types: + print(i); + with open(i+'_Data.txt', 'r') as fileRead: + content = [file.rstrip('\n') for file in fileRead]; + s=0; + print(len(content)); + for filename in content: + #print(filename) + #if(s==8 and i is 'LFPW_Test'): + # break + if(s%500 ==0): + print(str(s) +'/' +str(len(content))) + image_path = 'Data/BFM/300W_LP/'+ i+'/'+filename+'.jpg'; + mat_path = 'Data/BFM/300W_LP/'+i+'/'+filename+'.mat'; + if(i is 'LFPW_Test'): + image_path = 'Data/BFM/300W_LP/'+ 'LFPW'+'/'+filename+'.jpg'; + mat_path = 'Data/BFM/300W_LP/'+'LFPW'+'/'+filename+'.mat'; + run_posmap_300W_LP(bfm, image_path, mat_path, save_folder) + s+=1; + print(s+f) + print(f)