Spaces:
Running
Running
File size: 6,235 Bytes
c0eac48 |
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 197 198 199 200 201 202 203 204 205 |
import torch
import numpy as np
import argparse
import pickle
import smplx
from utils import bvh, quat
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, default="./visualization/data/smpl/")
parser.add_argument("--model_type", type=str, default="smpl", choices=["smpl", "smplx"])
parser.add_argument("--gender", type=str, default="MALE", choices=["MALE", "FEMALE", "NEUTRAL"])
parser.add_argument("--num_betas", type=int, default=10, choices=[10, 300])
parser.add_argument("--poses", type=str, default="data/gWA_sFM_cAll_d27_mWA5_ch20.pkl")
parser.add_argument("--fps", type=int, default=60)
parser.add_argument("--output", type=str, default="data/gWA_sFM_cAll_d27_mWA5_ch20.bvh")
parser.add_argument("--mirror", action="store_true")
return parser.parse_args()
def mirror_rot_trans(lrot, trans, names, parents):
joints_mirror = np.array([(
names.index("Left"+n[5:]) if n.startswith("Right") else (
names.index("Right"+n[4:]) if n.startswith("Left") else
names.index(n))) for n in names])
mirror_pos = np.array([-1, 1, 1])
mirror_rot = np.array([1, 1, -1, -1])
grot = quat.fk_rot(lrot, parents)
trans_mirror = mirror_pos * trans
grot_mirror = mirror_rot * grot[:,joints_mirror]
return quat.ik_rot(grot_mirror, parents), trans_mirror
def smpl2bvh(model_path:str, poses:str, output:str, mirror:bool,
model_type="smpl", gender="MALE",
num_betas=10, fps=60) -> None:
"""Save bvh file created by smpl parameters.
Args:
model_path (str): Path to smpl models.
poses (str): Path to npz or pkl file.
output (str): Where to save bvh.
mirror (bool): Whether save mirror motion or not.
model_type (str, optional): I prepared "smpl" only. Defaults to "smpl".
gender (str, optional): Gender Information. Defaults to "MALE".
num_betas (int, optional): How many pca parameters to use in SMPL. Defaults to 10.
fps (int, optional): Frame per second. Defaults to 30.
"""
# names = [
# "Pelvis",
# "Left_hip",
# "Right_hip",
# "Spine1",
# "Left_knee",
# "Right_knee",
# "Spine2",
# "Left_ankle",
# "Right_ankle",
# "Spine3",
# "Left_foot",
# "Right_foot",
# "Neck",
# "Left_collar",
# "Right_collar",
# "Head",
# "Left_shoulder",
# "Right_shoulder",
# "Left_elbow",
# "Right_elbow",
# "Left_wrist",
# "Right_wrist",
# "Left_palm",
# "Right_palm",
# ]
names = [
"Hips",
"LeftUpLeg",
"RightUpLeg",
"Spine",
"LeftLeg",
"RightLeg",
"Spine1",
"LeftFoot",
"RightFoot",
"Spine2",
"LeftToe",
"RightToe",
"Neck",
"LeftShoulder",
"RightShoulder",
"Head",
"LeftArm",
"RightArm",
"LeftForeArm",
"RightForeArm",
"LeftHand",
"RightHand",
"LeftThumb",
"RightThumb",
]
# I prepared smpl models only,
# but I will release for smplx models recently.
model = smplx.create(model_path=model_path,
model_type=model_type,
gender=gender,
batch_size=1)
parents = model.parents.detach().cpu().numpy()
# You can define betas like this.(default betas are 0 at all.)
rest = model(
# betas = torch.randn([1, num_betas], dtype=torch.float32)
)
rest_pose = rest.joints.detach().cpu().numpy().squeeze()[:24,:]
root_offset = rest_pose[0]
offsets = rest_pose - rest_pose[parents]
offsets[0] = root_offset
offsets *= 1
scaling = None
# Pose setting.
if poses.endswith(".npz"):
poses = np.load(poses)
rots = np.squeeze(poses["poses"], axis=0) # (N, 24, 3)
trans = np.squeeze(poses["trans"], axis=0) # (N, 3)
elif poses.endswith(".pkl"):
with open(poses, "rb") as f:
poses = pickle.load(f)
rots = poses["smpl_poses"] # (N, 72)
rots = rots.reshape(rots.shape[0], -1, 3) # (N, 24, 3)
scaling = poses["smpl_scaling"] # (1,)
trans = poses["smpl_trans"] # (N, 3)
else:
raise Exception("This file type is not supported!")
if scaling is not None:
trans /= scaling
# to quaternion
rots = quat.from_axis_angle(rots)
order = "zyx"
pos = offsets[None].repeat(len(rots), axis=0)
positions = pos.copy()
# positions[:,0] += trans * 10
positions[:, 0] += trans
rotations = np.degrees(quat.to_euler(rots, order=order))
bvh_data ={
"rotations": rotations[:, :22],
"positions": positions[:, :22],
"offsets": offsets[:22],
"parents": parents[:22],
"names": names[:22],
"order": order,
"frametime": 1 / fps,
}
if not output.endswith(".bvh"):
output = output + ".bvh"
bvh.save(output, bvh_data)
if mirror:
rots_mirror, trans_mirror = mirror_rot_trans(
rots, trans, names, parents)
positions_mirror = pos.copy()
positions_mirror[:,0] += trans_mirror
rotations_mirror = np.degrees(
quat.to_euler(rots_mirror, order=order))
bvh_data ={
"rotations": rotations_mirror,
"positions": positions_mirror,
"offsets": offsets,
"parents": parents,
"names": names,
"order": order,
"frametime": 1 / fps,
}
output_mirror = output.split(".")[0] + "_mirror.bvh"
bvh.save(output_mirror, bvh_data)
def joints2bvh()
if __name__ == "__main__":
args = parse_args()
smpl2bvh(model_path=args.model_path, model_type=args.model_type,
mirror = args.mirror, gender=args.gender,
poses=args.poses, num_betas=args.num_betas,
fps=args.fps, output=args.output)
print("finished!") |