Esempio n. 1
0
def load(file, motion=None, scale=1.0, load_skel=True, load_motion=True):
    if load_skel:
        tmp_joints = parse_asf(file)

        # convert format
        joints = []
        parent_joints = []
        for k, v in tmp_joints.items():
            joint = motion_class.Joint(name=k)
            if v.parent_joint is None:
                joint.info["dof"] = 6
                parent_joint = None
                joint.xform_from_parent_joint = conversions.p2T(np.zeros(3))
            else:
                joint.info["dof"] = 3
                parent_joint = v.parent_joint
                joint.xform_from_parent_joint = conversions.p2T(
                    v.direction.squeeze() * v.length)
            joints.append(joint)
            parent_joints.append(parent_joint)

        skel = motion_class.Skeleton()
        for i in range(len(joints)):
            skel.add_joint(joints[i], parent_joints[i])

        if load_motion:
            return parse_amc(motion, tmp_joints, skel)
        return skel
    raise NotImplementedError
Esempio n. 2
0
def create_skeleton_from_amass_bodymodel(bm, betas, num_joints, joint_names):
    pose_body_zeros = torch.zeros((1, 3 * (num_joints - 1)))
    body = bm(pose_body=pose_body_zeros, betas=betas)
    base_position = body.Jtr.detach().numpy()[0, 0:num_joints]
    parents = bm.kintree_table[0].long()[:num_joints]

    joints = []
    for i in range(num_joints):
        joint = motion_class.Joint(name=joint_names[i])
        if i == 0:
            joint.info["dof"] = 6
            joint.xform_from_parent_joint = conversions.p2T(np.zeros(3))
        else:
            joint.info["dof"] = 3
            joint.xform_from_parent_joint = conversions.p2T(
                base_position[i] - base_position[parents[i]])
        joints.append(joint)

    parent_joints = []
    for i in range(num_joints):
        parent_joint = None if parents[i] < 0 else joints[parents[i]]
        parent_joints.append(parent_joint)

    skel = motion_class.Skeleton()
    for i in range(num_joints):
        skel.add_joint(joints[i], parent_joints[i])

    return skel
Esempio n. 3
0
def load(
    file,
    motion=None,
    scale=1.0,
    load_skel=True,
    load_motion=True,
    v_up_skel=np.array([0.0, 1.0, 0.0]),
    v_face_skel=np.array([0.0, 0.0, 1.0]),
    v_up_env=np.array([0.0, 1.0, 0.0]),
):
    if not motion:
        motion = motion_class.Motion(fps=60)

    if load_skel:
        skel = motion_class.Skeleton(
            v_up=v_up_skel, v_face=v_face_skel, v_up_env=v_up_env,
        )
        smpl_offsets = np.zeros([24, 3])
        smpl_offsets[0] = OFFSETS[0]
        for idx, pid in enumerate(SMPL_PARENTS[1:]):
            smpl_offsets[idx + 1] = OFFSETS[idx + 1] - OFFSETS[pid]
        for joint_name, parent_joint, offset in zip(
            SMPL_JOINTS, SMPL_PARENTS, smpl_offsets
        ):
            joint = motion_class.Joint(name=joint_name)
            if parent_joint == -1:
                parent_joint_name = None
                joint.info["dof"] = 6  # root joint is free
                offset -= offset
            else:
                parent_joint_name = SMPL_JOINTS[parent_joint]
            offset = offset / np.linalg.norm(smpl_offsets[4])
            T1 = conversions.p2T(scale * offset)
            joint.xform_from_parent_joint = T1
            skel.add_joint(joint, parent_joint_name)
        motion.skel = skel
    else:
        assert motion.skel is not None

    if load_motion:
        assert motion.skel is not None
        # Assume 60fps
        motion.fps = 60.0
        dt = float(1 / motion.fps)
        with open(file, "rb") as f:
            data = pkl.load(f, encoding="latin1")
            poses = np.array(data["poses"])  # shape (seq_length, 135)
            assert len(poses) > 0, "file is empty"
            poses = poses.reshape((-1, len(SMPL_MAJOR_JOINTS), 3, 3))

            for pose_id, pose in enumerate(poses):
                pose_data = [
                    constants.eye_T() for _ in range(len(SMPL_JOINTS))
                ]
                major_joint_id = 0
                for joint_id, joint_name in enumerate(SMPL_JOINTS):
                    if joint_id in SMPL_MAJOR_JOINTS:
                        pose_data[
                            motion.skel.get_index_joint(joint_name)
                        ] = conversions.R2T(pose[major_joint_id])
                        major_joint_id += 1
                motion.add_one_frame(pose_id * dt, pose_data)

    return motion
Esempio n. 4
0
def load(
        file,
        motion=None,
        scale=1.0,
        load_skel=True,
        load_motion=True,
        v_up_skel=np.array([0.0, 1.0, 0.0]),
        v_face_skel=np.array([0.0, 0.0, 1.0]),
        v_up_env=np.array([0.0, 1.0, 0.0]),
):
    if not motion:
        motion = motion_classes.Motion()
    words = None
    with open(file, "rb") as f:
        words = [word.decode() for line in f for word in line.split()]
        f.close()
    assert words is not None and len(words) > 0
    cnt = 0
    total_depth = 0
    joint_stack = [None, None]
    joint_list = []
    parent_joint_list = []

    if load_skel:
        assert motion.skel is None
        motion.skel = motion_classes.Skeleton(
            v_up=v_up_skel,
            v_face=v_face_skel,
            v_up_env=v_up_env,
        )

    if load_skel:
        while cnt < len(words):
            # joint_prev = joint_stack[-2]
            joint_cur = joint_stack[-1]
            word = words[cnt].lower()
            if word == "root" or word == "joint":
                parent_joint_list.append(joint_cur)
                name = words[cnt + 1]
                joint = motion_classes.Joint(name=name)
                joint_stack.append(joint)
                joint_list.append(joint)
                cnt += 2
            elif word == "offset":
                x, y, z = (
                    float(words[cnt + 1]),
                    float(words[cnt + 2]),
                    float(words[cnt + 3]),
                )
                T1 = conversions.p2T(scale * np.array([x, y, z]))
                joint_cur.xform_from_parent_joint = T1
                cnt += 4
            elif word == "channels":
                ndofs = int(words[cnt + 1])
                if ndofs == 6:
                    joint_cur.info["type"] = "free"
                elif ndofs == 3:
                    joint_cur.info["type"] = "ball"
                elif ndofs == 1:
                    joint_cur.info["type"] = "revolute"
                else:
                    raise Exception("Undefined")
                joint_cur.info["dof"] = ndofs
                joint_cur.info["bvh_channels"] = []
                for i in range(ndofs):
                    joint_cur.info["bvh_channels"].append(words[cnt + 2 +
                                                                i].lower())
                cnt += ndofs + 2
            elif word == "end":
                joint_dummy = motion_classes.Joint(name="END")
                joint_stack.append(joint_dummy)
                cnt += 2
            elif word == "{":
                total_depth += 1
                cnt += 1
            elif word == "}":
                joint_stack.pop()
                total_depth -= 1
                cnt += 1
                if total_depth == 0:
                    for i in range(len(joint_list)):
                        motion.skel.add_joint(
                            joint_list[i],
                            parent_joint_list[i],
                        )
                    break
            elif word == "hierarchy":
                cnt += 1
            else:
                raise Exception(f"Unknown Token {word} at token {cnt}")

    if load_motion:
        assert motion.skel is not None
        assert np.allclose(motion.skel.v_up, v_up_skel)
        assert np.allclose(motion.skel.v_face, v_face_skel)
        assert np.allclose(motion.skel.v_up_env, v_up_env)
        while cnt < len(words):
            word = words[cnt].lower()
            if word == "motion":
                num_frames = int(words[cnt + 2])
                dt = float(words[cnt + 5])
                motion.set_fps(round(1 / dt))
                cnt += 6
                t = 0.0
                range_num_dofs = range(motion.skel.num_dofs)
                positions = np.zeros(
                    (num_frames, motion.skel.num_joints(), 3, 3))
                rotations = np.zeros((num_frames, motion.skel.num_joints(), 3))
                T = np.zeros((num_frames, motion.skel.num_joints(), 4, 4))
                T[...] = constants.eye_T()
                position_channels = {
                    "xposition": 0,
                    "yposition": 1,
                    "zposition": 2,
                }
                rotation_channels = {
                    "xrotation": 0,
                    "yrotation": 1,
                    "zrotation": 2,
                }
                for frame_idx in range(num_frames):
                    # if frame_idx == 1:
                    #     break
                    raw_values = [
                        float(words[cnt + j]) for j in range_num_dofs
                    ]
                    cnt += motion.skel.num_dofs
                    cnt_channel = 0
                    for joint_idx, joint in enumerate(motion.skel.joints):
                        for channel in joint.info["bvh_channels"]:
                            value = raw_values[cnt_channel]
                            if channel in position_channels:
                                value = scale * value
                                positions[frame_idx][joint_idx][
                                    position_channels[channel]][
                                        position_channels[channel]] = value
                            elif channel in rotation_channels:
                                value = conversions.deg2rad(value)
                                rotations[frame_idx][joint_idx][
                                    rotation_channels[channel]] = value
                            else:
                                raise Exception("Unknown Channel")
                            cnt_channel += 1

                for joint_idx, joint in enumerate(motion.skel.joints):
                    for channel in joint.info["bvh_channels"]:
                        if channel in position_channels:
                            T[:,
                              joint_idx] = T[:, joint_idx] @ conversions.p2T(
                                  positions[:, joint_idx,
                                            position_channels[channel], :, ])
                        elif channel == "xrotation":
                            T[:,
                              joint_idx] = T[:, joint_idx] @ conversions.R2T(
                                  conversions.Ax2R(
                                      rotations[:, joint_idx,
                                                rotation_channels[channel], ]))
                        elif channel == "yrotation":
                            T[:,
                              joint_idx] = T[:, joint_idx] @ conversions.R2T(
                                  conversions.Ay2R(
                                      rotations[:, joint_idx,
                                                rotation_channels[channel], ]))
                        elif channel == "zrotation":
                            T[:,
                              joint_idx] = T[:, joint_idx] @ conversions.R2T(
                                  conversions.Az2R(
                                      rotations[:, joint_idx,
                                                rotation_channels[channel], ]))

                for i in range(num_frames):
                    motion.add_one_frame(list(T[i]))
                    t += dt
            else:
                cnt += 1
        assert motion.num_frames() > 0
    return motion