utils module

Utilities available across all scripts

exception lib.utils.FaceswapError

Bases: Exception

Faceswap Error for handling specific errors with useful information.

Raises:FaceswapError – on a captured error
class lib.utils.GetModel(model_filename, cache_dir, git_model_id)

Bases: object

Check for models in their cache path.

If available, return the path, if not available, get, unzip and install model

  • model_filename (str or list) – The name of the model to be loaded (see notes below)
  • cache_dir (str) – The model cache folder of the current plugin calling this class. IE: The folder that holds the model to be loaded.
  • git_model_id (int) – The second digit in the github tag that identifies this model. See https://github.com/deepfakes-models/faceswap-models for more information


Models must have a certain naming convention: <model_name>_v<version_number>.<extension> (eg: s3fd_v1.pb).

Multiple models can exist within the model_filename. They should be passed as a list and follow the same naming convention as above. Any differences in filename should occur AFTER the version number: <model_name>_v<version_number><differentiating_information>.<extension> (eg: [“mtcnn_det_v1.1.py”, “mtcnn_det_v1.2.py”, “mtcnn_det_v1.3.py”], [“resnet_ssd_v1.caffemodel” ,”resnet_ssd_v1.prototext”]


The model path(s) in the cache folder.

class lib.utils.KerasFinder

Bases: importlib.abc.MetaPathFinder

Importlib Abstract Base Class for intercepting the import of Keras and returning either Keras (AMD backend) or tensorflow.keras (any other backend).

The Importlib documentation is sparse at best, and real world examples are pretty much non-existent. Coupled with this, the import tensorflow.keras does not resolve so we need to split out to the actual location of Keras within tensorflow_core. This method works, but it relies on hard coded paths, and is likely to not be the most robust.

A custom loader is not used, as we can use the standard loader once we have returned the correct spec.

find_spec(fullname, path, target=None)

Obtain the spec for either keras or tensorflow.keras depending on the backend in use.

If keras is not passed in as part of the fullname or the path is not None (i.e this is a dependency import) then this returns None to use the standard import library.

  • fullname (str) – The absolute name of the module to be imported
  • path (str) – The search path for the module
  • target (module object, optional) – Inherited from parent but unused

The spec for the Keras module to be imported

Return type:



Split a camel case name

Parameters:identifier (str) – The camel case text to be split
Returns:A list of the given identifier split into it’s constituent parts
Return type:list




Convert a time to seconds.

Parameters:args (tuple) – 2 or 3 ints. If 2 ints are supplied, then (minutes, seconds) is implied. If 3 ints are supplied then (hours, minutes, seconds) is implied.
Returns:The given time converted to seconds
Return type:int
lib.utils.deprecation_warning(function, additional_info=None)

Log at warning level that a function will be removed in a future update.

  • function (str) – The function that will be deprecated.
  • additional_info (str, optional) – Any additional information to display with the deprecation message. Default: None

Split a full path to a location into all of it’s separate components.

Parameters:path (str) – The full path to be split
Returns:The full path split into a separate item for each part
Return type:list


>>> path = "/foo/baz/bar"
>>> full_path_split(path)
>>> ["foo", "baz", "bar"]

Get the backend that Faceswap is currently configured to use.

Returns:The backend configuration in use by Faceswap
Return type:str
lib.utils.get_folder(path, make_folder=True)

Return a path to a folder, creating it if it doesn’t exist

  • path (str) – The path to the folder to obtain
  • make_folder (bool, optional) – True if the folder should be created if it does not already exist, False if the folder should not be created

The path to the requested folder. If make_folder is set to False and the requested path does not exist, then None is returned

Return type:

pathlib.Path or None

lib.utils.get_image_paths(directory, extension=None)

Obtain a list of full paths that reside within a folder.

  • directory (str) – The folder that contains the images to be returned
  • extension (str) – The specific image extensions that should be returned

The list of full paths to the images contained within the given folder

Return type:



Close all tracked queues and threads in event of crash or on shut down.

Parameters:got_error (bool, optional) – True if this function is being called as the result of raised error, otherwise False. Default: False

Override the configured backend with the given backend.

Parameters:backend (["amd", "cpu", "nvidia"]) – The backend to set faceswap to

Set the verbosity level of tensorflow and suppresses future and deprecation warnings from any modules

Parameters:log_level (str) – The requested Faceswap log level


https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information Can be set to: 0: all logs shown. 1: filter out INFO logs. 2: filter out WARNING logs. 3: filter out ERROR logs.