def combined_json( detection_json_dir, classification_json_dir): """ Combine results from detector and classifier @param detection_json_dir: directory to detector results @param classification_json_dir: directory to classifier results """ for json_file in tools.find_files(detection_json_dir): json_data = data.parse_json(json_file) annotations = json_data['annotations'] for i in range(len(annotations)): classification_json_file = os.path.join( classification_json_dir, os.path.basename(json_file).split('.')[0] + '_' + str(i) + '.json') if os.path.exists(classification_json_file): classification_annotations = data.parse_json( classification_json_file)['annotations'][0] annotations[i]['type'] = 'detection_classification' annotations[i]['label'] += '_' + \ classification_annotations['label'] annotations[i]['labinfo'] = classification_annotations[ 'labinfo'] json_data['annotations'] = annotations out_json = os.path.join( os.path.dirname(json_file), os.path.basename(json_file).split('.')[0] + '_combined.json') with open(out_json, 'w') as wf: json.dump(json_data, wf)
def set_dyda_config(self, dyda_config, encoding=None, terminate_if_fail=True): """ Set TrainerBase.config :param str dyda_config: Input for setting up config.It can be a read dictionary or a path of the config json :param str encoding: Choose encoding of the config json file :param bool terminate_if_fail: True to terminate the programe if the setting of TrainerBase.config fails. :return: None :rtype: None """ if isinstance(dyda_config, dict): self.config = dyda_config self.base_logger.info('TrainerBase.config set successfully') elif os.path.isfile(dyda_config): try: self.config = data.parse_json( dyda_config, encoding=encoding ) except IOError: self.base_logger.error('Cannot open %s' % dyda_config) if terminate_if_fail: sys.exit(0) except Exception: traceback.print_exc(file=sys.stdout) if terminate_if_fail: sys.exit(0)
def reverse_padding( in_json, shift, size, padded_dir, frame_dir): """ Reverse detection result of padded image to detection result fo original image @param in_json: input json filename @param shift: (shift_x, shift_y) @param size: (width, height), size of original image @param padded_dir: directory of padded images @param frame_dir: directory of original images """ shift_x = shift[0] shift_y = shift[1] width = size[0] height = size[1] json_data = data.parse_json(in_json) if json_data['folder'] == padded_dir: json_data = shift_boxes(json_data, (shift_x, shift_y), (width, height)) json_data['folder'] = frame_dir with open(in_json, 'w') as wf: json.dump(json_data, wf)
def get_metadata(self, metadata_input, encoding=None): """ Read from metadata_input @param metadata_input: filepath of the dict of metadata Keyword arguments: encoding -- encoding of the json file """ metadata = {} if isinstance(metadata_input, dict): metadata = metadata_input return metadata elif os.path.isfile(metadata_input): try: metadata = data.parse_json(metadata_input, encoding=encoding) return metadata except IOError: print('[dyda] Error: cannot open %s' % metadata_input) self.terminate_flag = True return {} except Exception: traceback.print_exc(file=sys.stdout) self.terminate_flag = True return {} else: self.terminate_flag = True print("%s is not a valid file" % metadata_input) return {}
def replace_dyda_config(self, dyda_config, session, encoding=None, terminate_if_fail=True): """ Replace the whole TrainerBase.config or just partially :param str dyda_config: Input for setting up config.It can be a read dictionary or a path of the config json :param str session: Specify the component session, all for all sessions :param str encoding: Choose encoding of the config json file :param bool terminate_if_fail: True to terminate the programe if the setting of TrainerBase.config fails. :return: None :rtype: None """ if session == 'all': self.set_dyda_config( dyda_config, encoding=encoding, terminate_if_fail=terminate_if_fail ) else: config = None if isinstance(dyda_config, dict): config = dyda_config elif os.path.isfile(dyda_config): try: config = data.parse_json( dyda_config, encoding=encoding ) except IOError: self.base_logger.error('Cannot open %s' % dyda_config) if terminate_if_fail: sys.exit(0) except Exception: traceback.print_exc(file=sys.stdout) if terminate_if_fail: sys.exit(0) if config: if session in config.keys(): self.config[session] = config[session] else: if isinstance(dyda_config, dict): self.base_logger.warning('%s session not found in ' 'input dyda_config dict' % session) elif isinstance(dyda_config, str): self.base_logger.warning('%s session not found in %s' % (session, dyda_config)) else: self.base_logger.warning('%s session not found' % session)
def box_append( filename, annotations): """ Append annotations to the file @param filename: filename of the file appended @param annotations: annotations of a detection result """ if os.path.isfile(filename): base = data.parse_json(filename) else: base = [] for i in range(len(annotations)): base.append(annotations[i]) data.write_json(base, filename)
def conv_to_df(array, ffields=None, target=None): """Convert array to pandas.DataFrame @param array: input array to be converted Keyword arguments: ffields -- json file of the fields (default: None) target -- if ffields is specified, can also specified the target column to be used (default: None) """ if ffields is not None: fields = data.parse_json(ffields) if isinstance(target, int): print('[pandas_data] Converting field from %s to target' % fields[target]) fields[target] = 'target' return pd.DataFrame(array, columns=fields) return pd.DataFrame(array)