scripts package

The Scripts Package is the entry point into Faceswap.

extract module

Main entry point to the extract process of FaceSwap

class scripts.extract.Extract(arguments)

Bases: object

The Faceswap Face Extraction Process.

The extraction process is responsible for detecting faces in a series of images/video, aligning these faces and then generating a mask.

It leverages a series of user selected plugins, chained together using plugins.extract.pipeline.

The extract process is self contained and should not be referenced by any other scripts, so it contains no public properties.

Parameters:arguments (argparse.Namespace) – The arguments to be passed to the extraction process as generated from Faceswap’s command line arguments
process()

The entry point for triggering the Extraction Process.

Should only be called from lib.cli.launcher.ScriptExecutor

train module

Main entry point to the training process of FaceSwap

class scripts.train.Train(arguments)

Bases: object

The Faceswap Training Process.

The training process is responsible for training a model on a set of source faces and a set of destination faces.

The training process is self contained and should not be referenced by any other scripts, so it contains no public properties.

Parameters:arguments (argparse.Namespace) – The arguments to be passed to the training process as generated from Faceswap’s command line arguments
process()

The entry point for triggering the Training Process.

Should only be called from lib.cli.launcher.ScriptExecutor

convert module

Module Summary

Convert The Faceswap Face Conversion Process.
DiskIO Disk Input/Output for the converter process.
OptionalActions Process specific optional actions for Convert.
Predict Obtains the output from the Faceswap model.

Module

Main entry point to the convert process of FaceSwap

class scripts.convert.Convert(arguments)

Bases: object

The Faceswap Face Conversion Process.

The conversion process is responsible for swapping the faces on source frames with the output from a trained model.

It leverages a series of user selected post-processing plugins, executed from lib.convert.Converter.

The convert process is self contained and should not be referenced by any other scripts, so it contains no public properties.

Parameters:arguments (argparse.Namespace) – The arguments to be passed to the convert process as generated from Faceswap’s command line arguments
process()

The entry point for triggering the Conversion Process.

Should only be called from lib.cli.launcher.ScriptExecutor

class scripts.convert.DiskIO(alignments, images, arguments)

Bases: object

Disk Input/Output for the converter process.

Background threads to:
  • Load images from disk and get the detected faces
  • Save images back to disk
Parameters:
  • alignments (lib.alignmnents.Alignments) – The alignments for the input video
  • images (lib.image.ImagesLoader) – The input images
  • arguments (argparse.Namespace) – The arguments that were passed to the convert process as generated from Faceswap’s command line arguments
completion_event

Event is set when the DiskIO Save task is complete

Type:event.Event
draw_transparent

True if the selected writer’s Draw_transparent configuration item is set otherwise False

Type:bool
load_queue

The queue that images and detected faces are loaded into.

Type:queue.Queue()
load_thread

The thread that is running the image loading operation.

Type:lib.multithreading.MultiThread
pre_encode

Selected writer’s pre-encode function, if it has one, otherwise None

Type:python function
save_thread

The thread that is running the image writing operation.

Type:lib.multithreading.MultiThread
class scripts.convert.OptionalActions(arguments, input_images, alignments)

Bases: object

Process specific optional actions for Convert.

Currently only handles skip faces. This class should probably be (re)moved.

Parameters:
  • arguments (argparse.Namespace) – The arguments that were passed to the convert process as generated from Faceswap’s command line arguments
  • input_images (list) – List of input image files
  • alignments (lib.align.Alignments) – The alignments file for this conversion
class scripts.convert.Predict(in_queue, queue_size, arguments)

Bases: object

Obtains the output from the Faceswap model.

Parameters:
  • in_queue (queue.Queue) – The queue that contains images and detected faces for feeding the model
  • queue_size (int) – The maximum size of the input queue
  • arguments (argparse.Namespace) – The arguments that were passed to the convert process as generated from Faceswap’s command line arguments
centering

The centering that the model was trained on (“face” or “legacy”)

Type:str
coverage_ratio

The coverage ratio that the model was trained at.

Type:float
faces_count

The total number of faces seen by the Predictor.

Type:int
has_predicted_mask

True if the model was trained to learn a mask, otherwise False.

Type:bool
in_queue

The input queue to the predictor.

Type:queue.Queue
load_aligned(item)

Load the model’s feed faces and the reference output faces.

For each detected face in the incoming item, load the feed face and reference face images, correctly sized for input and output respectively.

Parameters:item (dict) – The incoming image, list of DetectedFace objects and list of AlignedFace objects for the feed face(s) and list of AlignedFace objects for the reference face(s)
out_queue

The output queue from the predictor.

Type:queue.Queue
output_size

The size in pixels of the Faceswap model output.

Type:int
thread

The thread that is running the prediction function from the Faceswap model.

Type:MultiThread
verify_output

True if multiple faces have been found in frames, otherwise False.

Type:bool

fsmedia module

Module Summary

Alignments Override lib.align.Alignments to add custom loading based on command line arguments.
DebugLandmarks Draw debug landmarks on face output.
FaceFilter Filter in or out faces based on input image(s).
Images Handles the loading of frames from a folder of images or a video file for extract and convert processes.
PostProcess Optional pre/post processing tasks for convert and extract.
finalize Output summary statistics at the end of the extract or convert processes.

Module

Helper functions for extract and convert.

Holds the classes for the 2 main Faceswap ‘media’ objects: Images and Alignments.

Holds optional pre/post processing functions for convert and extract.

class scripts.fsmedia.Alignments(arguments, is_extract, input_is_video=False)

Bases: lib.align.alignments.Alignments

Override lib.align.Alignments to add custom loading based on command line arguments.

Parameters:
  • arguments (argparse.Namespace) – The command line arguments that were passed to Faceswap
  • is_extract (bool) – True if the process calling this class is extraction otherwise False
  • input_is_video (bool, optional) – True if the input to the process is a video, False if it is a folder of images. Default: False
class scripts.fsmedia.DebugLandmarks(*args, **kwargs)

Bases: scripts.fsmedia.PostProcessAction

Draw debug landmarks on face output. Extract Only

process(extract_media)

Draw landmarks on a face.

Parameters:extract_media (ExtractMedia) – The ExtractMedia object that contains the faces to draw the landmarks on to
Returns:The original ExtractMedia with landmarks drawn onto the face
Return type:ExtractMedia
class scripts.fsmedia.FaceFilter(*args, **kwargs)

Bases: scripts.fsmedia.PostProcessAction

Filter in or out faces based on input image(s). Extract or Convert

Parameters:
  • args (tuple) – Unused
  • kwargs (dict) –

    Keyword arguments for face filter:

    • detector (str) - The detector to use
    • aligner (str) - The aligner to use
    • multiprocess (bool) - Whether to run the extraction pipeline in single process mode or not
    • ref_threshold (float) - The reference threshold for a positive match
    • filter_lists (dict) - The filter and nfilter image paths
process(extract_media)

Filters in or out any wanted or unwanted faces based on command line arguments.

Parameters:extract_media (ExtractMedia) – The ExtractMedia object to perform the face filtering on.
Returns:The original ExtractMedia with any requested filters applied
Return type:ExtractMedia
class scripts.fsmedia.Images(arguments)

Bases: object

Handles the loading of frames from a folder of images or a video file for extract and convert processes.

Parameters:arguments (argparse.Namespace) – The command line arguments that were passed to Faceswap
images_found

The number of frames that exist in the video file, or the folder of images.

Type:int
input_images

Path to the video file if the input is a video otherwise list of image paths.

Type:str or list
is_video

True if the input is a video file otherwise False.

Type:bool
load()

Generator to load frames from a folder of images or from a video file.

Yields:
  • filename (str) – The filename of the current frame
  • image (numpy.ndarray) – A single frame
load_one_image(filename)

Obtain a single image for the given filename.

Parameters:filename (str) – The filename to return the image for
Returns:The image for the requested filename,
Return type:numpy.ndarray
class scripts.fsmedia.PostProcess(arguments)

Bases: object

Optional pre/post processing tasks for convert and extract.

Builds a pipeline of actions that have optionally been requested to be performed in this session.

Parameters:arguments (argparse.Namespace) – The command line arguments that were passed to Faceswap
do_actions(extract_media)

Perform the requested optional post-processing actions on the given image.

Parameters:extract_media (ExtractMedia) – The ExtractMedia object to perform the action on.
Returns:The original ExtractMedia with any actions applied
Return type:ExtractMedia
class scripts.fsmedia.PostProcessAction(*args, **kwargs)

Bases: object

Parent class for Post Processing Actions.

Usable in Extract or Convert or both depending on context. Any post-processing actions should inherit from this class.

Parameters:
  • args (tuple) – Varies for specific post process action
  • kwargs (dict) – Varies for specific post process action
process(extract_media)

Override for specific post processing action

Parameters:extract_media (ExtractMedia) – The ExtractMedia object to perform the action on.
valid

True if the action if the parameters passed in for this action are valid, otherwise False

Type:bool
scripts.fsmedia.finalize(images_found, num_faces_detected, verify_output)

Output summary statistics at the end of the extract or convert processes.

Parameters:
  • images_found (int) – The number of images/frames that were processed
  • num_faces_detected (int) – The number of faces that have been detected
  • verify_output (bool) – True if multiple faces were detected in frames otherwise False.