Spaces:
Runtime error
Runtime error
File size: 7,460 Bytes
da48dbe 3577d3c da48dbe c3d3e4a da48dbe c3d3e4a da48dbe 3577d3c da48dbe 3577d3c c3d3e4a 3577d3c c3d3e4a da48dbe c3d3e4a da48dbe fb140f6 da48dbe 3577d3c fb140f6 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a fb140f6 c3d3e4a 3577d3c fb140f6 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c fb140f6 c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a fb140f6 c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c c3d3e4a 3577d3c fb140f6 3577d3c c3d3e4a 3577d3c fb140f6 3577d3c c3d3e4a 3577d3c c3d3e4a fb140f6 da48dbe 3577d3c fb140f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import numpy as np
import trimesh
import torch
import argparse
import os.path as osp
import lib.smplx as smplx
from pytorch3d.ops import SubdivideMeshes
from pytorch3d.structures import Meshes
from lib.smplx.lbs import general_lbs
from lib.dataset.mesh_util import keep_largest, poisson
from scipy.spatial import cKDTree
from lib.dataset.mesh_util import SMPLX
from lib.common.local_affine import register
# loading cfg file
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", type=str, default="")
parser.add_argument("-g", "--gpu", type=int, default=0)
args = parser.parse_args()
smplx_container = SMPLX()
device = torch.device(f"cuda:{args.gpu}")
prefix = f"./results/econ/obj/{args.name}"
smpl_path = f"{prefix}_smpl_00.npy"
econ_path = f"{prefix}_0_full.obj"
smplx_param = np.load(smpl_path, allow_pickle=True).item()
econ_obj = trimesh.load(econ_path)
econ_obj.vertices *= np.array([1.0, -1.0, -1.0])
econ_obj.vertices /= smplx_param["scale"].cpu().numpy()
econ_obj.vertices -= smplx_param["transl"].cpu().numpy()
for key in smplx_param.keys():
smplx_param[key] = smplx_param[key].cpu().view(1, -1)
smpl_model = smplx.create(
smplx_container.model_dir,
model_type="smplx",
gender="neutral",
age="adult",
use_face_contour=False,
use_pca=False,
num_betas=200,
num_expression_coeffs=50,
ext='pkl'
)
smpl_out_lst = []
for pose_type in ["t-pose", "da-pose", "pose"]:
smpl_out_lst.append(
smpl_model(
body_pose=smplx_param["body_pose"],
global_orient=smplx_param["global_orient"],
betas=smplx_param["betas"],
expression=smplx_param["expression"],
jaw_pose=smplx_param["jaw_pose"],
left_hand_pose=smplx_param["left_hand_pose"],
right_hand_pose=smplx_param["right_hand_pose"],
return_verts=True,
return_full_pose=True,
return_joint_transformation=True,
return_vertex_transformation=True,
pose_type=pose_type
)
)
smpl_verts = smpl_out_lst[2].vertices.detach()[0]
smpl_tree = cKDTree(smpl_verts.cpu().numpy())
dist, idx = smpl_tree.query(econ_obj.vertices, k=5)
if not osp.exists(f"{prefix}_econ_da.obj") or not osp.exists(f"{prefix}_smpl_da.obj"):
# t-pose for ECON
econ_verts = torch.tensor(econ_obj.vertices).float()
rot_mat_t = smpl_out_lst[2].vertex_transformation.detach()[0][idx[:, 0]]
homo_coord = torch.ones_like(econ_verts)[..., :1]
econ_cano_verts = torch.inverse(rot_mat_t) @ torch.cat([econ_verts, homo_coord],
dim=1).unsqueeze(-1)
econ_cano_verts = econ_cano_verts[:, :3, 0].cpu()
econ_cano = trimesh.Trimesh(econ_cano_verts, econ_obj.faces)
# da-pose for ECON
rot_mat_da = smpl_out_lst[1].vertex_transformation.detach()[0][idx[:, 0]]
econ_da_verts = rot_mat_da @ torch.cat([econ_cano_verts, homo_coord], dim=1).unsqueeze(-1)
econ_da = trimesh.Trimesh(econ_da_verts[:, :3, 0].cpu(), econ_obj.faces)
# da-pose for SMPL-X
smpl_da = trimesh.Trimesh(
smpl_out_lst[1].vertices.detach()[0], smpl_model.faces, maintain_orders=True, process=False
)
smpl_da.export(f"{prefix}_smpl_da.obj")
# remove hands from ECON for next registeration
econ_da_body = econ_da.copy()
mano_mask = ~np.isin(idx[:, 0], smplx_container.smplx_mano_vid)
econ_da_body.update_faces(mano_mask[econ_da.faces].all(axis=1))
econ_da_body.remove_unreferenced_vertices()
econ_da_body = keep_largest(econ_da_body)
# remove SMPL-X hand and face
register_mask = ~np.isin(
np.arange(smpl_da.vertices.shape[0]),
np.concatenate([smplx_container.smplx_mano_vid, smplx_container.smplx_front_flame_vid])
)
register_mask *= ~smplx_container.eyeball_vertex_mask.bool().numpy()
smpl_da_body = smpl_da.copy()
smpl_da_body.update_faces(register_mask[smpl_da.faces].all(axis=1))
smpl_da_body.remove_unreferenced_vertices()
smpl_da_body = keep_largest(smpl_da_body)
# upsample the smpl_da_body and do registeration
smpl_da_body = Meshes(
verts=[torch.tensor(smpl_da_body.vertices).float()],
faces=[torch.tensor(smpl_da_body.faces).long()],
).to(device)
sm = SubdivideMeshes(smpl_da_body)
smpl_da_body = register(econ_da_body, sm(smpl_da_body), device)
# remove over-streched+hand faces from ECON
econ_da_body = econ_da.copy()
edge_before = np.sqrt(
((econ_obj.vertices[econ_cano.edges[:, 0]] -
econ_obj.vertices[econ_cano.edges[:, 1]])**2).sum(axis=1)
)
edge_after = np.sqrt(
((econ_da.vertices[econ_cano.edges[:, 0]] -
econ_da.vertices[econ_cano.edges[:, 1]])**2).sum(axis=1)
)
edge_diff = edge_after / edge_before.clip(1e-2)
streched_mask = np.unique(econ_cano.edges[edge_diff > 6])
mano_mask = ~np.isin(idx[:, 0], smplx_container.smplx_mano_vid)
mano_mask[streched_mask] = False
econ_da_body.update_faces(mano_mask[econ_cano.faces].all(axis=1))
econ_da_body.remove_unreferenced_vertices()
# stitch the registered SMPL-X body and floating hands to ECON
econ_da_tree = cKDTree(econ_da.vertices)
dist, idx = econ_da_tree.query(smpl_da_body.vertices, k=1)
smpl_da_body.update_faces((dist > 0.02)[smpl_da_body.faces].all(axis=1))
smpl_da_body.remove_unreferenced_vertices()
smpl_hand = smpl_da.copy()
smpl_hand.update_faces(smplx_container.mano_vertex_mask.numpy()[smpl_hand.faces].all(axis=1))
smpl_hand.remove_unreferenced_vertices()
econ_da = sum([smpl_hand, smpl_da_body, econ_da_body])
econ_da = poisson(econ_da, f"{prefix}_econ_da.obj")
else:
econ_da = trimesh.load(f"{prefix}_econ_da.obj")
smpl_da = trimesh.load(f"{prefix}_smpl_da.obj", maintain_orders=True, process=False)
smpl_tree = cKDTree(smpl_da.vertices)
dist, idx = smpl_tree.query(econ_da.vertices, k=5)
knn_weights = np.exp(-dist**2)
knn_weights /= knn_weights.sum(axis=1, keepdims=True)
econ_J_regressor = (smpl_model.J_regressor[:, idx] * knn_weights[None]).sum(axis=-1)
econ_lbs_weights = (smpl_model.lbs_weights.T[:, idx] * knn_weights[None]).sum(axis=-1).T
num_posedirs = smpl_model.posedirs.shape[0]
econ_posedirs = (
smpl_model.posedirs.view(num_posedirs, -1, 3)[:, idx, :] * knn_weights[None, ..., None]
).sum(axis=-2).view(num_posedirs, -1).float()
econ_J_regressor /= econ_J_regressor.sum(axis=1, keepdims=True)
econ_lbs_weights /= econ_lbs_weights.sum(axis=1, keepdims=True)
# re-compute da-pose rot_mat for ECON
rot_mat_da = smpl_out_lst[1].vertex_transformation.detach()[0][idx[:, 0]]
econ_da_verts = torch.tensor(econ_da.vertices).float()
econ_cano_verts = torch.inverse(rot_mat_da) @ torch.cat(
[econ_da_verts, torch.ones_like(econ_da_verts)[..., :1]], dim=1
).unsqueeze(-1)
econ_cano_verts = econ_cano_verts[:, :3, 0].double()
# ----------------------------------------------------
# use any SMPL-X pose to animate ECON reconstruction
# ----------------------------------------------------
new_pose = smpl_out_lst[2].full_pose
new_pose[:, :3] = 0.
posed_econ_verts, _ = general_lbs(
pose=new_pose,
v_template=econ_cano_verts.unsqueeze(0),
posedirs=econ_posedirs,
J_regressor=econ_J_regressor,
parents=smpl_model.parents,
lbs_weights=econ_lbs_weights
)
econ_pose = trimesh.Trimesh(posed_econ_verts[0].detach(), econ_da.faces)
econ_pose.export(f"{prefix}_econ_pose.obj")
|