def extract_features(self, features_path, database, parameters=None): if parameters: access_modifier = AccessModifier( target_framerate=parameters['framerate'], target_duration=parameters['total_time_acquisition'], starting_time=parameters['starting_time_acquisition']) else: access_modifier = AccessModifier() dict_accesses = database.get_all_accesses(access_modifier) self.informer.set_title('Extracting features') len_total_accesses = 0 for subset, accesses in dict_accesses.items(): enable_data_augmentation = False if subset is 'Train' and self.configuration.use_data_augmentation: enable_data_augmentation = True self.__extract_from_accesses( features_path, subset, accesses, data_augmentation=enable_data_augmentation) len_total_accesses += len(accesses) self.informer.message('Finished. Extracted {} features{}'.format( len_total_accesses, SPACES), color=Colors.FG.green, prefix='\r', suffix='\n')
def test_str(self): access_modifier = AccessModifier() self.assertEqual( access_modifier.__str__(), 'AccessModifier [ target_framerate = 30 | ' 'target_duration = -1 | ' 'starting_time = -1 | ' 'center_video_acquisition = False ]')
def test_constructor_with_correct_params_with_timestamp_reference_zero( self): dict_images = TestUtils.get_synthetic_dict_image(timestamp_reference=0) access_modifier = AccessModifier() mod_dict_images = access_modifier.run(dict_images) self.assertEqual(dict_images, mod_dict_images)
def test_should_return_an_access_normalized_when_run_with_default_params( self): dict_images = TestUtils.get_synthetic_dict_image() access_modifier = AccessModifier() mod_dict_images = access_modifier.run(dict_images) self.assertEqual(len(dict_images), len(mod_dict_images)) self.assertNotEqual(dict_images, mod_dict_images) self.assertEqual(list(mod_dict_images)[0], 0)
def test_should_return_an_access_normalized_and_trimmed_when_run_with_target_framerate_target_duration_and_center_video_acquisition( self): dict_images = TestUtils.get_synthetic_dict_image() access_modifier = AccessModifier(target_framerate=15, target_duration=1000, center_video_acquisition=True) mod_dict_images = access_modifier.run(dict_images) self.assertNotEqual(len(dict_images), len(mod_dict_images)) self.assertEqual(len(mod_dict_images), 15)
def test_should_return_an_access_normalized_and_trimmed_when_run_with_target_framerate_target_duration_and_starting_time( self): dict_images = TestUtils.get_synthetic_dict_image() access_modifier = AccessModifier(target_framerate=15, target_duration=1000, starting_time=100) mod_dict_images = access_modifier.run(dict_images) self.assertNotEqual(len(dict_images), len(mod_dict_images)) self.assertEqual(len(mod_dict_images), 16)
def get_all_accesses(self, access_modifier=AccessModifier()): if not self.dict_all_accesses: self.dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for root, dirs, files in os.walk(self.base_path): if not dirs: for f in files: if f.lower().endswith(('.mov', '.mp4')): basename, extension = os.path.splitext(f) split_path = os.path.normpath(root).split( os.path.sep) relative_path = os.path.join(*split_path[-3:]) subject = int(basename.split('-')[0]) original_subset = split_path[-3] if original_subset == 'Train' and subject in SIW_USERS_PER_SUBSET[ 'Dev']: subset = 'Dev' else: subset = original_subset self.dict_all_accesses[subset].append( VideoAccess(self.base_path, os.path.join( relative_path, basename), extension, access_modifier=access_modifier, annotation_base_path=self. annotation_base_path, database_name=SiwDatabase.name())) for subset, list_accesses in self.dict_all_accesses.items(): list_accesses.sort(key=lambda x: x.name) return self.dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): if not self.dict_all_accesses: self.dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for root, dirs, files in os.walk(self.base_path): if not dirs: if sum([f.endswith('.jpg') for f in files]): split_path = os.path.normpath(root).split(os.path.sep) relative_path = os.path.relpath(root, self.base_path) user = split_path[-2] for subset, list_users in CSMAD_USERS_PER_SUBSET.items( ): if user in list_users: self.dict_all_accesses[subset].append( FolderAccess( self.base_path, relative_path, '.jpg', access_modifier=access_modifier, annotation_base_path=self. annotation_base_path, database_name=CsmadDatabase.name())) for subset, list_accesses in self.dict_all_accesses.items(): list_accesses.sort(key=lambda x: x.name) return self.dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): if not self.dict_all_accesses: self.dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for root, dirs, files in os.walk(self.base_path): if not dirs: if sum([f.endswith('.jpg') for f in files]): relative_path = os.path.relpath(root, self.base_path) split_path = os.path.normpath(relative_path).split( os.path.sep) # Until March 10th, only Train set is available if split_path[0] == 'Training' and split_path[ -1] == 'color': depth_relative_path = relative_path.replace( 'color', 'depth') ir_relative_path = relative_path.replace( 'color', 'ir') user = split_path[-3] if user in CASIA_DEV_USERS: subset = 'Dev' elif user in CASIA_TEST_USERS: subset = 'Test' else: subset = 'Train' self.dict_all_accesses[subset].append( FolderAccess( self.base_path, relative_path, '.jpg', depth_name=depth_relative_path, infrared_name=ir_relative_path, access_modifier=access_modifier, annotation_base_path=self. annotation_base_path, database_name=CasiaSurfDatabase.name())) # subset = 'Test' # orig_dev_list = [line.rstrip() for line in open(os.path.join(self.base_path, # 'val_public_list.txt'))] # for line in orig_dev_list: # color_relative_path, depth_relative_path, ir_relative_path = line.split(' ') # self.dict_all_accesses[subset].append( # FolderAccess(self.base_path, color_relative_path, '.jpg', # depth_name=depth_relative_path, # infrared_name=ir_relative_path, # access_modifier=access_modifier, # annotation_base_path=self.annotation_base_path, # database_name=CasiaSurfDatabase.name() # )) for subset, list_accesses in self.dict_all_accesses.items(): list_accesses.sort(key=lambda x: x.name) return self.dict_all_accesses
def get_all_accesses(self, access_modificator=AccessModifier()): dict_all_accesses = {} list_accesses = [] dict_accesses_by_subset = self.get_accesses_by_subset( access_modificator) for subset in dict_accesses_by_subset: list_accesses += dict_accesses_by_subset[subset] list_accesses.sort(key=lambda x: x.name) dict_all_accesses['All'] = list_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): dict_all_accesses = {} dict_all_accesses['Train'] = self.__get_accesses_from_subset( 'train', access_modifier) dict_all_accesses['Dev'] = self.__get_accesses_from_subset( 'devel', access_modifier) dict_all_accesses['Test'] = self.__get_accesses_from_subset( 'test', access_modifier) return dict_all_accesses
def test_constructor_with_valid_parameters_and_setter_access_modifier( self, dummy_dict_images=dummy_dict_images): base_path = TestUtils.get_resources_path() name = 'genuine' folder_access = FolderAccess(base_path, name) folder_access.set_access_modifier(AccessModifier()) dict_images = folder_access.load() AccessModifier.run.assert_called_once() self.assertEqual(dict_images, dummy_dict_images)
def __init__(self, base_path, name, access_modificator=AccessModifier(), annotation_base_path=None): self.name = name super(DummyAccess, self).__init__(base_path, name, access_modificator, annotation_base_path=annotation_base_path)
def get_all_accesses(self, access_modifier=AccessModifier()): dict_all_accesses = {'All': []} for database in iter(self.included_databases.values()): dict_all_accesses_one_database = database.get_all_accesses( access_modifier) for subset in dict_all_accesses_one_database: subset_accesses = dict_all_accesses_one_database[subset] for access in subset_accesses: access.database_name = database.name() dict_all_accesses['All'] += subset_accesses return dict_all_accesses
def test_constructor_with_valid_parameters_with_setter_access_modifier( self, dummy_dict_images=dummy_dict_images): base_path = TestUtils.get_resources_path() name = 'video' extension = '.mov' video_access = VideoAccess(base_path, name, extension) video_access.set_access_modifier(AccessModifier()) dict_images = video_access.load() AccessModifier.run.assert_called_once() self.assertEqual(dict_images, dummy_dict_images)
def get_enrolment_access(self, access_modifier=AccessModifier()): list_accesses = [] objects = self.db.objects(cls='enroll') for access in objects: path, extension = os.path.splitext(access.videofile()) list_accesses.append( VideoAccess(self.base_path, path, extension, access_modifier=access_modifier, annotation_base_path=self.annotation_base_path)) list_accesses.sort(key=lambda x: x.name) return list_accesses
def get_enrolment_access(self, access_modifier=AccessModifier()): list_accesses = [] objects = self.db.objects(cls='enroll') for access in objects: rotation_rule = self.get_rotation_rule(access) path, extension = os.path.splitext(access.videofile()) list_accesses.append( VideoAccess(self.base_path, path, extension, access_modifier=access_modifier, rotation_rule=rotation_rule, annotation_base_path=self.annotation_base_path, database_name=ReplayMobileDatabase.name())) list_accesses.sort(key=lambda x: x.name) return list_accesses
def get_accesses_by_subset(self, access_modificator=AccessModifier()): dict_accesses_by_subset = {} for subset in ['Train', 'Dev', 'Test']: list_accesses = [] for number_access in range(0, 5): list_accesses.append( DummyAccess('base_path', 'real_' + str(number_access), access_modificator=access_modificator)) for number_access in range(0, 5): list_accesses.append( DummyAccess('base_path', 'attack_' + str(number_access), access_modificator=access_modificator)) list_accesses.sort(key=lambda x: x.name) dict_accesses_by_subset[subset] = list_accesses return dict_accesses_by_subset
def get_all_accesses(self, access_modifier=AccessModifier()): dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for subset, subset_accesses in dict_all_accesses.items(): for access in self.db.objects( sets=THREEDMAD_SUBSETS_CORRESPONDENCE[subset]): path, extension = os.path.splitext(access.videofile()) subset_accesses.append( VideoAccess(self.base_path, path, extension, access_modifier=access_modifier, annotation_base_path=self.annotation_base_path, database_name=ThreedmadDatabase.name())) subset_accesses.sort(key=lambda x: x.name) return dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for subset, subset_accesses in dict_all_accesses.items(): objects = self.db.objects( groups=REPLAY_MOBILE_SUBSETS_CORRESPONDENCE[subset]) for access in objects: rotation_rule = self.get_rotation_rule(access) path, extension = os.path.splitext(access.videofile()) subset_accesses.append( VideoAccess(self.base_path, path, extension, access_modifier=access_modifier, rotation_rule=rotation_rule, annotation_base_path=self.annotation_base_path, database_name=ReplayMobileDatabase.name())) subset_accesses.sort(key=lambda x: x.name) return dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): dict_all_accesses = {} list_accesses = [] folders_correspondence = {'Train_files': 'Train', 'Dev_files': 'Dev', 'Test_files': 'Test'} for folder in OULU_FOLDERS_VIDEO: folder_set = os.path.join(self.base_path, folder) list_basename_access = self.__get_list_basename_access_from_folder(folder_set) for basename in list_basename_access: list_accesses.append(VideoAccess(self.base_path, os.path.join(folder, basename), OULU_VIDEO_EXTENSION, access_modifier=access_modifier, annotation_base_path=self.annotation_base_path, database_name=OuluNpuDatabase.name())) list_accesses.sort(key=lambda x: x.name) dict_all_accesses[folders_correspondence[folder]] = list_accesses list_accesses = [] return dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): if not self.dict_all_accesses: self.dict_all_accesses = {'Train':[], 'Dev': [], 'Test': []} dict_videos = {} for root, dirs, files in os.walk(self.base_path): if not dirs: for f in files: if f.lower().endswith(('.mp4', '.mov')): basename, ext = os.path.splitext(f) if basename not in dict_videos: dict_videos[basename] = [] dict_videos[basename].append(os.path.join(root, f)) for subset, subset_accesses in self.dict_all_accesses.items(): txt_file = os.path.join(self.base_path, 'release_1', 'protocols', 'grandtest', subset + '.txt') with open(txt_file) as my_file: for line in my_file.readlines(): user, video1, video2 = line.rstrip().split(' ') basename1, _ = os.path.splitext(video1) basename2, _ = os.path.splitext(video2) for basename in [basename1, basename2]: for full_access_path in dict_videos[basename]: extension = os.path.basename(full_access_path).split('.')[1] relative_path = os.path.relpath(full_access_path, self.base_path).replace('.' + extension, '') subset_accesses.append(VideoAccess(os.path.join(self.base_path), relative_path, ('.' + extension).rstrip(), access_modifier=access_modifier, annotation_base_path=self.annotation_base_path, database_name=UvadDatabase.name())) subset_accesses.sort(key=lambda x: x.name) return self.dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): if not self.dict_all_accesses: self.dict_all_accesses = {'Train': [], 'Dev': [], 'Test': []} for root, dirs, files in os.walk(self.base_path): if not dirs: for f in files: if f.lower().endswith('.avi'): basename, extension = os.path.splitext(f) split_path = os.path.normpath(root).split( os.path.sep) subject = int(split_path[-1]) if split_path[-2].startswith('train_'): if subject in CASIA_FASD_DEV_USERS: subset = 'Dev' else: subset = 'Train' else: subset = 'Test' self.dict_all_accesses[subset].append( VideoAccess( self.base_path, os.path.join(split_path[-2], split_path[-1], basename), extension, access_modifier=access_modifier, annotation_base_path=self. annotation_base_path, database_name=CasiaFasdDatabase.name())) for subset, list_accesses in self.dict_all_accesses.items(): list_accesses.sort(key=lambda x: x.name) return self.dict_all_accesses
def get_all_accesses(self, access_modifier=AccessModifier()): dict_accesses_by_subset = self.get_accesses_by_subset(access_modifier) return dict_accesses_by_subset
def __extract_pad_results(self, scores_filename, database, protocol): self.informer.highlight_message( 'end-to-end parameters [framerate = {}, total_time_acquisition = {}]' .format(self.configuration.framerate, self.configuration.total_time_acquisition), title='\tEnd-to-end extraction', color=Colors.FG.lightcyan) access_modifier = AccessModifier( self.configuration.framerate, self.configuration.total_time_acquisition) if database.type_database is TypeDatabase.ALL_FILES_TOGETHER: dict_accesses = database.get_accesses_by_subset(access_modifier) else: dict_accesses = database.get_all_accesses(access_modifier) self.informer.set_title('Extracting pad results') info = cpuinfo.get_cpu_info() name_algorithm = '{}_f{}_ta{}'.format( self.configuration.face_pad.name, self.configuration.framerate, self.configuration.total_time_acquisition) framerate = self.configuration.framerate total_time_of_acquisition = self.configuration.total_time_acquisition processor = ' '.join(info['brand'].split()) processed_frames = 0 scores_list = [] time_of_delay_list = [] cpu_time_list = [] labels_list = [] benchmark_labels_list = [] for subset in SUBSETS_TO_EVALUATE: subset_accesses = dict_accesses[subset] subset_ground_truth = database.get_ground_truth(protocol)[subset] failure_accesses = 0 for i, access in enumerate(subset_accesses): progressBar(i + 1, len(subset_accesses), access.name) dict_images = access.load() start_time_cpu_processing = time.time() * 1000 for key, image in dict_images.items(): self.configuration.face_pad.process(image) processed_frames += 1 if self.configuration.face_pad.isfinished(): break cpu_time = time.time() * 1000 - start_time_cpu_processing start_time_decision = time.time() * 1000 label, score = self.configuration.face_pad.get_decission() time_of_delay = time.time() * 1000 - start_time_decision if label == "FAILURE_TO_COMPUTE": failure_accesses += 1 self.configuration.face_pad.reset() scores_list.append(score) time_of_delay_list.append(time_of_delay) cpu_time_list.append(cpu_time) labels_list.append(label) benchmark_labels_list.append('ATTACK' if subset_ground_truth[ access.name] == 1 else 'NO_ATTACK') print('\n\t\tFAILURE_TO_COMPUTE accesses: ' + str(failure_accesses) + '/' + str(len(subset_accesses))) end_to_end_info = EndToEndInfo(name_algorithm, framerate, total_time_of_acquisition, processor, processed_frames, scores_list, time_of_delay_list, cpu_time_list, labels_list, benchmark_labels_list) end_to_end_info.save(scores_filename) return end_to_end_info
def test_constructor_with_none_dict_images(self): dict_images = None access_modifier = AccessModifier() self.assertRaises(ValueError, lambda: access_modifier.run(dict_images))