def load_asf_file(builder, filename): scene_object = SceneObject() scene_object.scene = builder._scene asf_data = parse_asf_file(filename) skeleton = SkeletonBuilder().load_from_asf_data(asf_data) color = get_random_color() builder.create_component("skeleton_vis", scene_object, skeleton, color) motion_vector = MotionVector() motion_vector.frames = [skeleton.get_reduced_reference_frame()] motion_vector.n_frames = 1 scene_object = SceneObject() vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=get_random_color()) animation_controller = SkeletonAnimationController(scene_object) animation_controller.name = filename.split("/")[-1] animation_controller.set_motion(motion_vector) animation_controller.frameTime = 1 animation_controller.set_visualization(vis) scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) builder._scene.addAnimationController(scene_object, "animation_controller") return scene_object
def __init__(self, bvhreader): self.skeleton = SkeletonBuilder().load_from_bvh(bvhreader) self.bvhreader = bvhreader self.quat_frames = [] self.euler_frames = bvhreader.frames self.n_frames = len(self.euler_frames) self.body_plane = None
def load_skeleton_from_json(builder, file_path, scale=1.0): data = load_json_file(file_path) if "skeleton" in data: data = data["skeleton"] #skeleton = SkeletonBuilder().load_from_custom_unity_format(data) skeleton = SkeletonBuilder().load_from_json_data(data) skeleton.scale(scale) motion_vector = MotionVector() motion_vector.frames = [skeleton.get_reduced_reference_frame()] motion_vector.n_frames = 1 scene_object = SceneObject() vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=get_random_color()) animation_controller = SkeletonAnimationController(scene_object) animation_controller.name = file_path.split("/")[-1] animation_controller.set_motion(motion_vector) animation_controller.frameTime = 1 animation_controller.set_visualization(vis) scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) print("load mg json from", file_path) if scene_object is not None: builder._scene.addAnimationController(scene_object, "animation_controller") return scene_object
def get_distgrid(self, ref_motion, test_motion): skeleton = SkeletonBuilder().load_from_bvh(self.ref_bvhreader) n_ref_frames = len(ref_motion['frames']) n_test_frames = len(test_motion['frames']) distgrid = np.zeros([n_test_frames, n_ref_frames]) for i in range(n_test_frames): for j in range(n_ref_frames): distgrid[i, j] = calculate_frame_distance( skeleton, ref_motion['frames'][j], test_motion['frames'][i]) if self.verbose: res = MotionDynamicTimeWarping.calculate_path(distgrid) ref_indices = res[0] test_indices = res[1] shape = (n_test_frames, n_ref_frames) path = self.get_warping_index(test_indices, ref_indices, shape) distgrid = distgrid.T plt.figure() plt.imshow(distgrid) plt.plot(list(range(len(path))), path, color='red') plt.ylabel(ref_motion['filename']) plt.ylim(0, n_ref_frames) plt.xlim(0, n_test_frames) plt.xlabel(test_motion['filename']) plt.title('similarity grid with path') plt.show() return distgrid
def load_motion(path, skeleton_type=None): bvh = BVHReader(path) mv = MotionVector() mv.from_bvh_reader(bvh) skeleton = SkeletonBuilder().load_from_bvh(bvh) skeleton.skeleton_model = load_skeleton_model(skeleton_type) return skeleton, mv
def __init__(self, scene_object, name, data, color=(0, 0, 1)): LegacySkeletonAnimationController.__init__(self, scene_object) self.motion_primitive = MotionStateGraphNode(None) self.skeleton = SkeletonBuilder().load_from_json_data(data["skeleton"]) self.frameTime = self.skeleton.frame_time self._visualizations = [] self.samples = [] self.algorithm_config = DEFAULT_ALGORITHM_CONFIG self.color = color self.motion_primitive._initialize_from_json(convert_to_mgrd_skeleton(self.skeleton), data) print("loaded motion primitive") print("spatial", self.motion_primitive.get_n_spatial_components()) print("time", self.motion_primitive.get_n_time_components()) self.motion_primitive.cluster_tree = None self.training_data = None #print("n gmm", len(self.motion_primitive.get_gaussian_mixture_model().weights)) self.name = name self._regenerate = True self.type = CONTROLLER_TYPE_MP set_log_mode(LOG_MODE_DEBUG) self.start_pose = {"position": [0, 0, 0], "orientation": [0, 0, 0]} self.mock_graph = MockGraph(self.skeleton) self.label_color_map = dict()
def load_skeleton_from_db(db_url, skeleton_name, session=None): skeleton_data = get_skeleton_from_remote_db(db_url, skeleton_name, session) if skeleton_data is not None: skeleton = SkeletonBuilder().load_from_custom_unity_format(skeleton_data) skeleton_model = get_skeleton_model_from_remote_db(db_url, skeleton_name, session) skeleton.skeleton_model = skeleton_model return skeleton
def __init__(self, elementary_action, motion_primitive, data_repo, functional_motion_data, npc, skeleton_json, knots, n_joints): """ :param functional_motion_data (numpy.array<3d> n_samples * n_coeffs * n_dims): each dimension of motion data is represented as a function, the dimension of motion data is: first three channels are Hips' translation, four channels for the orientation of each joint :return: """ self.functional_motion_data = functional_motion_data self.motion_primitive = motion_primitive self.elementary_action = elementary_action self.cartesian_motion_data = convert_quat_functional_data_to_cartesian_functional_data(elementary_action, motion_primitive, data_repo, skeleton_json, functional_motion_data, knots) self.npc = npc self.knots = knots skeleton_bvh = os.path.join(os.path.dirname(__file__), r'../../../skeleton.bvh') bvhreader = BVHReader(skeleton_bvh) self.skeleton = SkeletonBuilder().load_from_bvh(bvhreader) self.skeleton_json = skeleton_json self.data_repo = data_repo self.n_joints = n_joints self.len_weights = self.n_joints + LEN_CARTESIAN self.pca = PCA(n_components=self.npc)
def load_bvh_file(builder, path, scale=1.0, draw_mode=2, offset=None, reference_frame=None, skeleton_model=None, use_clip=False, color=None, visualize=True): bvh_reader = BVHReader(path) bvh_reader.scale(scale) animated_joints = [ key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite") ] o = None if bvh_reader.frames is not None: skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints, reference_frame=reference_frame, skeleton_model=skeleton_model) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) motion_vector.skeleton = skeleton if offset is not None: motion_vector.translate_root(offset) name = path.split("/")[-1] o = builder.create_object("animation_controller", name, skeleton, motion_vector, bvh_reader.frame_time, draw_mode, visualize, color) return o
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 load_motion_from_bvh(filename): bvh_reader = BVHReader(filename) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) animated_joints = list(bvh_reader.get_animated_joints()) motion_vector.skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints=animated_joints) return motion_vector
def load_file(self, filename): bvh_reader = BVHReader(str(self.folder_path) + os.sep + filename) mv = MotionVector() mv.from_bvh_reader(bvh_reader, False) animated_joints = list(bvh_reader.get_animated_joints()) mv.skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints=animated_joints) self.motion_cache[filename] = mv
def load_skeleton(file_path, joint_filter=None, scale=1.0): target_bvh = BVHReader(file_path) bvh_joints = list(target_bvh.get_animated_joints()) if joint_filter is not None: animated_joints = [j for j in bvh_joints if j in joint_filter] else: print("set default joints") animated_joints = bvh_joints skeleton = SkeletonBuilder().load_from_bvh(target_bvh, animated_joints) skeleton.scale(scale) return skeleton
def load_data_for_normalization(self, data_folder): if not data_folder.endswith(os.sep): data_folder += os.sep bvh_files = glob.glob(data_folder + '*.bvh') self.ref_bvh = bvh_files[0] self.ref_bvhreader = BVHReader(self.ref_bvh) self.skeleton = SkeletonBuilder().load_from_bvh(self.ref_bvhreader) for bvh_file_path in bvh_files: bvhreader = BVHReader(bvh_file_path) filename = os.path.split(bvh_file_path)[-1] self.aligned_motions[filename] = bvhreader.frames
def create_motion_vector_from_bvh(bvh_str, animated_joints=None): bvh_reader = get_bvh_from_str(bvh_str) print("loaded motion", bvh_reader.frames.shape) if animated_joints is None: animated_joints = [key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite")] skeleton = SkeletonBuilder().load_from_bvh(bvh_reader, animated_joints) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False, animated_joints) motion_vector.skeleton = skeleton return motion_vector
def load_custom_unity_format_file(builder, filename, draw_mode=2): data = load_json_file(filename) if data is not None: frame_time = data["frameTime"] motion_vector = MotionVector() motion_vector.from_custom_unity_format(data) skeleton = SkeletonBuilder().load_from_json_data(data) o = builder.create_object("animation_controller", filename.split("/")[-1], skeleton, motion_vector, motion_vector.frame_time, draw_mode, visualize, color) return o
def slot_load_asf_str(self): filename = QFileDialog.getOpenFileName(self, 'Open File', '.')[0] filename = str(filename) if os.path.isfile(filename): try: asf_data = parse_asf_file(filename) skeleton = SkeletonBuilder().load_from_asf_data(asf_data) self.data = skeleton.to_unity_format() print("loaded asf string from", filename) except Exception as e: self.data = None print("Could not read file", e.args, filename)
def load_target_skeleton(file_path, scale_factor=1.0): skeleton = None target_bvh = BVHReader(file_path) animated_joints = list(target_bvh.get_animated_joints()) skeleton = SkeletonBuilder().load_from_bvh(target_bvh, animated_joints, add_tool_joints=False) for node in list(skeleton.nodes.values()): node.offset[0] *= scale_factor node.offset[1] *= scale_factor node.offset[2] *= scale_factor return skeleton
def main(): global context, pool port = 8888 target_skeleton_file = MODEL_DATA_DIR + os.sep + "iclone_female4.bvh" skeleton_model = "iclone" target_skeleton_file = None parser = argparse.ArgumentParser( description="Start the MorphableGraphs REST-interface") parser.add_argument( "-set", nargs='+', default=[], help="JSONPath expression, e.g. -set $.model_data=path/to/data") parser.add_argument("-config_file", nargs='?', default=SERVICE_CONFIG_FILE, help="Path to default config file") parser.add_argument("-target_skeleton", nargs='?', default=target_skeleton_file, help="Path to target skeleton file") parser.add_argument("-skeleton_scale", nargs='?', default=1.0, help="Scale applied to the target skeleton offsets") args = parser.parse_args() if os.path.isfile(args.config_file): service_config = load_json_file(args.config_file) algorithm_config_file = "config" + os.sep + service_config[ "algorithm_settings"] + "_algorithm.config" algorithm_config = load_json_file(algorithm_config_file) port = service_config["port"] if args.target_skeleton is not None: # TODO use custom json file instead bvh_reader = BVHReader(args.target_skeleton) animated_joints = list(bvh_reader.get_animated_joints()) target_skeleton = SkeletonBuilder().load_from_bvh( bvh_reader, animated_joints=animated_joints) target_skeleton.skeleton_model = SKELETON_MODELS[skeleton_model] else: target_skeleton = None context = Context(service_config, algorithm_config, target_skeleton) count = cpu_count() print("run {} processes on port {}".format(count, port)) pool = ProcessPoolExecutor(max_workers=count) # configure tornado to work with the asynchio loop tornado.platform.asyncio.AsyncIOMainLoop().install() app.listen(port) asyncio.get_event_loop().run_forever() pool.shutdown()
def slot_load_bvh_str(self): filename = QFileDialog.getOpenFileName(self, 'Open File', '.')[0] filename = str(filename) if os.path.isfile(filename): try: bvh = BVHReader(filename) skeleton = SkeletonBuilder().load_from_bvh( bvh, list(bvh.get_animated_joints())) self.data = skeleton.to_unity_format() print("loaded bvh string from", filename) except Exception as e: self.data = None print("Could not read file", e.args, filename)
def create_cluster_tree_from_model(model_data, n_samples, n_subdivisions_per_level = 4, session=None): skeleton = SkeletonBuilder().load_from_json_data(model_data["skeleton"]) CLUSTERING_METHOD_KMEANS = 0 mp = MotionPrimitiveModelWrapper() mp.cluster_tree = None mp._initialize_from_json(convert_to_mgrd_skeleton(skeleton), model_data) data = mp.sample_low_dimensional_vectors(n_samples) n_spatial = mp.get_n_spatial_components() features = data[:, :n_spatial] options = {"n_subdivisions": n_subdivisions_per_level, "clustering_method": CLUSTERING_METHOD_KMEANS, "use_feature_mean": False} return FeatureClusterTree(features, data, None, options, [])
def load_motion_from_str(builder, bvh_str, name, node_key, motion_id, meta_info_str="", draw_mode=2, visualize=True, color=None): if color is None: color = get_random_color() bvh_reader = get_bvh_from_str(bvh_str) print("loaded motion", bvh_reader.frames.shape) animated_joints = [ key for key in list(bvh_reader.node_names.keys()) if not key.endswith("EndSite") ] skeleton = SkeletonBuilder().load_from_bvh(bvh_reader, animated_joints) motion_vector = MotionVector() motion_vector.from_bvh_reader(bvh_reader, False) motion_vector.skeleton = skeleton motion_vector.scale_root(0.01) o = builder.create_object("animation_controller", name, skeleton, motion_vector, bvh_reader.frame_time, draw_mode, visualize, color) 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] = (node_key, motion_id) if meta_info_str != "": c = o._components["animation_controller"] meta_info = json.loads(meta_info_str) if "sections" in meta_info: sections = meta_info["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) return o
def create_animated_mesh(builder, name, model_data, scale=1, visualize=True): scene_object = SceneObject() scene_object.name = name skeleton_data = model_data["skeleton"] skeleton = SkeletonBuilder().load_from_fbx_data(skeleton_data) skeleton.skeleton_model = dict() skeleton.scale(scale) vis = None if visualize: vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=(0, 1, 0), width_scale=0.1) #vis.box_scale = 0.1 animation_controller = SkeletonAnimationController(scene_object) anim_key = None if "animations" in model_data and len(model_data["animations"]) > 0: for k in model_data["animations"]: if len(model_data["animations"][k]["curves"].keys()) > 0: anim_key = k break anim = model_data["animations"][anim_key]["curves"] clip = create_clip_from_animation(skeleton, anim) clip.scale_root(scale) if anim_key is None: clip = create_clip_from_reference_frame(skeleton) clip.scale_root(scale) animation_controller.name = scene_object.name animation_controller.set_motion(clip) animation_controller.set_visualization(vis, 1) animation_controller.frameTime = skeleton.frame_time scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) if visualize: if len(model_data["mesh_list"]) > 0 and len( model_data["mesh_list"][0]["vertices"]) > 0: builder.create_component("animated_mesh", scene_object, model_data, "animation_controller", scale) return scene_object
def create_animation_controller_from_fbx(builder, name, skeleton_data): skeleton = SkeletonBuilder().load_from_fbx_data(skeleton_data) scene_object = SceneObject() vis = builder.create_component("skeleton_vis", scene_object, skeleton, color=(0, 1, 0)) animation_controller = SkeletonAnimationController(scene_object) animation_controller.name = name animation_controller.currentFrameNumber = 0 animation_controller.frameTime = 0.011333 clip = create_clip_from_reference_frame(skeleton) animation_controller.set_motion(clip) skeleton.reference_frame = clip.frames[0] animation_controller.set_visualization(vis, 2) scene_object.name = animation_controller.name scene_object.add_component("animation_controller", animation_controller) return scene_object
def get_joint_speed(bvhreader, feature_joints): skeleton = SkeletonBuilder().load_from_bvh(bvhreader) left_toe_pos = [] right_toe_pos = [] left_toe_speed = [0] right_toe_speed = [0] for i in range(len(bvhreader.frames)): left_toe_pos.append( get_cartesian_coords(bvhreader, skeleton, feature_joints[0], bvhreader.frames[i])) right_toe_pos.append( get_cartesian_coords(bvhreader, skeleton, feature_joints[1], bvhreader.frames[i])) for i in range(len(bvhreader.frames) - 1): left_toe_speed.append((left_toe_pos[i + 1][0] - left_toe_pos[i][0])**2 + (left_toe_pos[i + 1][2] - left_toe_pos[i][2])**2) right_toe_speed.append( (right_toe_pos[i + 1][0] - right_toe_pos[i][0])**2 + (right_toe_pos[i + 1][2] - right_toe_pos[i][2])**2) return left_toe_speed, right_toe_speed
def normalize_root(self, origin_point): """set the offset of root joint to (0, 0, 0), and shift the motions to original_point, if original_point is None, the set it as (0, 0, 0) """ origin_point = [origin_point['x'], origin_point['y'], origin_point['z']] if self.ref_bvh is not None: self.ref_bvhreader = BVHReader(self.ref_bvh) elif self.bvhreader is not None: self.ref_bvhreader = self.bvhreader else: raise ValueError('No reference BVH file for skeleton information') self.ref_bvhreader.node_names['Hips']['offset'] = [0, 0, 0] skeleton = SkeletonBuilder().load_from_bvh(self.ref_bvhreader) for filename, frames in self.aligned_motions.items(): height_1 = get_cartesian_coordinates_from_euler_full_skeleton(self.ref_bvhreader, skeleton, 'Bip01_R_Toe0', frames[0])[1] height_2 = get_cartesian_coordinates_from_euler_full_skeleton(self.ref_bvhreader, skeleton, 'Bip01_L_Toe0', frames[0])[1] height_3 = get_cartesian_coordinates_from_euler_full_skeleton(self.ref_bvhreader, skeleton, 'Bip01_R_Toe0', frames[-1])[1] height_4 = get_cartesian_coordinates_from_euler_full_skeleton(self.ref_bvhreader, skeleton, 'Bip01_L_Toe0', frames[-1])[1] height_offset = (height_1 + height_2 + height_3 + height_4)/4.0 self.aligned_motions[filename] = self.translate_to_original_point( frames, origin_point, height_offset)
def load_skeleton(filename): bvh = BVHReader(filename) skeleton = SkeletonBuilder().load_from_bvh(bvh) return skeleton
def load_from_file(self, file_name): bvh = BVHReader(file_name) self.skeleton = SkeletonBuilder().load_from_bvh(bvh)
def splitt_motion(frames, keyframes, mname, skeleton_file='skeleton.bvh', outputpath=''): """ Splitt a Motion by the given Keyframes Parameters ---------- frames : numpy.ndarray The frames of the walking motion keyframes : dict of list of int A dictionary containing a list for each feature. Each list contains all Keyframes for this feature. mname: string Subfix of the splitted motions (i.e. the original name of the motion) skeleton_file: string (optional) The path to the skeleton file. Default is the 'skeleton.bvh' in the current folder outputpath: string (optional) The path where to save the motions. Default is the current folder Returns ------- None """ # Calc number of steps for status update n = 0.0 counter = 0.0 for feature in keyframes: n += len(keyframes[feature]) tmpmins = [] tmpmax = [] for feature in keyframes: tmpmins.append(min(keyframes[feature])[0]) tmpmax.append(max(keyframes[feature])[1]) firstframe = min(tmpmins) lastframe = max(tmpmax) reader = BVHReader(skeleton_file) skel = SkeletonBuilder().load_from_bvh(reader) for feature in keyframes: # save first step: if firstframe in keyframes[feature][0]: keyframe = keyframes[feature][0] subframes = frames[keyframe[0]:keyframe[1]] name = 'begin_' + str(keyframe[0]) + '_' + str(keyframe[1]) \ + '_' + feature + '_' + mname BVHWriter(outputpath + os.sep + name, skel, subframes, 0.013889) keyframes[feature] = keyframes[feature][1:] # last step: if lastframe in keyframes[feature][-1]: keyframe = keyframes[feature][-1] subframes = frames[keyframe[0]:keyframe[1]] name = 'end_' + str(keyframe[0]) + '_' + str(keyframe[1]) \ + '_' + feature + '_' + mname BVHWriter(outputpath + os.sep + name, skel, subframes, 0.013889) keyframes[feature] = keyframes[feature][:-1] for keyframe in keyframes[feature]: subframes = frames[keyframe[0]:keyframe[1]] name = str(keyframe[0]) + '_' + str(keyframe[1]) \ + '_' + feature + '_' + mname BVHWriter(outputpath + os.sep + name, skel, subframes, 0.013889) counter += 1.0
def load_skeleton(path, skeleton_type=None): bvh = BVHReader(path) skeleton = SkeletonBuilder().load_from_bvh(bvh) skeleton.skeleton_model = load_skeleton_model(skeleton_type) return skeleton