def _update_file_format(self, folder, filename): """ Convert old style serialized alignments to new ``.fsa`` format. Parameters ---------- folder: str The folder that the legacy alignments exist in filename: str The file name of the legacy alignments Returns ------- str The full path to the newly created ``.fsa`` alignments file """ logger.info("Reformatting legacy alignments file...") old_location = os.path.join(str(folder), filename) new_location = "{}.{}".format(os.path.splitext(old_location)[0], self._serializer.file_extension) if os.path.exists(old_location): if os.path.exists(new_location): logger.info("Using existing updated alignments file found at '%s'. If you do not " "wish to use this existing file then you should delete or rename it.", new_location) else: logger.info("Old location: '%s', New location: '%s'", old_location, new_location) load_serializer = get_serializer_from_filename(old_location) data = load_serializer.load(old_location) self._serializer.save(new_location, data) return os.path.basename(new_location)
def process(self): """ Main processing function of the sort tool """ # Setting default argument values that cannot be set by argparse # Set output dir to the same value as input dir # if the user didn't specify it. if self.args.output_dir is None: logger.verbose("No output directory provided. Using input dir as output dir.") self.args.output_dir = self.args.input_dir # Assigning default threshold values based on grouping method if (self.args.final_process == "folders" and self.args.min_threshold < 0.0): method = self.args.group_method.lower() if method == 'face-cnn': self.args.min_threshold = 7.2 elif method == 'hist': self.args.min_threshold = 0.3 # Load VGG Face if sorting by face if self.args.sort_method.lower() == "face": conf = Config("global", configfile=self.args.configfile) allow_growth = (conf.config_dict["allow_growth"] and self.args.backend.lower() == "gpu" and get_backend() == "nvidia") self.vgg_face = VGGFace(backend=self.args.backend, allow_growth=allow_growth, loglevel=self.args.loglevel) # If logging is enabled, prepare container if self.args.log_changes: self.changes = dict() # Assign default sort_log.json value if user didn't specify one if self.args.log_file_path == 'sort_log.json': self.args.log_file_path = os.path.join(self.args.input_dir, 'sort_log.json') # Set serializer based on logfile extension self.serializer = get_serializer_from_filename(self.args.log_file_path) # Prepare sort, group and final process method names _sort = "sort_" + self.args.sort_method.lower() _group = "group_" + self.args.group_method.lower() _final = "final_process_" + self.args.final_process.lower() if _sort.startswith('sort_color-'): self.args.color_method = _sort.replace('sort_color-', '') _sort = _sort[:10] self.args.sort_method = _sort.replace('-', '_') self.args.group_method = _group.replace('-', '_') self.args.final_process = _final.replace('-', '_') self.sort_process()
def update_file_format(self, folder, filename): """ Convert old style alignments format to new style format """ logger.info("Reformatting legacy alignments file...") old_location = os.path.join(str(folder), filename) new_location = "{}.{}".format( os.path.splitext(old_location)[0], self.serializer.file_extension) if os.path.exists(old_location): if os.path.exists(new_location): logger.info( "Using existing updated alignments file found at '%s'. If you do not " "wish to use this existing file then you should delete or rename it.", new_location) else: logger.info("Old location: '%s', New location: '%s'", old_location, new_location) load_serializer = get_serializer_from_filename(old_location) data = load_serializer.load(old_location) self.serializer.save(new_location, data) return os.path.basename(new_location)
def get_serializer(filename, serializer): """ Set the serializer to be used for loading and saving alignments If a filename with a valid extension is passed in this will be used as the serializer, otherwise the specified serializer will be used """ logger.debug("Getting serializer: (filename: '%s', serializer: '%s')", filename, serializer) extension = os.path.splitext(filename)[1] if extension in (".json", ".p", ".yaml", ".yml"): logger.debug("Serializer set from filename extension: '%s'", extension) retval = get_serializer_from_filename(filename) elif serializer not in ("json", "pickle", "yaml"): raise ValueError("Error: {} is not a valid serializer. Use " "'json', 'pickle' or 'yaml'") else: logger.debug("Serializer set from argument: '%s'", serializer) retval = get_serializer(serializer) logger.verbose("Using '%s' serializer for alignments", retval.file_extension) return retval