def __check_json_content(self, path_expected, path_actual): expected = get_files_with_suffix(path_expected, ".json") actual = get_files_with_suffix(path_actual, ".json") for exp, act in zip(expected, actual): json_exp = read_json(exp) json_act = read_json(act) if exp.name == act.name == config.MVROI_LAYOUT_FILE: self.assertEqual(json_exp, json_act) elif exp.name == act.name == config.MVROI_NAMING_FILE: self.assertEqual(json_exp, json_act) else: for shape_exp, shape_act in zip(json_exp["shapes"], json_exp["shapes"]): self.assertAlmostEqual(shape_exp, shape_act)
def merge_json_data(json_merge_groups, image_suffix): """ Merge individual frames json data into single frame json data :param json_merge_groups: :param image_suffix: :return: """ merged_json_data = [] for index, json_merge_group in enumerate( tqdm(json_merge_groups, desc="Merging json...")): merged_json = copy.deepcopy(json_merge_group.image_layouts[0]. image_layout) # Use any layout as template merged_json["imageData"] = None merged_json["imageHeight"] = json_merge_group.height merged_json["imageWidth"] = json_merge_group.width merged_json["shapes"] = [] merged_json["imagePath"] = "merged_%06i%s" % (index, image_suffix) for layout in json_merge_group.image_layouts: file_path = json_merge_group.get_file_path_by_key(layout.key) json_data = read_json(file_path) json_data = shift_label_points(json_data, layout.x, layout.y) merged_json["shapes"].extend(json_data["shapes"]) merged_json_data.append(merged_json) return merged_json_data
def main(): """Main""" args = parse_arguments() Path(args.output_dir).mkdir(parents=True, exist_ok=True) image_files = get_files_with_suffix(args.input_dir, args.suffix) json_files = get_files_with_suffix(args.input_dir, config.LABELME_SUFFIX) layout_json = [ json_files.pop(json_files.index(file)) for file in json_files if config.MVROI_LAYOUT_FILE == file.name ] if not layout_json: print( "Input folder does not contain a %s file." % config.MVROI_LAYOUT_FILE, file=sys.stderr, ) sys.exit(1) print( "Found %d %s images and %d label files in %s\n" % (len(image_files), args.suffix, len(json_files), args.input_dir) ) layout_data = read_json(layout_json[0]) if args.split_images: split_images(image_files, layout_data, args.output_dir) individual_json_files = split_json_data(json_files, layout_data, args.suffix) for path, data in tqdm(individual_json_files, desc="Writing json files..."): write_json(Path(args.output_dir).joinpath(path), data)
def init_naming_data(naming_file_arg): """ Init naming data from optional naming file :param naming_file_arg: :return: """ if naming_file_arg is None: return {} else: return read_json(naming_file_arg)
def prepare_scenario_group_gazemaps(scenario_group, image_size, output_gaze_path): """ Create gaze maps from labels :param scenario_group: :param image_size: :param output_gaze_path: :return: """ if image_size is None: raise ValueError("Cannot convert labels without corresponding images.") for path_pair in scenario_group.get_prepared_path_pairs( scenario_group.json_groups, output_gaze_path, config.BDDA_IMAGE_SUFFIX): json_data = read_json(path_pair.source) gazemap = create_gazemap_from_shapes(json_data["shapes"], image_size) gazemap.save(path_pair.target)
def split_json_data(json_files, layout, image_suffix): """ Split json data into individuals according to the provided layout :param json_files: :param layout: :param image_suffix: :return: """ files_to_save = [] for json_file in tqdm(json_files, desc="Splitting json..."): json_file_model = FileModel(json_file) json_data = read_json(json_file) for image_segment in layout["layout"]: layout_model = ImageLayoutModel(image_segment) segment_data = crop_from_json(json_data, layout_model) file_name = json_file_model.get_file_name_with_view_key(layout_model.key) segment_data["imagePath"] = Path(file_name).stem + image_suffix files_to_save.append((file_name, segment_data)) return files_to_save
def main(): """main""" args = parse_arguments() Path(args.output_dir).mkdir(parents=True, exist_ok=True) print("Reading %s..." % config.MVROI_NAMING_FILE) naming_data = read_json(args.naming.name) gazemaps = get_files_with_suffix(args.input_dir, args.suffix) gazemap_groups = FileGrouper.group_files_by_keys(gazemaps, naming_data.keys()) grouped_pairs = get_path_pair_gazemap_groups(gazemap_groups, naming_data, args.output_dir) print( "Found %d %s gazemaps in %d groups" % (len(gazemaps), config.BDDA_IMAGE_SUFFIX, len(gazemap_groups.keys()))) bar = tqdm(grouped_pairs.items()) for key, pair_group in bar: bar.set_description("Reformatting sequence for index %s..." % key) reformat_gaze_map_sequence(pair_group)
def read_json_string(path): return json.dumps(read_json(path))