CoolFace
Apppublic

pengsida/NeuralBody

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
base_utils.py47 linesDownload Raw Back to utils
1import pickle2import os3import numpy as np4 5 6def read_pickle(pkl_path):7    with open(pkl_path, 'rb') as f:8        return pickle.load(f)9 10 11def save_pickle(data, pkl_path):12    os.system('mkdir -p {}'.format(os.path.dirname(pkl_path)))13    with open(pkl_path, 'wb') as f:14        pickle.dump(data, f)15 16 17def project(xyz, K, RT):18    """19    xyz: [N, 3]20    K: [3, 3]21    RT: [3, 4]22    """23    xyz = np.dot(xyz, RT[:, :3].T) + RT[:, 3:].T24    xyz = np.dot(xyz, K.T)25    xy = xyz[:, :2] / xyz[:, 2:]26    return xy27 28 29def write_K_pose_inf(K, poses, img_root):30    K = K.copy()31    K[:2] = K[:2] * 832    K_inf = os.path.join(img_root, 'Intrinsic.inf')33    os.system('mkdir -p {}'.format(os.path.dirname(K_inf)))34    with open(K_inf, 'w') as f:35        for i in range(len(poses)):36            f.write('%d\n'%i)37            f.write('%f %f %f\n %f %f %f\n %f %f %f\n' % tuple(K.reshape(9).tolist()))38            f.write('\n')39 40    pose_inf = os.path.join(img_root, 'CamPose.inf')41    with open(pose_inf, 'w') as f:42        for pose in poses:43            pose = np.linalg.inv(pose)44            A = pose[0:3,:]45            tmp = np.concatenate([A[0:3,2].T, A[0:3,0].T,A[0:3,1].T,A[0:3,3].T])46            f.write('%f %f %f %f %f %f %f %f %f %f %f %f\n' % tuple(tmp.tolist()))47