def main(): """main""" args = parse_arguments() output_image_path = Path(args.output_dir).joinpath("camera_images") output_gaze_path = Path(args.output_dir).joinpath("gazemap_images") output_image_path.mkdir(parents=True, exist_ok=True) output_gaze_path.mkdir(parents=True, exist_ok=True) input_dirs = [Path(input_dir) for input_dir in args.input_dirs] naming_data = init_naming_data(args.naming) print("Reading scenarios...") scenario_index = get_scenario_start_index(naming_data) num_previous_scenarios = scenario_index - 1 scenario_groups = [] for input_dir in input_dirs: image_files = get_files_with_suffix(input_dir, args.suffix) json_files = get_files_with_suffix(input_dir, config.LABELME_SUFFIX) scenario_grouper = ScenarioGrouper( scenario_index, input_dir.name, args.image_topics, image_files, json_files, ) if not image_files: print( "Could not find any image files in scenario %s with %s extension. " "Make sure you set the corrent suffix with --suffix." % (scenario_grouper.scenario_name, args.suffix), file=sys.stderr, ) sys.exit(1) if not scenario_grouper.is_valid: print( "Images of scenario %s are not aligned or of same length for topics %s.\n" "Run merge.py with --reindex to align your files" % (scenario_grouper.scenario_name, args.image_topics), file=sys.stderr, ) sys.exit(1) if FileGrouper.is_empty(scenario_grouper.image_groups): print( "None of the image files for scenario %s is matching a topic in %s" % (scenario_grouper.scenario_name, args.image_topics), file=sys.stderr, ) sys.exit(1) scenario_groups.append(scenario_grouper) scenario_index += 1 print("Write %s" % config.MVROI_NAMING_FILE) naming_data = append_naming_data(scenario_groups, naming_data) write_json( Path(args.output_dir).joinpath(config.MVROI_NAMING_FILE), naming_data) for scenario_group in tqdm(scenario_groups, desc="Preparing scenarios..."): size = prepare_scenario_group_images(scenario_group, output_image_path) prepare_scenario_group_gazemaps(scenario_group, size, output_gaze_path)
def test_group_files_by_keys__keys_and_files_not_matching__empty_six_keys_dict( self, ): result = FileGrouper.group_files_by_keys(["test_0.png", "test_1.png"], self.TEST_KEYS) self.assertEqual(set(self.TEST_KEYS), result.keys()) for val in result.values(): self.assertFalse(val)
def main(): """main""" args = parse_arguments() width, height = parse_resolution(args.res) Path(args.output_dir).mkdir(parents=True, exist_ok=True) layout_data = create_layout_data(args.image_topics, args.images_per_row, width, height) if not (args.hdf5 or args.reindex): print("Write layout.json") write_json( Path(args.output_dir).joinpath(config.MVROI_LAYOUT_FILE), layout_data) image_files = get_files_with_suffix(args.input_dir, args.suffix) json_files = get_files_with_suffix(args.input_dir, ".json", ignore=config.MVROI_LAYOUT_FILE) print("Found %d %s images and %d label files in %s\n" % (len(image_files), args.suffix, len(json_files), args.input_dir)) if args.reindex: print("Reindexing files...") reindex_files(image_files, args.image_topics) return print("Grouping files for merging...") image_grouper = FileGrouper(layout_data, image_files, args.image_topics) json_grouper = FileGrouper(layout_data, json_files, args.image_topics) if not (image_grouper.is_valid and json_grouper.is_valid): print( "Image or json files not aligned or of same length for topics %s.\n" "Run with --reindex to align your files" % args.image_topics, file=sys.stderr, ) sys.exit(1) print("Found %d image and %d json groups to merge\n" % (len(image_grouper.merge_groups), len(json_grouper.merge_groups))) if args.hdf5: hdf5_merge(args, image_grouper, json_grouper) else: file_merge(args, image_grouper, json_grouper, args.suffix)
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 test_has_same_lengths__all_empty__true(self): test_group = FileGrouper.group_files_by_keys([], ["test"]) self.assertTrue(FileGrouper.has_same_lengths(test_group))
def test_is_valid__not_consecutive__false(self): test_files = [ "front_000000.png", "front_000001.png", "front_000004.png" ] unit = FileGrouper(TEST_LAYOUT_SINGLE, test_files, ["front"]) self.assertFalse(unit.is_valid)
def test_is_valid__different_length__false(self): test_files = copy.deepcopy(self.TEST_SINGLE_FILE_PER_KEY) test_files.append("front_000001.png") unit = FileGrouper(self.TEST_LAYOUT, test_files, self.TEST_KEYS) self.assertFalse(unit.is_valid)
def test_is_valid__same_length__true(self): unit = FileGrouper(self.TEST_LAYOUT, self.TEST_SINGLE_FILE_PER_KEY, self.TEST_KEYS) self.assertTrue(unit.is_valid)
def test_is_valid__no_files__true(self): unit = FileGrouper(self.TEST_LAYOUT, [], self.TEST_KEYS) self.assertTrue(unit.is_valid)
def test_group_files_by_keys__gazemap_keys__two_keys_dict(self): result = FileGrouper.group_files_by_keys( ["10_00000.jpg", "11_00000.jpg"], ["10", "11"]) self.assertEqual({"10", "11"}, result.keys()) for key, val in result.items(): self.assertEqual(1, len(val))
def test_group_files_by_keys__file_order_with_one_leading_zeros__ordered_by_file_index( self, ): file_list = get_ordered_file_list_with_one_leading_zero() result = FileGrouper.group_files_by_keys(file_list, ["a"]) self.assertTrue(FileGrouper.is_consecutive(result))
def test_group_files_by_keys__test_keys__six_keys_dict(self): result = FileGrouper.group_files_by_keys([], self.TEST_KEYS) self.assertEqual(set(self.TEST_KEYS), result.keys()) for val in result.values(): self.assertFalse(val)
def test_group_files_by_keys__no_keys__empty_dict(self): result = FileGrouper.group_files_by_keys([], []) self.assertFalse(result)
def test_group_files_for_merging__one_file_per_key__one_merge_item(self): unit = FileGrouper(self.TEST_LAYOUT, self.TEST_SINGLE_FILE_PER_KEY, self.TEST_KEYS) result = unit.merge_groups self.assertEqual(1, len(result))
def test_group_files_for_merging__no_files__empty(self): unit = FileGrouper(self.TEST_LAYOUT, [], self.TEST_KEYS) result = unit.merge_groups self.assertFalse(result)
def test_is_consecutive__correct_order__true(self): test_group = FileGrouper.group_files_by_keys( ["test_00.jpg", "test_01.jpg"], ["test"]) self.assertTrue(FileGrouper.is_consecutive(test_group))
def test_is_empty__all_empty__true(self): test_group = FileGrouper.group_files_by_keys([], ["test"]) self.assertTrue(FileGrouper.is_empty(test_group))
def test_group_files_by_keys__test_single_file_per_key__correct(self): result = FileGrouper.group_files_by_keys(self.TEST_SINGLE_FILE_PER_KEY, self.TEST_KEYS) for key, val in result.items(): self.assertEqual(1, len(val)) self.assertIn(val[0].file_path.name, self.TEST_SINGLE_FILE_PER_KEY)