diff --git a/bob/paper/nir_patch_pooling/database/mlfp.py b/bob/paper/nir_patch_pooling/database/mlfp.py index 74b9bcc056af6fdfccd7a1bccbc850485a29ff9a..b1c84acdeac7d2a50b5eddc836fe1f212239eb92 100644 --- a/bob/paper/nir_patch_pooling/database/mlfp.py +++ b/bob/paper/nir_patch_pooling/database/mlfp.py @@ -50,7 +50,6 @@ class File(VideoPadFile): #------------------------------------------------------------------------------ -#------------------------------------------------------------------------------ class MLFPDatabase(FileListPadDatabase): @@ -61,7 +60,7 @@ class MLFPDatabase(FileListPadDatabase): def __init__( self, - name = "MLFP", + name = "MLFP NIR", original_directory = None, original_extension = ".hdf5", annotation_directory = None, @@ -98,22 +97,35 @@ class MLFPDatabase(FileListPadDatabase): file_path = os.path.join(self.annotation_directory, f.path + ".json") - # if file exists, load the annotations + # if annotations exist, load from file_path if os.path.isfile(file_path): with open(file_path, "r") as json_file: annotations = json.load(json_file) - if not annotations: # if dictionary is empty - logger.warning("Empty annotations for %s", f.path) - return None - - return annotations + else: + + # find and save annotations + annotations = {} + video = f.load(directory=self.original_directory, extension=self.original_extension) + + for idx, image in enumerate(video.as_array()): + frame_annotations = detect_face_landmarks_in_image(image, method=self.landmark_detect_method) + if frame_annotations: + annotations[str(idx)] = frame_annotations - else: + # save to file_path + create_directories_safe(directory=os.path.split(file_path)[0], dryrun=False) + with open(file_path, 'w+') as json_file: + json_file.write(json.dumps(annotations)) - logger.warning("Annotation file for %s does not exist. (Overall path: %s)", f.path, file_path) + + if not annotations: # if dictionary is empty + logger.warning("Empty annotations for {}".format(f.path)) return None + return annotations + + #------------------------------------------------------------------------------ diff --git a/bob/paper/nir_patch_pooling/database/wmca_mask.py b/bob/paper/nir_patch_pooling/database/wmca_mask.py index 5fa5a185da756294425f962b1cffe714e13ead25..ded9a6f94f342134946ead614b8f1f8b4047b5e9 100644 --- a/bob/paper/nir_patch_pooling/database/wmca_mask.py +++ b/bob/paper/nir_patch_pooling/database/wmca_mask.py @@ -7,16 +7,17 @@ Implementation of dataset interface of WMCA Masks for PAD. """ # Imports -from bob.pad.base.database import FileListPadDatabase, PadFile +from bob.pad.base.database import FileListPadDatabase from bob.pad.face.database.batl import BatlPadFile from bob.db.batl.models import VideoFile from bob.extension import rc -from bob.bio.video.utils import FrameSelector, FrameContainer +from bob.bio.video.utils import FrameSelector from bob.pad.face.preprocessor.FaceCropAlign import detect_face_landmarks_in_image -import json import os -import bob.io.base +import json +from bob.io.base import create_directories_safe + import pkg_resources import logging @@ -42,26 +43,6 @@ class File(VideoFile): #------------------------------------------------------------------------------ - # auxiliary function to load existing preprocessed files stored as *.hdf5 - - def load_aux(self, directory=None, extension=None, - frame_selector=FrameSelector()): - - path = self.make_path(directory, extension) - - # if loading a preprocessed data - if path.endswith('hdf5'): - with bob.io.base.HDF5File(path) as f: - return FrameContainer(hdf5=f) - else: - raise NotImplementedError("No loading method for {} extension"\ - .format(extension)) - - -#------------------------------------------------------------------------------ -#------------------------------------------------------------------------------ - - class WMCAMask(FileListPadDatabase): """ @@ -76,6 +57,7 @@ class WMCAMask(FileListPadDatabase): original_extension = ".h5", protocol = "grandtest", annotation_directory = None, + landmark_detect_method = "mtcnn", pad_file_class = BatlPadFile, low_level_pad_file_class = File, **kwargs, @@ -139,14 +121,10 @@ class WMCAMask(FileListPadDatabase): type_id = int(info.split("_")[3]) pai_id = int(info.split("_")[4]) - video_file = self.low_level_pad_file_class( - path = path, - client_id = client_id, - session_id = session_id, - presenter_id = presenter_id, - type_id = type_id, - pai_id = pai_id - ) + video_file = self.low_level_pad_file_class(path = path, + client_id = client_id, session_id = session_id, + presenter_id = presenter_id, type_id = type_id, + pai_id = pai_id) video_pad_files.append(video_file) @@ -164,16 +142,16 @@ class WMCAMask(FileListPadDatabase): if groups is None: groups = ["train", "dev", "eval"] - # parse the protocol to extract necessary information - protocol = protocol - stream_type = "nir" - num_frames = 50 - # obtain the file list using the parent class's functionality files = super(WMCAMask, self).objects(groups=groups, protocol=protocol, purposes=purposes, model_ids=model_ids, **kwargs) # create objects for each file where the class is BATLPadFile + + # aux information + stream_type = "nir" + num_frames = 50 + files = [self.pad_file_class(f=f, stream_type=stream_type, max_frames=num_frames) for f in files] @@ -184,8 +162,8 @@ class WMCAMask(FileListPadDatabase): def annotations(self, f): """ Returns annotations for a given file object ``f``. - The annotations must be precomputed using the script provided with the - package. + If annotations do not exist, these will be computed and saved in annotation_ + directory. **Parameters:** @@ -209,21 +187,34 @@ class WMCAMask(FileListPadDatabase): file_path = os.path.join(self.annotation_directory, f.f.path + ".json") + # if annotations exist, load from file_path if os.path.isfile(file_path): with open(file_path, "r") as json_file: annotations = json.load(json_file) - if not annotations: # if dictionary is empty - logger.warning("Empty annotations for %s", f.path) - return None + else: - return annotations + # find and save annotations + annotations = {} + video = f.load(directory=self.original_directory, extension=self.original_extension) - else: - logger.warning("Annotation file for %s does not exist. (Overall path: %s)", f.path, file_path) + for idx, image in enumerate(video.as_array()): + frame_annotations = detect_face_landmarks_in_image(image, method=self.landmark_detect_method) + if frame_annotations: + annotations[str(idx)] = frame_annotations + + # save to file_path + create_directories_safe(directory=os.path.split(file_path)[0], dryrun=False) + with open(file_path, 'w+') as json_file: + json_file.write(json.dumps(annotations)) + + + if not annotations: # if dictionary is empty + logger.warning("Empty annotations for {}".format(f.path)) return None + return annotations #------------------------------------------------------------------------------