def param_recon_3d_vertex(param_path, reference_img_names_list, method='', dense=True): # load 62d param param_62d = _load(param_path) # reconstruct 3d face shape, include pose information, and save 3d vertex info benchmark_3d_vertex_save(param_62d, reference_img_names_list, method, dense=True)
def gen_3d_vertex(filelists, params_path, save_path): # dense 3d face rather than 68 keypoints fns = open(filelists).read().strip().split('\n') params = _load(params_path) #jia zai 2000 ge yang ben de 3d data for i in range(2000): fn = fns[i] vertex = reconstruct_vertex(params[i], dense=True) #dense 3d face wfp = osp.join(save_path, fn.replace('.jpg', '.mat')) print(wfp) sio.savemat(wfp, {'vertex': vertex})
def gen_3d_vertex(): filelists = 'test.data/AFLW2000-3D_crop.list' root = 'test.data/AFLW-2000-3D/' fns = open(filelists).read().strip().split('\n') params = _load('results/params_aflw2000_xgtu.npy') for i in range(100): fn = fns[i] vertex = reconstruct_vertex(params[i], dense=True) wfp = osp.join('results/AFLW-2000-3D_vertex/', fn.replace('.jpg', '.mat')) print(wfp) sio.savemat(wfp, {'vertex': vertex})
def draw_landmarks(): root_ori = 'test.data/AFLW-2000-3D' root = 'test.data/AFLW2000-3D_crop' filelists = 'test.data/AFLW2000-3D_crop.list' fns = open(filelists).read().strip().split('\n') params = _load('results/params_aflw2000_xgtu.npy') for i in range(len(fns)): plt.close() img_fp = osp.join(root_ori, fns[i]) img = io.imread(img_fp) lms = reconstruct_vertex(params[i], dense=False) lms = convert_to_ori(lms, i) # lms = convert_to_ori_frmMat(lms, [root + fns[i]]) # print(lms.shape) fig = plt.figure(figsize=plt.figaspect(.5)) # fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(1, 2, 1) ax.imshow(img) alpha = 0.8 markersize = 1.5 lw = 1.2 color = 'w' markeredgecolor = 'b' nums = [0, 17, 22, 27, 31, 36, 42, 48, 60, 68] for ind in range(len(nums) - 1): l, r = nums[ind], nums[ind + 1] ax.plot(lms[0, l:r], lms[1, l:r], color=color, lw=lw, alpha=alpha - 0.1) ax.plot(lms[0, l:r], lms[1, l:r], marker='o', linestyle='None', markersize=markersize, color=color, markeredgecolor=markeredgecolor, alpha=alpha) ax.axis('off') plt.savefig('results/3dLandmarks_proj_resnet50_4chls/' + img_fp.split('/')[-1])
def load_bfm(model_path): suffix = _get_suffix(model_path) if suffix == 'mat': C = sio.loadmat(model_path) model = C['model_refine'] model = model[0, 0] model_new = {} w_shp = model['w'].astype(np.float32) model_new['w_shp_sim'] = w_shp[:, :40] w_exp = model['w_exp'].astype(np.float32) model_new['w_exp_sim'] = w_exp[:, :10] u_shp = model['mu_shape'] u_exp = model['mu_exp'] u = (u_shp + u_exp).astype(np.float32) model_new['mu'] = u model_new['tri'] = model['tri'].astype(np.int32) - 1 # flatten it, pay attention to index value keypoints = model['keypoints'].astype(np.int32) - 1 keypoints = np.concatenate( (3 * keypoints, 3 * keypoints + 1, 3 * keypoints + 2), axis=0) model_new['keypoints'] = keypoints.T.flatten() # w = np.concatenate((w_shp, w_exp), axis=1) w_base = w[keypoints] w_norm = np.linalg.norm(w, axis=0) w_base_norm = np.linalg.norm(w_base, axis=0) dim = w_shp.shape[0] // 3 u_base = u[keypoints].reshape(-1, 1) w_shp_base = w_shp[keypoints] w_exp_base = w_exp[keypoints] model_new['w_norm'] = w_norm model_new['w_base_norm'] = w_base_norm model_new['dim'] = dim model_new['u_base'] = u_base model_new['w_shp_base'] = w_shp_base model_new['w_exp_base'] = w_exp_base _dump(model_path.replace('.mat', '.pkl'), model_new) return model_new else: return _load(model_path)
def gen_3d_vertex(): filelists = 'test.data/AFLW2000-3D_crop.list' root = 'AFLW-2000-3D/' fns = open(filelists).read().strip().split('\n') params = _load('res/params_aflw2000.npy') sel = [ '00427', '00439', '00475', '00477', '00497', '00514', '00562', '00623', '01045', '01095', '01104', '01506', '01621', '02214', '02244', '03906', '04157' ] sel = list(map(lambda x: f'image{x}.jpg', sel)) for i in range(2000): fn = fns[i] if fn in sel: vertex = reconstruct_vertex(params[i], dense=True) wfp = osp.join('res/AFLW-2000-3D_vertex/', fn.replace('.jpg', '.mat')) print(wfp) sio.savemat(wfp, {'vertex': vertex})
def _load_gpu(fp): return torch.from_numpy(_load(fp)).cuda()
def _load_tensor(fp, mode='cpu'): if mode.lower() == 'cpu': return torch.from_numpy(_load(fp)) elif mode.lower() == 'gpu': return torch.from_numpy(_load(fp)).cuda()
#!/usr/bin/env python3 # coding: utf-8 import os.path as osp import numpy as np from io_utils import _load, _numpy_to_cuda, _numpy_to_tensor, _load_gpu def make_abs_path(d): return osp.join(osp.dirname(osp.realpath(__file__)), d) d = make_abs_path('train.configs') keypoints = _load(osp.join(d, 'keypoints_sim.npy')) w_shp = _load(osp.join(d, 'w_shp_sim.npy')) w_exp = _load(osp.join(d, 'w_exp_sim.npy')) # simplified version meta = _load(osp.join(d, 'param_whitening.pkl')) param_mean = meta.get('param_mean') param_std = meta.get('param_std') u_shp = _load(osp.join(d, 'u_shp.npy')) u_exp = _load(osp.join(d, 'u_exp.npy')) u = u_shp + u_exp w = np.concatenate((w_shp, w_exp), axis=1) w_base = w[keypoints] w_norm = np.linalg.norm(w, axis=0) w_base_norm = np.linalg.norm(w_base, axis=0) # for test dim = w_shp.shape[0] // 3 u_base = u[keypoints].reshape(-1, 1)
#from benchmark_aflw1998 import ana as ana_alfw1998 from benchmark_aflw import calc_nme as calc_nme_alfw from benchmark_aflw import ana as ana_aflw from resnet_xgtu_4chls import resnet50 from ddfa_utils2 import ToTensorGjz, NormalizeGjz, DDFATestDataset, reconstruct_vertex import argparse from io_utils import _load, _numpy_to_cuda, _numpy_to_tensor import os.path as osp import numpy as np from math import sqrt from io_utils import _load d = 'test.configs' fail_detect = [1082 - 1, 1799 - 1] yaws_list = _load(osp.join(d, 'AFLW2000-3D.pose.npy')) yaws_list = [ yaws_list[idx] for idx in range(len(yaws_list)) if idx not in fail_detect ] # pts21 = _load(osp.join(d, 'AFLW2000-3D.pts21.npy')) # origin pts68_all_ori = _load(osp.join(d, 'AFLW2000-3D.pts68.npy')) pts68_all_ori = [ pts68_all_ori[idx] for idx in range(len(pts68_all_ori)) if idx not in fail_detect ] # reannonated pts68_all_re = _load(osp.join(d, 'AFLW2000-3D-Reannotated.pts68.npy')) pts68_all_re = [
def draw_landmarks(): filelists = 'test.data/AFLW2000-3D_crop.list' root = 'AFLW-2000-3D/' fns = open(filelists).read().strip().split('\n') params = _load('res/params_aflw2000.npy') for i in range(2000): plt.close() img_fp = osp.join(root, fns[i]) img = io.imread(img_fp) lms = reconstruct_vertex(params[i], dense=False) lms = convert_to_ori(lms, i) # print(lms.shape) fig = plt.figure(figsize=plt.figaspect(.5)) # fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(1, 2, 1) ax.imshow(img) alpha = 0.8 markersize = 4 lw = 1.5 color = 'w' markeredgecolor = 'black' nums = [0, 17, 22, 27, 31, 36, 42, 48, 60, 68] for ind in range(len(nums) - 1): l, r = nums[ind], nums[ind + 1] ax.plot(lms[0, l:r], lms[1, l:r], color=color, lw=lw, alpha=alpha - 0.1) ax.plot(lms[0, l:r], lms[1, l:r], marker='o', linestyle='None', markersize=markersize, color=color, markeredgecolor=markeredgecolor, alpha=alpha) ax.axis('off') # 3D ax = fig.add_subplot(1, 2, 2, projection='3d') lms[1] = img.shape[1] - lms[1] lms[2] = -lms[2] # print(lms) ax.scatter(lms[0], lms[2], lms[1], c="cyan", alpha=1.0, edgecolor='b') for ind in range(len(nums) - 1): l, r = nums[ind], nums[ind + 1] ax.plot3D(lms[0, l:r], lms[2, l:r], lms[1, l:r], color='blue') ax.view_init(elev=5., azim=-95) # ax.set_xlabel('x') # ax.set_ylabel('y') # ax.set_zlabel('z') ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_zticklabels([]) plt.tight_layout() # plt.show() wfp = f'res/AFLW-2000-3D/{osp.basename(img_fp)}' plt.savefig(wfp, dpi=200)
#!/usr/bin/env python3 # coding: utf-8 import os import os.path as osp import numpy as np import sys from glob import glob from math import sqrt from io_utils import _load d = 'test.configs' yaw_list = _load(osp.join(d, 'AFLW_GT_crop_yaws.npy')) roi_boxs = _load(osp.join(d, 'AFLW_GT_crop_roi_box.npy')) pts68_all = _load(osp.join(d, 'AFLW_GT_pts68.npy')) pts21_all = _load(osp.join(d, 'AFLW_GT_pts21.npy')) def ana(nme_list): yaw_list_abs = np.abs(yaw_list) ind_yaw_1 = yaw_list_abs <= 30 ind_yaw_2 = np.bitwise_and(yaw_list_abs > 30, yaw_list_abs <= 60) ind_yaw_3 = yaw_list_abs > 60 nme_1 = nme_list[ind_yaw_1] nme_2 = nme_list[ind_yaw_2] nme_3 = nme_list[ind_yaw_3] mean_nme_1 = np.mean(nme_1) * 100 mean_nme_2 = np.mean(nme_2) * 100 mean_nme_3 = np.mean(nme_3) * 100