tools package

The Tools Package provides various tools for working with Faceswap outside of the core functionality.

alignments module

Tools for manipulating the alignments serialized file

class tools.alignments.alignments.Alignments(arguments)

Bases: object

The main entry point for Faceswap’s Alignments Tool. This tool is part of the Faceswap Tools suite and should be called from the python tools.py alignments command.

The tool allows for manipulation, and working with Faceswap alignments files.

Parameters:arguments (argparse.Namespace) – The argparse arguments as passed in from tools.py
process()

The entry point for the Alignments tool from lib.tools.alignments.cli.

Launches the selected alignments job.

mask module

Tool to generate masks and previews of masks for existing alignments file

class tools.mask.mask.Mask(arguments)

Bases: object

This tool is part of the Faceswap Tools suite and should be called from python tools.py mask command.

Faceswap Masks tool. Generate masks from existing alignments files, and output masks for preview.

Parameters:arguments (argparse.Namespace) – The argparse arguments as passed in from tools.py
process()

The entry point for the Mask tool from lib.tools.cli. Runs the Mask process

preview module

Module Summary

ActionFrame Frame that holds the left hand side options panel containing the command line options.
ConfigFrame Holds the configuration options for a convert plugin inside the OptionsBook.
ConfigTools Tools for loading, saving, setting and retrieving configuration file values.
FacesDisplay Compiles the 2 rows of sample faces (original and swapped) into a single image
ImagesCanvas tkinter Canvas that holds the preview images.
OptionsBook The notebook that holds the Convert configuration options.
Patch The Patch pipeline
Preview This tool is part of the Faceswap Tools suite and should be called from python tools.py preview command.
Samples The display samples.

Module

Tool to preview swaps and tweak configuration prior to running a convert

class tools.preview.preview.ActionFrame(parent, available_masks, has_predicted_mask, selected_color, selected_mask_type, config_tools, patch_callback, refresh_callback, tk_vars)

Bases: tkinter.ttk.Frame

Frame that holds the left hand side options panel containing the command line options.

Parameters:
  • parent (tkinter object) – The parent tkinter object that holds the Action Frame
  • available_masks (list) – The available masks that exist within the alignments file
  • has_predicted_mask (bool) – Whether the model was trained with a mask
  • selected_color (str) – The selected color adjustment type
  • selected_mask_type (str) – The selected mask type
  • config_tools (ConfigTools) – Tools for loading and saving configuration files
  • patch_callback (python function) – The function to execute when a patch callback is received
  • refresh_callback (python function) – The function to execute when a refresh callback is received
  • tk_vars (dict) – Global tkinter variables. Refresh and Busy tkinter.BooleanVar
convert_args

Currently selected Command line arguments from the ActionFrame.

Type:dict
class tools.preview.preview.ConfigFrame(parent, config_key, options)

Bases: tkinter.ttk.Frame

Holds the configuration options for a convert plugin inside the OptionsBook.

Parameters:
  • parent (tkinter object) – The tkinter object that will hold this configuration frame
  • config_key (str) – The section/plugin key for these configuration options
  • options (dict) – The options for this section/plugin
class tools.preview.preview.ConfigTools

Bases: object

Tools for loading, saving, setting and retrieving configuration file values.

tk_vars

Global tkinter variables. Refresh and Busy tkinter.BooleanVar

Type:dict
config

plugins.convert._config.Config The convert configuration

config_dicts

The convert configuration options in dictionary form.

Type:dict
plugins_dict

Dictionary of configuration option sections as key with a list of containing plugins as the value

Type:dict
reset_config_to_default(section=None)

Reset the GUI parameters to their default configuration values.

Parameters:section (str, optional) – The configuration section to reset the values for, If None provided then all sections are reset. Default: None
reset_config_to_saved(section=None)

Reset the GUI parameters to their saved values within the configuration file.

Parameters:section (str, optional) – The configuration section to reset the values for, If None provided then all sections are reset. Default: None
save_config(section=None)

Save the configuration .ini file with the currently stored values.

Parameters:section (str, optional) – The configuration section to save, If None provided then all sections are saved. Default: None
sections

The sorted section names that exist within the convert Configuration options.

Type:list
update_config()

Update config with the currently selected values from the GUI.

class tools.preview.preview.FacesDisplay(size, padding, tk_vars)

Bases: object

Compiles the 2 rows of sample faces (original and swapped) into a single image

Parameters:
  • size (int) – The size of each individual face sample in pixels
  • padding (int) – The amount of extra padding to apply to the outside of the face
  • tk_vars (dict) – Global tkinter variables. Refresh and Busy tkinter.BooleanVar
update_source

Flag to indicate that the source images for the preview have been updated, so the preview should be recompiled.

Type:bool
source

The list of numpy.ndarray source preview images for top row of display

Type:list
destination

The list of numpy.ndarray swapped and patched preview images for bottom row of display

Type:list
set_centering(centering)

The centering that the model uses is not known at initialization time. Set _centering when the model has been loaded.

Parameters:centering (str) – The centering that the model was trained on
set_display_dimensions(dimensions)

Adjust the size of the frame that will hold the preview samples.

Parameters:dimensions (tuple) – The (width, height) of the frame that holds the preview
tk_image

The compiled preview display in tkinter display format

Type:PIL.ImageTk.PhotoImage
update_tk_image()

Build the full preview images and compile tk_image for display.

class tools.preview.preview.ImagesCanvas(parent, tk_vars)

Bases: tkinter.ttk.Frame

tkinter Canvas that holds the preview images.

Parameters:
  • parent (tkinter object) – The parent tkinter object that holds the canvas
  • tk_vars (dict) – Global tkinter variables. Refresh and Busy tkinter.BooleanVar
class tools.preview.preview.OptionsBook(parent, config_tools, patch_callback)

Bases: tkinter.ttk.Notebook

The notebook that holds the Convert configuration options.

Parameters:
  • parent (tkinter object) – The parent tkinter object that holds the Options book
  • config_tools (ConfigTools) – Tools for loading and saving configuration files
  • patch_callback (python function) – The function to execute when a patch callback is received
config_tools

Tools for loading and saving configuration files

Type:ConfigTools
class tools.preview.preview.Patch(arguments, available_masks, samples, display, lock, trigger, config_tools, tk_vars)

Bases: object

The Patch pipeline

Runs in it’s own thread. Takes the output from the Faceswap model predictor and runs the faces through the convert pipeline using the currently selected options.

Parameters:
  • arguments (argparse.Namespace) – The argparse arguments as passed in from tools.py
  • available_masks (list) – The masks that are available for convert
  • samples (Samples) – The Samples for display.
  • display (FacesDisplay) – The display section of the Preview GUI.
  • lock (threading.Lock) – A threading lock to prevent multiple GUI updates at the same time.
  • trigger (threading.Event) – An event to indicate that a converter patch should be run
  • config_tools (ConfigTools) – Tools for loading and saving configuration files
  • tk_vars (dict) – Global tkinter variables. Refresh and Busy tkinter.BooleanVar
converter_arguments

The currently selected converter command line arguments for the patch queue

Type:dict
current_config

The currently set configuration for the patch queue

Type:lib.config.FaceswapConfig
converter

The converter to use for patching the images.

Type:lib.convert.Converter
trigger

The trigger to indicate that a patching run should commence.

Type:threading.Event
class tools.preview.preview.Preview(arguments)

Bases: tkinter.Tk

This tool is part of the Faceswap Tools suite and should be called from python tools.py preview command.

Loads up 5 semi-random face swaps and displays them, cropped, in place in the final frame. Allows user to live tweak settings, before saving the final config to ./config/convert.ini

Parameters:arguments (argparse.Namespace) – The argparse arguments as passed in from tools.py
process()

The entry point for the Preview tool from lib.tools.cli.

Launch the tkinter preview Window and run main loop.

class tools.preview.preview.Samples(arguments, sample_size, display, lock, trigger_patch)

Bases: object

The display samples.

Obtains and holds sample_size semi random test faces for displaying in the preview GUI.

The file list is split into evenly sized groups of sample_size. When a display set is generated, a random image from each of the groups is selected to provide an array of images across the length of the video.

Parameters:
  • arguments (argparse.Namespace) – The argparse arguments as passed in from tools.py
  • sample_size (int) – The number of samples to take from the input video/images
  • display (FacesDisplay) – The display section of the Preview GUI.
  • lock (threading.Lock) – A threading lock to prevent multiple GUI updates at the same time.
  • trigger_patch (threading.Event) – An event to indicate that a converter patch should be run
alignments

The alignments for the preview faces

Type:Alignments
generate()

Generate a sample set.

Selects sample_size random faces. Runs them through prediction to obtain the swap, then trigger the patch event to run the faces through patching.

predicted_images

The predicted faces output from the Faceswap model

Type:list
predictor

The Predictor for the Faceswap model

Type:Predict
sample_size

The number of samples to take from the input video/images

Type:int