def test_get_prepared_path_pairs__2_topics_and_no_files__2_correct_path_pairs( self): unit = ScenarioGrouper(1, "test", ["front", "rear"], []) result = unit.get_prepared_path_pairs(unit.image_groups, Path("output")) self.assertFalse(result)
def test_get_prepared_path_pairs__output_suffix_jpg__2_correct_path_pairs( self): unit = ScenarioGrouper(1, "test", ["front", "rear"], ["front_000000.png", "rear_000000.png"]) result = unit.get_prepared_path_pairs(unit.image_groups, Path("output"), ".jpg") self.assertEqual(2, len(result)) self.assertEqual(".jpg", result[0].target.suffix) self.assertEqual(".jpg", result[1].target.suffix)
def test_get_prepared_path_pairs__2_topics_and_2_files__2_correct_path_pairs( self): unit = ScenarioGrouper(1, "test", ["front", "rear"], ["front_000000.png", "rear_000000.png"]) result = unit.get_prepared_path_pairs(unit.image_groups, Path("output")) self.assertEqual(2, len(result)) self.assertEqual(Path("front_000000.png"), result[0].source) self.assertEqual(Path("output/10_00000.png"), result[0].target)
def test_get_naming_data_for_topic__existing_topic__correct_values(self): unit = ScenarioGrouper(1, "test", ["front"], []) self.assertEqual( { "view": "front", "scenario_name": "test", "scenario_index": 1 }, unit.get_naming_data_for_topic("front"), )
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_append_naming_data__one_2_topics_and_existing_naming__6_elements( self, ): result = prepare.append_naming_data( [ScenarioGrouper(3, "test", ["front", "rear"], [])], self.TEST_NAMING.copy()) self.assertEqual(6, len(result)) self.assertIn("31", result.keys())
def test_prepare_scenario_group_gazemaps__size_is_none__raise_error(self): with self.assertRaises(ValueError): prepare.prepare_scenario_group_gazemaps( ScenarioGrouper( 1, "test", ["front", "rear"], [], ["front_000000.json", "rear_000000.json"], ), None, "output_g", )
def test_prepare_scenario_group_gazemaps__2_topics_and_label_files__open_called_twice( self, save_mock_method, open_mock_method): prepare.prepare_scenario_group_gazemaps( ScenarioGrouper( 1, "test", ["front", "rear"], [], ["front_000000.json", "rear_000000.json"], ), (10, 20), "output_g", ) self.assertEqual(2, open_mock_method.call_count) self.assertEqual(2, save_mock_method.call_count)
def test_get_naming_data_for_topic__not_existing_topic__raise_error(self): unit = ScenarioGrouper(1, "test", ["front"], []) with self.assertRaises(ValueError): unit.get_naming_data_for_topic("test")
def test_get_sequence_index_for_topic__not_existing_topic__raise_error( self): unit = ScenarioGrouper(1, "test", ["front", "rear"], []) with self.assertRaises(ValueError): unit.get_sequence_index_for_topic("test")
def test_get_sequence_index_for_topic__existing_topic__correct_index(self): unit = ScenarioGrouper(1, "test", ["front", "rear"], []) self.assertEqual("10", unit.get_sequence_index_for_topic("front")) self.assertEqual("11", unit.get_sequence_index_for_topic("rear"))
def test_constructor__simple__correct_properties(self): unit = ScenarioGrouper(1, "test", [], []) self.assertEqual(1, unit.scenario_index) self.assertEqual("test", unit.scenario_name)
def test_prepare_scenario_group_images__2_topics_and_no_files__never_called( self, mock_method): prepare.prepare_scenario_group_images( ScenarioGrouper(1, "test", ["front", "rear"], []), "output") mock_method.assert_not_called()
def test_append_naming_data__one_2_topics__2_elements(self): result = prepare.append_naming_data( [ScenarioGrouper(1, "test", ["front", "rear"], [])], {}) self.assertEqual(2, len(result)) self.assertEqual({"10", "11"}, result.keys())
class PrepareTest(unittest.TestCase): """Prepare Data Test""" TEST_NAMING = { "10": { "scenario_index": 1 }, "11": { "scenario_index": 1 }, "20": { "scenario_index": 2 }, "21": { "scenario_index": 2 }, } TEST_GROUP_IMAGES = ScenarioGrouper( 1, "test", ["front", "rear"], ["front_000000.png", "rear_000000.png"]) def test_append_naming_data__empty_scenario_groups__empty_naming_list( self): result = prepare.append_naming_data([], {}) self.assertFalse(result) def test_append_naming_data__one_2_topics__2_elements(self): result = prepare.append_naming_data( [ScenarioGrouper(1, "test", ["front", "rear"], [])], {}) self.assertEqual(2, len(result)) self.assertEqual({"10", "11"}, result.keys()) def test_append_naming_data__one_2_topics_and_existing_naming__6_elements( self, ): result = prepare.append_naming_data( [ScenarioGrouper(3, "test", ["front", "rear"], [])], self.TEST_NAMING.copy()) self.assertEqual(6, len(result)) self.assertIn("31", result.keys()) def test_init_naming_data__no_naming__empty_dict(self): result = prepare.init_naming_data(None) self.assertFalse(result) @patch("builtins.open", new_callable=mock_open, read_data="data") @patch("json.loads", MagicMock()) def test_init_naming_data__with_naming__read_json_called( self, mock_method): prepare.init_naming_data("naming.json") mock_method.assert_called_once() @patch("PIL.Image.open") def test_prepare_scenario_group_images__2_topics_and_2_files__open_called_twice( self, mock_method): prepare.prepare_scenario_group_images( self.TEST_GROUP_IMAGES, "output", ) self.assertEqual(2, mock_method.call_count) @patch("PIL.Image.open") def test_prepare_scenario_group_images__2_topics_and_no_files__never_called( self, mock_method): prepare.prepare_scenario_group_images( ScenarioGrouper(1, "test", ["front", "rear"], []), "output") mock_method.assert_not_called() @patch("builtins.open", new_callable=mock_open, read_data="data") @patch("json.loads", MagicMock()) def test_prepare_scenario_group_gazemaps__2_topics_and_image_files_only__open_never_called( self, open_mock_method): prepare.prepare_scenario_group_gazemaps( self.TEST_GROUP_IMAGES, (10, 20), "output_g", ) open_mock_method.assert_not_called() @patch("builtins.open", new_callable=mock_open, read_data="data") @patch("json.loads", MagicMock()) @patch.object(PIL.Image.Image, "save") def test_prepare_scenario_group_gazemaps__2_topics_and_label_files__open_called_twice( self, save_mock_method, open_mock_method): prepare.prepare_scenario_group_gazemaps( ScenarioGrouper( 1, "test", ["front", "rear"], [], ["front_000000.json", "rear_000000.json"], ), (10, 20), "output_g", ) self.assertEqual(2, open_mock_method.call_count) self.assertEqual(2, save_mock_method.call_count) def test_prepare_scenario_group_gazemaps__size_is_none__raise_error(self): with self.assertRaises(ValueError): prepare.prepare_scenario_group_gazemaps( ScenarioGrouper( 1, "test", ["front", "rear"], [], ["front_000000.json", "rear_000000.json"], ), None, "output_g", ) def test_get_scenario_start_index__empty_naming__start_index_1(self): result = prepare.get_scenario_start_index({}) self.assertEqual(1, result) def test_get_scenario_start_index__2_scenarios_in_naming__start_index_3( self): result = prepare.get_scenario_start_index(self.TEST_NAMING) self.assertEqual(3, result) def test_create_gazemap_from_shapes__empty_shapes__all_back_and_correct_size( self): result = prepare.create_gazemap_from_shapes([], (10, 20)) self.assertEqual((10, 20), result.size) self.assertEqual(config.GAZEMAP_FORMAT, result.mode) self.assertIsNone(result.getbbox()) def test_create_gazemap_from_shapes__one_shape__not_all_back(self): result = prepare.create_gazemap_from_shapes([{ "points": [[20, 30], [30, 45.67]] }], (100, 100)) self.assertIsNotNone(result.getbbox()) def test_create_gazemap_from_shapes__two_shape__white_pixel_within_objects( self): result = prepare.create_gazemap_from_shapes( [{ "points": [[25, 25], [27, 25]] }, { "points": [[50, 50], [50, 51]] }], (60, 60), ) self.assertEqual(255, result.getpixel((26, 26))) self.assertEqual(255, result.getpixel((49, 49)))