Example #1
0
def gen_walk_annotation(elementary_action, motion_primitive):

    aligned_data_folder = get_aligned_data_folder(elementary_action,
                                                  motion_primitive)
    if 'rightstance' in motion_primitive.lower():

        motion_primitive_model = 'rightStance'
    elif 'leftstance' in motion_primitive.lower():
        motion_primitive_model = 'leftStance'
    elif 'sidestep' in motion_primitive.lower():
        motion_primitive_model = 'sideStep'
    elif 'turnleft' in motion_primitive.lower():
        motion_primitive_model = 'rightStance'
    elif 'turnright' in motion_primitive.lower():
        motion_primitive_model = 'leftStance'
    else:
        raise KeyError('unknown motion type!')
    feature_points = ['Bip01_L_Toe0', 'Bip01_R_Toe0']
    annotation_data = {}
    aligned_bvhfiles = glob.glob(aligned_data_folder + os.sep + '*.bvh')
    for item in aligned_bvhfiles:
        filename = os.path.split(item)[-1]
        anno_vec = gen_foot_contact_annotation(item, feature_points, motion_primitive_model)
        annotation_data[filename] = anno_vec
    output_filename = aligned_data_folder + os.sep + elementary_action + '_' + motion_primitive + '_semantic_annotation.json'
    semantic_annotation = {'annotation_list': ['LeftFootContact', 'RightFootContact', 'start', 'end'],
                           'data': annotation_data}
    # with open(output_filename, 'w') as outfile:
    #     json.dump(semantic_annotation, outfile)
    write_to_json_file(output_filename, semantic_annotation)
 def save_updated_meta_info(self):
     """ Save updated meta data to a json file
     """
     if self.meta_information is not None and self.ea_directory is not None:
         path = self.ea_directory + os.sep + META_INFORMATION_FILE_NAME
         write_to_json_file(
             path, self._convert_tuples_to_strings(self.meta_information))
     return
Example #3
0
 def dump_motion_data(self, filename="temp_result.json"):
     dump_data = dict()
     dump_data["animation"] = [
         pose.tolist() for pose in
         convert_quaternion_frames_to_euler_frames(self._motion.mv.frames)
     ]
     dump_data["jointOrder"] = self._graph.skeleton.animated_joints
     write_to_json_file(filename, dump_data)
 def get_full_euler_frames(self, load_data=True, export_data=False):
     full_euler_frames_filename = os.path.join(self.data_analysis_folder,
                                               'full_euler_frames.json')
     if load_data and os.path.exists(full_euler_frames_filename):
         full_euler_frames_dic = load_json_file(
             full_euler_frames_filename)['data']
     else:
         full_euler_frames_dic = {}
         for filename, frames in self.spatial_data.items():
             full_euler_frames_dic[filename] = frames
     if export_data:
         output_data = {'data': full_euler_frames_dic}
         write_to_json_file(full_euler_frames_filename, output_data)
     return full_euler_frames_dic
Example #5
0
 def _export_motion_to_file(self, bvh_string, motion_vector):
     bvh_filename = self.application.service_config[
         "output_dir"] + os.sep + self.application.service_config[
             "output_filename"]
     if self.application.add_timestamp_to_filename:
         bvh_filename += "_" + str(datetime.now().strftime("%d%m%y_%H%M%S"))
     write_message_to_log("export motion to file " + bvh_filename,
                          LOG_MODE_DEBUG)
     with open(bvh_filename + ".bvh", "wb") as out_file:
         out_file.write(bvh_string)
     if motion_vector.mg_input is not None:
         write_to_json_file(bvh_filename + "_input.json",
                            motion_vector.mg_input.mg_input_file)
     if motion_vector.keyframe_event_list is not None:
         motion_vector.keyframe_event_list.export_to_file(bvh_filename)
 def get_reduced_euler_frames(self, load_data=True, export_data=False):
     reduced_euler_frames_filename = os.path.join(
         self.data_analysis_folder, 'reduced_euler_frames.json')
     if load_data and os.path.exists(reduced_euler_frames_filename):
         reduced_euler_frames_dic = load_json_file(
             reduced_euler_frames_filename)['data']
     else:
         bvhreader = BVHReader(self.skeleton_bvh)
         reduced_euler_frames_dic = {}
         for filename, frames in self.spatial_data.items():
             reduced_euler_frames_dic[
                 filename] = convert_euler_frames_to_reduced_euler_frames(
                     bvhreader, frames)
     if export_data:
         output_data = {'data': reduced_euler_frames_dic}
         write_to_json_file(reduced_euler_frames_filename, output_data)
     return reduced_euler_frames_dic
 def load_functional_data(self, n_basis):
     data_analysis_folder = get_data_analysis_folder(
         self.elementary_action, self.motion_primitive, self.data_repo_path)
     if self.parameterization == 'quaternion':
         prestored_filename = os.path.join(data_analysis_folder,
                                           'functional_quat_data.json')
         if not os.path.isfile(prestored_filename):
             functional_quat_data = create_quat_functional_data(
                 self.elementary_action, self.motion_primitive,
                 self.data_repo_path, n_basis)
             write_to_json_file(prestored_filename, functional_quat_data)
         else:
             functional_quat_data = load_json_file(prestored_filename)
     elif self.parameterization == 'Euler':
         raise NotImplementedError
     else:
         raise KeyError('Motion Parameterization type is not supported')
     return functional_quat_data
    def load_data(self, n_basis=None):
        '''
        load the data for specified setting, the data will be loaded from data analysis folder if it is precomputed and
        stored, otherwise it will be loaded directly from alignment folder
        :return:
        '''
        data_analysis_folder = get_data_analysis_folder(
            self.elementary_action, self.motion_primitive, self.data_repo_path)
        if self.data_type == 'discrete':
            if self.parameterization == 'quaternion':
                prestored_filename = os.path.join(data_analysis_folder,
                                                  'smoothed_quat_frames.json')
                if not os.path.isfile(prestored_filename):
                    discrete_motion_data = get_smoothed_quat_frames(
                        self.elementary_action, self.motion_primitive,
                        self.data_repo_path)

                    write_to_json_file(prestored_filename,
                                       discrete_motion_data)
                else:
                    discrete_motion_data = load_json_file(prestored_filename)
                return discrete_motion_data
            elif self.parameterization == 'Euler':
                raise NotImplementedError
            else:
                raise KeyError('Motion Parameterization type is not supported')
        elif self.data_type == 'functional':
            if self.parameterization == 'quaternion':
                prestored_filename = os.path.join(data_analysis_folder,
                                                  'functional_quat_data.json')
                if not os.path.isfile(prestored_filename):
                    functional_quat_data = create_quat_functional_data(
                        self.elementary_action, self.motion_primitive,
                        self.data_repo_path, n_basis)
                    write_to_json_file(prestored_filename,
                                       functional_quat_data)
                else:
                    functional_quat_data = load_json_file(prestored_filename)
                return functional_quat_data
            elif self.parameterization == 'Euler':
                raise NotImplementedError
            else:
                raise KeyError('Motion Parameterization type is not supported')
    def load_discrete_data(self):
        data_analysis_folder = get_data_analysis_folder(
            self.elementary_action, self.motion_primitive, self.data_repo_path)
        if self.parameterization == 'quaternion':
            prestored_filename = os.path.join(data_analysis_folder,
                                              'smoothed_quat_frames.json')
            if not os.path.isfile(prestored_filename):
                discrete_motion_data = get_smoothed_quat_frames(
                    self.elementary_action, self.motion_primitive,
                    self.data_repo_path)

                write_to_json_file(prestored_filename, discrete_motion_data)
            else:
                discrete_motion_data = load_json_file(prestored_filename)
        elif self.parameterization == 'Euler':
            raise NotImplementedError
        else:
            raise KeyError('Motion Parameterization type is not supported')
        return discrete_motion_data
Example #10
0
 def save_model(self, filename):
     write_to_json_file(filename, self.convert_model_to_json())
 def export_annotation(self, output_filename):
     if self.mg_input is not None:
         write_to_json_file(output_filename + ".json",
                            self.mg_input.mg_input_file)
     if self.keyframe_event_list is not None:
         self.keyframe_event_list.export_to_file(output_filename)
    def reduce_dimension(self, n_low_dims, n_basis=None, save_result=True):
        motion_data = self.load_data(n_basis)
        if self.data_type == 'discrete':
            filelist = list(motion_data.keys())
            motion_data_matrix = np.asarray(list(motion_data.values()))
        else:
            knots = motion_data['knots']
            motion_data_matrix = np.asarray(
                list(motion_data['functional_data'].values()))
            filelist = list(motion_data['functional_data'].keys())
            n_canonical_frames = motion_data['n_canonical_frames']
        # scale motion data
        if self.scaling == 'normalization':
            motion_data_2d, scale_params = self.normalize_motion_data(
                motion_data_matrix)
        elif self.scaling == 'scaled_root':
            motion_data_2d, scale_params = self.scale_motion_data_root(
                motion_data_matrix)
        elif self.scaling == 'normal':
            motion_data_2d = reshape_data_for_PCA(motion_data_matrix)
        elif self.scaling == 'scaled':
            if self.data_type == 'discrete':
                raise NotImplementedError
            else:
                sfpca = self.optimize_feature_weights(motion_data_matrix,
                                                      n_low_dims, knots)
        else:
            raise KeyError(self.scaling + ' is not supported!')

        # apply dimension reduction methods
        if self.method == 'pca':
            if self.scaling != 'scaled':
                pca = PCA(n_components=n_low_dims)
                projections = pca.fit_transform(motion_data_2d)
            else:
                projections = sfpca.transform()
        elif self.method == 'lle':
            raise NotImplementedError
        elif self.method == 'iosmap':
            raise NotImplementedError
        elif self.method == 'GPLVM':
            raise NotImplementedError
        else:
            raise KeyError(self.method + ' is not supported.')

        if save_result:
            data_analysis_folder = get_data_analysis_folder(
                self.elementary_action, self.motion_primitive,
                self.data_repo_path)
            outfile_name = os.path.join(
                data_analysis_folder, '_'.join([
                    self.scaling, self.method, 'latent', 'vector',
                    self.data_type, 'data',
                    str(n_basis), 'dims.json'
                ]))
            low_dims_motion_dic = {}
            for i in range(len(filelist)):
                low_dims_motion_dic[filelist[i]] = list(projections[i])

            if self.method == 'pca':
                if self.scaling != 'scaled':
                    variance_vector = list(pca.explained_variance_ratio_)
                else:
                    variance_vector = list(sfpca.pca.explained_variance_ratio_)
                output_data = {
                    'motion_data': low_dims_motion_dic,
                    'variance_vector': variance_vector
                }
                write_to_json_file(outfile_name, output_data)
            elif self.method == 'lle':
                raise NotImplementedError
            elif self.method == 'iosmap':
                raise NotImplementedError
            elif self.method == 'GPLVM':
                raise NotImplementedError
            else:
                raise KeyError(self.method + ' is not supported.')
Example #13
0
 def export_to_file(self, prefix):
     write_to_json_file(prefix + "_annotations" + ".json",
                        self.frame_annotation)
     write_to_json_file(prefix + "_actions" + ".json",
                        self.keyframe_events_dict)