def get_bvh_data_from_motion_list(db_url, motion_list, is_processed=False, session=None):
    count = 1
    n_motions = len(motion_list)
    bvh_data = dict()
    for node_id, name in motion_list:
        print("download motion", str(count)+"/"+str(n_motions), node_id, name)
        bvh_str = get_bvh_str_by_id_from_remote_db(db_url, node_id, is_processed, )
        annotation_str = get_annotation_by_id_from_remote_db(db_url, node_id, is_processed, session)
        section_annotation = None
        if annotation_str != "": 
            data = json.loads(annotation_str)
            section_annotation = data["sections"]
        
        time_function_str = get_time_function_by_id_from_remote_db(db_url, node_id)
        time_function = None
        if time_function_str != "": 
            time_function = json.loads(time_function_str)
            #print("found str", node_id, name, type(time_function),  time_function_str)
            print(time_function)
            if isinstance(time_function, str) and time_function != "":
                time_function = json.loads(time_function)
                print("WARINING: time function was deserialized to string instead of list", type(time_function))
                

        bvh_data[node_id] = dict()
        bvh_data[node_id]["bvh_str"] = bvh_str
        bvh_data[node_id]["section_annotation"] = section_annotation
        bvh_data[node_id]["time_function"] = time_function
        bvh_data[node_id]["name"] = name
        count+=1
    return bvh_data
Example #2
0
def retarget_motion_in_db(db_url,
                          retargeting,
                          motion_id,
                          motion_name,
                          collection,
                          skeleton_model_name,
                          is_aligned=False,
                          session=None):
    motion_data = get_motion_by_id_from_remote_db(db_url,
                                                  motion_id,
                                                  is_processed=is_aligned)
    if motion_data is None:
        print("Error: motion data is empty")
        return

    meta_info_str = get_annotation_by_id_from_remote_db(
        db_url, motion_id, is_processed=is_aligned)
    motion_vector = MotionVector()
    motion_vector.from_custom_db_format(motion_data)
    motion_vector.skeleton = retargeting.src_skeleton
    new_frames = retargeting.run(motion_vector.frames, frame_range=None)
    target_motion = MotionVector()
    target_motion.frames = new_frames
    target_motion.skeleton = retargeting.target_skeleton
    target_motion.frame_time = motion_vector.frame_time
    target_motion.n_frames = len(new_frames)
    m_data = target_motion.to_db_format()
    upload_motion_to_db(db_url,
                        motion_name,
                        m_data,
                        collection,
                        skeleton_model_name,
                        meta_info_str,
                        is_processed=is_aligned,
                        session=session)
def align_motions_in_db(db_url, skeleton_name, c_id, session=None):
    motion_data = get_motion_vectors(db_url, c_id, skeleton_name, is_aligned=0)
    # delete old data
    old_aligned_motions = get_motion_list_from_remote_db(db_url, c_id, skeleton_name, is_processed=True)
    for motion in old_aligned_motions:
        delete_motion_by_id_from_remote_db(db_url, motion[0], is_processed=True, session=session)
    skeleton = load_skeleton_from_db(db_url, skeleton_name)

    n_motions = len(motion_data)
    if n_motions > 1:
        print("start alignment", n_motions)
        aligned_data = align_motion_data(skeleton, motion_data)
        print("finished alignment")
        for key, data in aligned_data.items():
            frames = data["frames"]
            name = motion_data[key]["name"] + "_aligned"
            mv = get_motion_vector(skeleton, frames)
            m_data = mv.to_db_format()
            try:
                meta_data = json.loads(data["meta_info"])
            except:
                meta_data = dict()
            meta_data["time_function"] = data["time_function"]
            meta_data_str = json.dumps(meta_data)
            upload_motion_to_db(db_url, name, m_data, c_id, skeleton_name, meta_data_str, is_processed=True, session=session)
        print("uploaded aligned data")
    elif n_motions == 1:
        
        first_key = list(motion_data.keys())[0]
        name = motion_data[first_key]["name"] + "_aligned"
        mdata = motion_data[first_key]["data"]
        meta_data_str = get_annotation_by_id_from_remote_db(db_url, first_key)
        try:
            meta_data = json.loads(meta_data_str)
        except:
            meta_data = dict()
        print("process",name)
        motion = create_motion_vector_from_json(mdata)
        time_function = list(range(motion.n_frames))
        meta_data["time_function"] = time_function
        meta_data_str = json.dumps(meta_data)
        upload_motion_to_db(db_url, name, mdata, c_id, skeleton_name, meta_data_str, is_processed=True, session=session)
        print("Need more than 1 motion, found", n_motions)
    else:
        print("No motions found")