Faces

class tools.alignments.media.Faces(folder: str, alignments: Alignments | None = None)

Bases: MediaLoader

Object to load Extracted Faces from a folder.

Parameters:
  • folder (str) – The folder to load faces from

  • alignments (Alignments | None) – The alignments object that contains the faces. This can be used for 2 purposes: - To update legacy hash based faces for <v2.1 alignments to png header based version. - When the remove-faces job is being run, when the process will only load faces that exist in the alignments file. Default: None

Attributes Summary

count

Number of faces or frames

is_video

Whether source is a video or not

Methods Summary

check_input_folder()

Ensure that the frames or faces folder exists and is valid.

load_image(filename)

Load an image

load_items()

Load the face names into dictionary.

load_video_frame(filename)

Load a requested frame from video

process_folder()

Iterate through the faces folder pulling out various information for each face.

save_image(output_folder, filename, image[, ...])

Save an image

sorted_items()

Return the items sorted by the saved file name.

stream([skip_list])

Load the images in folder in the order they are received from lib.image.ImagesLoader in a background thread.

valid_extension(filename)

Check whether passed in file has a valid extension

Attributes Documentation

count

Number of faces or frames

is_video

Whether source is a video or not

Methods Documentation

check_input_folder() VideoCapture | None

Ensure that the frames or faces folder exists and is valid. If frames folder contains a video file return cv2 reader object

Return type:

Object for reading a video stream

load_image(filename: str) np.ndarray

Load an image

Parameters:

filename (str) – The filename of the image to load

Return type:

The loaded image

load_items() dict[str, list[int]]

Load the face names into dictionary.

Return type:

The source filename as key with list of face indices for the frame as value

load_video_frame(filename: str) np.ndarray

Load a requested frame from video

Parameters:

filename (str) – The frame name to load

Return type:

The loaded image

process_folder() Generator[tuple[str, PNGHeader], None, None]

Iterate through the faces folder pulling out various information for each face.

Yields:
Return type:

Generator[tuple[str, PNGHeader], None, None]

static save_image(output_folder: str, filename: str, image: np.ndarray, metadata: PNGHeader | None = None) None

Save an image

Parameters:
  • filename (str) – The filename of the image to save

  • image (np.ndarray) – The image to save

  • metadata (PNGHeader | None) – Any faceswap metadata that should be saved

  • output_folder (str)

Return type:

None

sorted_items() list[tuple[str, PNGHeader]]

Return the items sorted by the saved file name.

Return type:

List of dict objects for each face found, sorted by the face’s current filename

stream(skip_list: list[int] | None = None) Generator[tuple[str, np.ndarray], None, None]

Load the images in folder in the order they are received from lib.image.ImagesLoader in a background thread.

Parameters:

skip_list (list[int] | None) – A list of frame indices that should not be loaded. Pass None if all images should be loaded. Default: None

Yields:
  • filename – The filename of the image that is being returned

  • image – The image that has been loaded from disk

Return type:

Generator[tuple[str, np.ndarray], None, None]

static valid_extension(filename) bool

Check whether passed in file has a valid extension

Return type:

bool

load_items() dict[str, list[int]]

Load the face names into dictionary.

Return type:

The source filename as key with list of face indices for the frame as value

process_folder() Generator[tuple[str, PNGHeader], None, None]

Iterate through the faces folder pulling out various information for each face.

Yields:
Return type:

Generator[tuple[str, PNGHeader], None, None]

sorted_items() list[tuple[str, PNGHeader]]

Return the items sorted by the saved file name.

Return type:

List of dict objects for each face found, sorted by the face’s current filename