|
| 1 | +import json |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +from pathlib import Path |
| 6 | +import time |
| 7 | +import numpy as np |
| 8 | +import trimesh |
| 9 | +from tqdm import tqdm |
| 10 | + |
| 11 | + |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +def tri2tet(tris): |
| 16 | + v1 = tris[:, 0, :] |
| 17 | + v2 = tris[:, 1, :] |
| 18 | + v3 = tris[:, 2, :] |
| 19 | + |
| 20 | + e21 = v2 - v1 |
| 21 | + e31 = v3 - v1 |
| 22 | + n = np.cross(e21, e31) |
| 23 | + n = n / np.sqrt(np.linalg.norm(n, axis=1, keepdims=True)) |
| 24 | + |
| 25 | + v4 = v1 + n |
| 26 | + |
| 27 | + return v1, v2, v3, v4 |
| 28 | + |
| 29 | + |
| 30 | +def gradient(v1, v2, v3, v4): |
| 31 | + n = v1.shape[0] |
| 32 | + |
| 33 | + R = np.stack([v2 - v1, v3 - v1, v4 - v1], axis=2) |
| 34 | + T = v1 |
| 35 | + |
| 36 | + RT = np.eye(4)[None].repeat(n, axis=0).astype(np.float32) |
| 37 | + |
| 38 | + RT[:, :3, :3] = R |
| 39 | + RT[:, :3, 3] = T |
| 40 | + |
| 41 | + return RT |
| 42 | + |
| 43 | +def calculate_tbn(mesh): |
| 44 | + tris = mesh[0][mesh[1]] |
| 45 | + tets = tri2tet(tris) |
| 46 | + R = gradient(*tets)[:, :3, :3] |
| 47 | + return R |
| 48 | + |
| 49 | + |
| 50 | +def deformation_gradient(canon_tris, deform_tris): |
| 51 | + tet_canon = tri2tet(canon_tris) |
| 52 | + tet_def = tri2tet(deform_tris) |
| 53 | + |
| 54 | + RT_canon = gradient(*tet_canon) |
| 55 | + RT_def = gradient(*tet_def) |
| 56 | + |
| 57 | + to_local = np.linalg.inv(RT_canon) |
| 58 | + to_deform = RT_def |
| 59 | + |
| 60 | + return to_local, to_deform |
| 61 | + |
| 62 | + |
| 63 | +def transform_vertices(vertices, faces, to_local, to_deform): |
| 64 | + tris = vertices[faces] # Shape: (num_faces, 3, 3) |
| 65 | + homogeneous_tris = np.concatenate([tris, np.ones((*tris.shape[:2], 1))], axis=2) # Shape: (num_faces, 3, 4) |
| 66 | + local_tris = np.einsum('fij,fkj->fki', to_local, homogeneous_tris) # Shape: (num_faces, 3, 4) |
| 67 | + deformed_tris = np.einsum('fij,fkj->fki', to_deform, local_tris) # Shape: (num_faces, 3, 4) |
| 68 | + transformed_tris = deformed_tris[:, :, :3] / deformed_tris[:, :, 3:4] |
| 69 | + |
| 70 | + transformed_vertices = np.zeros_like(vertices) |
| 71 | + counts = np.zeros((vertices.shape[0], 1)) |
| 72 | + |
| 73 | + np.add.at(transformed_vertices, faces[:, 0], transformed_tris[:, 0]) |
| 74 | + np.add.at(transformed_vertices, faces[:, 1], transformed_tris[:, 1]) |
| 75 | + np.add.at(transformed_vertices, faces[:, 2], transformed_tris[:, 2]) |
| 76 | + |
| 77 | + np.add.at(counts, faces[:, 0], 1) |
| 78 | + np.add.at(counts, faces[:, 1], 1) |
| 79 | + np.add.at(counts, faces[:, 2], 1) |
| 80 | + |
| 81 | + transformed_vertices = np.divide(transformed_vertices, counts, where=counts != 0) |
| 82 | + |
| 83 | + return transformed_vertices |
| 84 | + |
| 85 | + |
| 86 | +def process(canon_target, canon_source, source_data, target_data, source, target): |
| 87 | + source_meshes = [] |
| 88 | + source_frames = source_data["frames"] |
| 89 | + for frame in tqdm(source_frames): |
| 90 | + mesh_path = frame["mesh_path"] |
| 91 | + mesh = trimesh.load(f"./data/{source}/{mesh_path}", process=False) |
| 92 | + source_meshes.append(mesh) |
| 93 | + |
| 94 | + canon_tris = canon_source.vertices[canon_source.faces] |
| 95 | + target_canon = canon_target.vertices[canon_target.faces] |
| 96 | + |
| 97 | + mesh_dst = f"./data/{target}/transfer/meshes" |
| 98 | + os.system(f"rm -rf {mesh_dst}") |
| 99 | + Path(mesh_dst).mkdir(parents=True, exist_ok=True) |
| 100 | + |
| 101 | + expr_dst = f"./data/{target}/transfer/expr" |
| 102 | + os.system(f"rm -rf {expr_dst}") |
| 103 | + Path(expr_dst).mkdir(parents=True, exist_ok=True) |
| 104 | + |
| 105 | + target_frames = target_data["frames"] |
| 106 | + for i, src_mesh in tqdm(enumerate(source_meshes)): |
| 107 | + vertices = src_mesh.vertices |
| 108 | + faces = src_mesh.faces |
| 109 | + deform_tris = vertices[faces] |
| 110 | + to_local, to_deform = deformation_gradient(canon_tris, deform_tris) |
| 111 | + new_vertices = transform_vertices(canon_target.vertices, faces, to_local, to_deform) |
| 112 | + mesh_name = f"{str(i).zfill(5)}.obj" |
| 113 | + expr_name = f"{str(i).zfill(5)}.txt" |
| 114 | + trimesh.Trimesh(new_vertices, faces).export(f"{mesh_dst}/{mesh_name}") |
| 115 | + |
| 116 | + src_expr = source_frames[i]["exp_path"] |
| 117 | + os.system(f"cp ./data/{source}/{src_expr} {expr_dst}/{expr_name}") |
| 118 | + |
| 119 | + source_frames[i]["mesh_path"] = f"transfer/meshes/{mesh_name}" |
| 120 | + source_frames[i]["exp_path"] = f"transfer/expr/{expr_name}" |
| 121 | + # Copy rest |
| 122 | + source_frames[i]["depth_path"] = target_frames[i]["depth_path"] |
| 123 | + source_frames[i]["file_path"] = target_frames[i]["depth_path"] |
| 124 | + source_frames[i]["seg_mask_path"] = target_frames[i]["seg_mask_path"] |
| 125 | + |
| 126 | + return source_frames |
| 127 | + |
| 128 | + |
| 129 | +def main(): |
| 130 | + # Set up argument parser |
| 131 | + parser = argparse.ArgumentParser(description="Update JSON file copy with n_sample.") |
| 132 | + parser.add_argument("--target", type=str, required=True, help="The target actor name used to locate the JSON file.") |
| 133 | + parser.add_argument("--source", type=str, required=True, help="The source actor name used to locate the JSON file.") |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + print(f"Processing {args.source} to {args.target}") |
| 137 | + |
| 138 | + # Define JSON file paths |
| 139 | + source_test_path = f"./data/{args.source}/transforms_test.json" |
| 140 | + target_test_path = f"./data/{args.target}/transforms_test.json" |
| 141 | + |
| 142 | + canonical_target = trimesh.load(f"./data/{args.target}/canonical.obj", process=False) |
| 143 | + canonical_soruce = trimesh.load(f"./data/{args.source}/canonical.obj", process=False) |
| 144 | + |
| 145 | + with open(source_test_path, "r") as file: |
| 146 | + source_data = json.load(file) |
| 147 | + |
| 148 | + with open(target_test_path, "r") as file: |
| 149 | + target_data = json.load(file) |
| 150 | + |
| 151 | + source_data["frames"] = process(canonical_target, canonical_soruce, source_data, target_data, args.source, args.target) |
| 152 | + with open(f"./data/{args.target}/transforms_transfer.json", "w") as file: |
| 153 | + json.dump(source_data, file, indent=4) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments