Writer

class plugins.convert.writer.patch.Writer(output_folder: str, patch_size: int, **kwargs)

Bases: Output

Face patch writer for outputting swapped face patches and transformation matrices

Parameters:
  • output_folder (str) – The full path to the output folder where the face patches should be saved

  • patch_size (int) – The size of the face patch output from the model

  • config_file (str, optional) – The full path to a custom configuration ini file. If None is passed then the file is loaded from the default location. Default: None.

Attributes Summary

is_stream

Whether the writer outputs a stream or a series images.

output_alpha

Override if the plugin can output an alpha channel and the user configuration option is set to use it.

Methods Summary

cache_frame(filename, image)

Add the incoming converted frame to the cache ready for writing out.

close()

Outputs json file if requested

get_output_filename(filename, extension[, ...])

Obtain the full path for the output file, including the correct extension, for the given input filename.

pre_encode(image, **kwargs)

Pre_encode the image in lib/convert.py threads as it is a LOT quicker.

write(filename, image)

Write out the pre-encoded image to disk.

Attributes Documentation

is_stream

Whether the writer outputs a stream or a series images.

Writers that write to a stream have a frame_order parameter to dictate the order in which frames should be written out (eg. gif/ffmpeg)

output_alpha

Override if the plugin can output an alpha channel and the user configuration option is set to use it. Default False

Methods Documentation

cache_frame(filename: str, image: ndarray) None

Add the incoming converted frame to the cache ready for writing out.

Used for ffmpeg and gif writers to ensure that the frames are written out in the correct order.

Parameters:
  • filename (str) – The filename of the incoming frame, where the frame index can be extracted from

  • image (ndarray) – The converted frame corresponding to the given filename

Return type:

None

close() None

Outputs json file if requested

Return type:

None

get_output_filename(filename: str, extension: str, separate_mask: bool = False) list[str]

Obtain the full path for the output file, including the correct extension, for the given input filename.

Parameters:
  • filename (str) – The input frame filename to generate the output file name for

  • extension (str) – The extension to use for the output file

  • separate_mask (bool) – True if the mask should be saved out to a sub-folder otherwise False

Returns:

  • The full path for the output converted frame to be saved to in position 1. The full path

  • for the mask to be output to in position 2 (if requested)

Return type:

list[str]

pre_encode(image: ndarray, **kwargs) list[list[bytes]]

Pre_encode the image in lib/convert.py threads as it is a LOT quicker.

Parameters:
  • image (numpy.ndarray) – A 3 or 4 channel BGR swapped face batch as float32

  • canvas_size (tuple[int, int]) – The size of the canvas (x, y) that the transformation matrix applies to.

  • matrices (numpy.ndarray, optional) – The transformation matrices for extracting the face patches from the original frame. Must be provided if an image is provided, otherwise None to insert a dummy matrix

Returns:

List of bytes objects ready for writing. The list will be of length 1 with image bytes object as the only member unless separate mask has been requested, in which case it will be length 2 with the image in position 0 and mask in position 1

Return type:

list

write(filename: str, image: list[list[bytes]]) None

Write out the pre-encoded image to disk. If separate mask has been selected, write out the encoded mask to a sub-folder in the output directory.

Parameters:
  • filename (str) – The full path to write out the image to.

  • image (list[list[bytes]]) – List of list of bytes objects of containing all swapped faces from a frame to write out. The inner list will be of length 1 (mask included in the alpha channel) or length 2 (mask to write out separately)

Return type:

None

close() None

Outputs json file if requested

Return type:

None

pre_encode(image: ndarray, **kwargs) list[list[bytes]]

Pre_encode the image in lib/convert.py threads as it is a LOT quicker.

Parameters:
  • image (numpy.ndarray) – A 3 or 4 channel BGR swapped face batch as float32

  • canvas_size (tuple[int, int]) – The size of the canvas (x, y) that the transformation matrix applies to.

  • matrices (numpy.ndarray, optional) – The transformation matrices for extracting the face patches from the original frame. Must be provided if an image is provided, otherwise None to insert a dummy matrix

Returns:

List of bytes objects ready for writing. The list will be of length 1 with image bytes object as the only member unless separate mask has been requested, in which case it will be length 2 with the image in position 0 and mask in position 1

Return type:

list

write(filename: str, image: list[list[bytes]]) None

Write out the pre-encoded image to disk. If separate mask has been selected, write out the encoded mask to a sub-folder in the output directory.

Parameters:
  • filename (str) – The full path to write out the image to.

  • image (list[list[bytes]]) – List of list of bytes objects of containing all swapped faces from a frame to write out. The inner list will be of length 1 (mask included in the alpha channel) or length 2 (mask to write out separately)

Return type:

None