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 _backprojection(self, ld_vec, filename=None): """Back project a low dimensional spatial parameter to motion """ eigenVectors = np.array(self._spatial_eigenvectors) backprojected_vector = np.dot(np.transpose(eigenVectors), ld_vec.T) backprojected_vector = np.ravel(backprojected_vector) backprojected_vector += self._mean_motion # reshape motion vector as a 2d array n_frames * n_dim assert len(backprojected_vector) == self._n_dim_spatial * \ self._n_frames, ( 'the length of back projected motion vector is not correct!') if filename is None: filename = 'sample.bvh' else: filename = filename frames = np.reshape(backprojected_vector, (self._n_frames, self._n_dim_spatial)) # rescale root position for each frame for i in range(self._n_frames): frames[i, 0] = frames[i, 0] * self._scale_vec[0] frames[i, 1] = frames[i, 1] * self._scale_vec[1] frames[i, 2] = frames[i, 2] * self._scale_vec[2] skeleton = os.sep.join(('lib', 'skeleton.bvh')) reader = BVHReader(skeleton) BVHWriter(filename, reader, frames, frame_time=0.013889, is_quaternion=True)
def load_motion_vector_from_bvh_file(bvh_file_path, animated_joints): bvh_data = BVHReader(bvh_file_path) mv = MotionVector(None) mv.from_bvh_reader(bvh_data, filter_joints=False, animated_joints=animated_joints) return mv
def _build_from_zip_file(self, ms_graph): zip_path = self.motion_state_graph_path + ".zip" zip_reader = ZipReader(zip_path, pickle_objects=True) graph_data = zip_reader.get_graph_data() self.pfnn_data = zip_reader.get_pfnn_data() if SKELETON_BVH_STRING_KEY in graph_data.keys(): bvh_reader = BVHReader("").init_from_string( graph_data[SKELETON_BVH_STRING_KEY]) ms_graph.skeleton = SkeletonBuilder().load_from_bvh(bvh_reader) elif SKELETON_JSON_KEY in graph_data.keys(): #use_all_joints = False if self.use_all_joints and "animated_joints" in graph_data[ SKELETON_JSON_KEY]: del graph_data[SKELETON_JSON_KEY]["animated_joints"] ms_graph.skeleton = SkeletonBuilder().load_from_json_data( graph_data[SKELETON_JSON_KEY], use_all_joints=self.use_all_joints) print("load skeleton", ms_graph.skeleton.animated_joints) else: raise Exception("There is no skeleton defined in the graph file") return ms_graph.animated_joints = ms_graph.skeleton.animated_joints ms_graph.mgrd_skeleton = convert_to_mgrd_skeleton(ms_graph.skeleton) transition_dict = graph_data["transitions"] actions = graph_data["subgraphs"] for action_name in actions.keys(): node_group = self.build_node_group_from_dict( actions[action_name], ms_graph) ms_graph.nodes.update(node_group.nodes) ms_graph.node_groups[node_group.ea_name] = node_group if action_name == "walk" and len(node_group.idle_states) > 0: idle_mp = node_group.idle_states[0] ms_graph.start_node = (action_name, idle_mp) self._set_transitions_from_dict(ms_graph, transition_dict) self._update_motion_state_stats(ms_graph, recalculate=False) if "hand_pose_info" in graph_data: ms_graph.hand_pose_generator = HandPoseGenerator(ms_graph.skeleton) ms_graph.hand_pose_generator.init_from_desc( graph_data["hand_pose_info"]) if "actionDefinitions" in graph_data: ms_graph.action_definitions = graph_data["actionDefinitions"] if "startNode" in graph_data: start_node = list(graph_data["startNode"]) if start_node[1].startswith("walk"): start_node[1] = start_node[1][5:] ms_graph.start_node = tuple(start_node)
def load_clips(data_path): print("load clips", data_path) loaded_clips = dict() for filepath in glob.glob(data_path+os.sep+"*.bvh"): print("load", filepath) name = filepath.split(os.sep)[-1][:-4] bvh = BVHReader(filepath) mv = MotionVector() mv.from_bvh_reader(bvh, True) state = MotionState(mv) state.play = True print("load", name, mv.n_frames) loaded_clips[name] = state return loaded_clips
def __init__(self, motion_primitive_file, skeleton_file): self.motion_primitive_model = MotionPrimitive(motion_primitive_file) bvhreader = BVHReader(skeleton_file) self.skeleton = Skeleton(bvhreader) self.low_dimension_vectors = [] self.feature_points = [] self.orientations = [] self.feature_point = None self.threshold = None self.root_pos = [] self.root_orientation = [] self.root_rot_angles = [] self.feature_point_dist = None self.root_feature_dist = None self.root_feature_dist_type = None
class MGRDSkeletonBVHLoader(object): """ Load a Skeleton from a BVH file. Attributes: file (string): path to the bvh file """ def __init__(self, file): self.file = file self.bvh = None def load(self): self.bvh = BVHReader(self.file) root = self.create_root() self.populate(root) return MGRDSkeleton(root) def create_root(self): return self.create_node(self.bvh.root, None) def create_node(self, name, parent): node_data = self.bvh.node_names[name] offset = node_data["offset"] if "channels" in node_data: angle_channels = ["Xrotation", "Yrotation", "Zrotation"] angles_for_all_frames = self.bvh.get_angles([(name, ch) for ch in angle_channels]) orientation = euler_to_quaternion(angles_for_all_frames[0]) else: orientation = euler_to_quaternion([0, 0, 0]) return MGRDSkeletonNode(name, parent, offset, orientation) def populate(self, node): node_data = self.bvh.node_names[node.name] if "children" not in node_data: return for child in node_data["children"]: child_node = self.create_node(child, node) node.add_child(child_node) self.populate(child_node)
def _cut_one_file(self, input_file, start_frame, end_frame): self.bvhreader = BVHReader(input_file) new_frames = self.bvhreader.frames[start_frame:end_frame] return new_frames
def load(self): self.bvh = BVHReader(self.file) root = self.create_root() self.populate(root) return MGRDSkeleton(root)
def load_skeleton(self, skeleton_path): bvh = BVHReader(skeleton_path) self.animated_joints = list(bvh.get_animated_joints()) if has_mgrd: self.mgrd_skeleton = MGRDSkeletonBVHLoader(skeleton_path).load() self.skeleton = SkeletonBuilder().load_from_bvh(BVHReader(skeleton_path), self.animated_joints)
def get_bvh_from_str(bvh_str): bvh_reader = BVHReader("") lines = bvh_str.split('\\n') lines = [l for l in lines if len(l) > 0] bvh_reader.process_lines(lines) return bvh_reader