def loads(s):
    bvhlib = BVHLIB.Bvh(s)
    root = parse_bvh_node(bvhlib.get_joints()[0])
    return (
        root,
        [[float(f) for f in frame] for frame in bvhlib.frames],
        bvhlib.frame_time,
    )
示例#2
0
def parse_imu_bone_info(fpath):
    with open(fpath, 'r') as f:
        mocap = bvh.Bvh(f.read())
    all_joints = mocap.get_joints()
    joints_names = mocap.get_joints_names()

    bone_info = dict()
    for b in bone_names:
        start = imu_bone_vicon_start[b]
        end = imu_bone_vicon_end[b]
        if start == end:
            this_joint = all_joints[mocap.get_joint_index(start)]
            bone_length = tuple(
                float(x) for x in this_joint.children[2].children[0].value[1:])
        else:
            bone_length = mocap.joint_offset(end)
        bone_info[b] = (start, end, bone_length)
    return bone_info
示例#3
0
def load_file(file_name):
    with open(file_name) as f:
        mocap = bvh.Bvh(f.read())

    FRAMES = mocap.nframes
    FRAME_TIME = mocap.frame_time

    joint_names = mocap.get_joints_names()

    JOINT_COUNT = joint_names.__len__()

    joint_channels = []
    joint_offsets = []
    joint_channels = []
    frame_joint_channels = []
    frame_joint_quaternions = []
    frame_positions = []

    for joint_name in joint_names:
        joint_channels.append(mocap.joint_channels(joint_name))
        joint_offsets.append(mocap.joint_offset(joint_name))

    # joint_id parent_id name offset
    dependency = []
    for joint_name in joint_names:
        currRow = []
        joint_idx = joint_names.index(joint_name)
        currRow.append(joint_idx)
        if (joint_idx == 0):
            currRow.append(-1)
        else:
            currRow.append(joint_names.index(mocap.get_joint(joint_name).parent.name))
        # currRow.append(joint_name)
        for coord in joint_offsets[joint_idx]:
            currRow.append(coord)
        dependency.append(currRow)

    # rotations local_positions global_positions(wrt root)

    frames = np.array(mocap.frames).astype(float)
    root_positions = frames[:, 0:3]
    euler_rotations = frames[:, 3:]

    dependencies = np.array(dependency)

    offsets = dependencies[:, -3:]
    zipped_euler_rotations = []
    zipped_local_rotations = []
    zipped_local_positions = []
    zipped_global_positions = []
    zipped_global_rotations = []
    zipped_local_quat_rotations = []
    zipped_global_quat_rotations = []

    for curr_joint in range(JOINT_COUNT):
        start_idx = 3 * curr_joint
        end_idx = 3 * (curr_joint + 1)
        curr_joint_euler_rots = euler_rotations[:, start_idx:end_idx]
        flipped_euler_rotations = np.flip(curr_joint_euler_rots, 1)
        curr_local_rotations = R.from_euler('xyz', flipped_euler_rotations, degrees=True)
        curr_joint_offset = np.array(offsets[curr_joint])
        curr_local_positions = curr_local_rotations.apply(curr_joint_offset)

        if (curr_joint == 0):
            curr_global_rotations = curr_local_rotations
            curr_global_positions = root_positions
        else:
            curr_parent_rotation = zipped_global_rotations[dependencies[curr_joint, -4].astype(int)];
            curr_global_rotations = curr_parent_rotation * curr_local_rotations
            curr_global_positions = curr_parent_rotation.apply(curr_joint_offset) + zipped_global_positions[
                dependencies[curr_joint, -4].astype(int)]

        zipped_euler_rotations.append(curr_joint_euler_rots)
        zipped_local_rotations.append(curr_local_rotations)
        zipped_local_positions.append(curr_local_positions)
        zipped_global_positions.append(curr_global_positions)
        zipped_global_rotations.append(curr_global_rotations)
        zipped_local_quat_rotations.append(curr_local_rotations.as_quat())
        zipped_global_quat_rotations.append(curr_global_rotations.as_quat())

    #print("lul")
    zipped_global_positions = np.moveaxis(np.array(zipped_global_positions), 0, 1)
    zipped_local_positions = np.moveaxis(np.array(zipped_local_positions), 0, 1)
    zipped_global_quat_rotations = np.moveaxis(np.array(zipped_global_quat_rotations), 0, 1)
    zipped_local_quat_rotations = np.moveaxis(np.array(zipped_local_quat_rotations), 0, 1)
    return FRAMES, FRAME_TIME, joint_names, dependencies[:, :2].astype('int'), zipped_global_quat_rotations, zipped_local_quat_rotations, zipped_global_positions, zipped_local_positions
示例#4
0
template_file = 'bvh_template1.yaml'

job_folder = './job/'

import glob
import os
import bvh

bvh_files = glob.glob('../data/bvh/*.bvh')

class SafeDict(dict):
    def __missing__(self, key):
        return '{' + key + '}'

template = open(template_file).read()
for bvh_file in bvh_files:
# bvh_file = '../data/bvh/embrace.bvh'
    act_name = os.path.basename(bvh_file).replace('.bvh', '')
    print(bvh_file)
    bvh_data = bvh.Bvh(open(bvh_file).read())
    print(bvh_data.nframes)
    
    num_frame = bvh_data.nframes

    job_filename = os.path.join(job_folder, '{act_name}.yaml'.format(**locals()))
    with open(job_filename, 'w') as f:
        kv = SafeDict(locals())
        f.write(template.format_map(kv))