def create_motion_model_in_db(db_url, skeleton_name, c_id, c_name, spline_basis_factor, animated_joints=None, session=None): motion_data = get_motion_vectors(db_url, c_id, skeleton_name, is_aligned=1) skeleton = load_skeleton_from_db(db_url, skeleton_name) n_motions = len(motion_data) if n_motions > 1: print("start modeling", n_motions, spline_basis_factor) config = get_standard_config() config["n_spatial_basis_factor"] = spline_basis_factor if animated_joints is None: animated_joints = skeleton.animated_joints model_data = create_motion_primitive_model(c_name, skeleton, motion_data, config, animated_joints, save_skeleton=True, align_frames=False) print("finished modelling") name = c_name+"_"+skeleton_name+"_"+str(n_motions) upload_motion_model_to_remote_db(db_url, name, c_id, skeleton_name, model_data, config, session) print("uploaded model") elif n_motions == 1: print("Create static motion primitive fromn 1 motion") first_key = list(motion_data.keys())[0] motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data[first_key]["data"]) config = get_standard_config() n_basis = int(config["n_spatial_basis_factor"]*motion_vector.n_frames) name = c_name+"_"+skeleton_name+"_"+str(n_motions) model_data = convert_motion_to_static_motion_primitive(name, motion_vector.frames, skeleton, n_basis=n_basis, degree=3) upload_motion_model_to_remote_db(db_url, name, c_id, skeleton_name, model_data, config, session) print("uploaded model") else: print("No motion, found")
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 generate_training_data(motion_data, animated_joints=None): motions = collections.OrderedDict() sections = collections.OrderedDict() temporal_data = collections.OrderedDict() for name, value in motion_data.items(): data = value["data"] motion_vector = MotionVector() motion_vector.from_custom_db_format(data) motions[name] = motion_vector.frames if value["section_annotation"] is not None:# v_type = type(value["section_annotation"]) if v_type == list: sections[name] = value["section_annotation"] elif v_type == dict: sections[name] = list()#create_sections_from_annotation(annotation) print(value["section_annotation"]) for section_key in value["section_annotation"]: n_sections = len(value["section_annotation"][section_key]) if n_sections == 1: # take only the first segment in the list sections[name].append(value["section_annotation"][section_key][0]) else: warnings.warn("number of annotations "+str(section_key)+" "+str(n_sections)) else: warnings.warn("type unknown", name, v_type) if value["time_function"] is not None: temporal_data[name] = value["time_function"] return motions, sections, temporal_data
def load_motion_from_json(builder, skeleton_data, motion_data, name, collection_id, motion_id, meta_data_str="", skeleton_model=None, is_processed=False, draw_mode=2, visualize=True, color=None, visible=True): if color is None: color = get_random_color() skeleton = SkeletonBuilder().load_from_custom_unity_format(skeleton_data) skeleton.skeleton_model = skeleton_model motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data) motion_vector.skeleton = skeleton skeleton.frame_time = motion_vector.frame_time #motion_vector.scale_root(scale) o = builder.create_object("animation_controller", name, skeleton, motion_vector, motion_vector.frame_time, draw_mode, visualize, color) o.visible = visible if "data_base_ids" not in builder._scene.internal_vars: builder._scene.internal_vars["data_base_ids"] = dict() builder._scene.internal_vars["data_base_ids"][o.node_id] = (collection_id, motion_id, is_processed) if meta_data_str != "": c = o._components["animation_controller"] meta_data = json.loads(meta_data_str) if "sections" in meta_data: sections = meta_data["sections"] print("sections", sections) sections = meta_data["sections"] if type(sections) == list: semantic_annotation = create_annotation_from_sections_list( sections, motion_vector.n_frames) else: semantic_annotation = create_annotation_from_sections_dict( sections, motion_vector.n_frames) color_map = dict() for key in semantic_annotation.keys(): color_map[key] = get_random_color() c.set_color_annotation(semantic_annotation, color_map) if "time_function" in meta_data: print("set time_function") time_function = meta_data["time_function"] print(meta_data["time_function"]) c.set_time_function(time_function) else: print("meta_data", meta_data) return o
def create_motion_vector_from_json(motion_data): motion_vector = MotionVector() motion_vector.from_custom_db_format(motion_data) return motion_vector