Alignments
- class lib.align.alignments.Alignments(folder: str, filename: str = 'alignments')
Bases:
objectThe alignments file is a custom serialized
.fsafile that holds information for each frame for a video or series of images.Specifically, it holds a list of faces that appear in each frame. Each face contains information detailing their detected bounding box location within the frame, the 68 point facial landmarks and any masks that have been extracted.
Additionally it can also hold video meta information (timestamp and whether a frame is a key frame.)
- Parameters:
folder (str) – The folder that contains the alignments
.fsafilefilename (str) – The filename of the
.fsaalignments file. If not provided then the given folder will be checked for a default alignments file filename. Default: “alignments”
Attributes Summary
The loaded alignments
filein dictionary form.The total number of faces that appear in the alignments
dataThe full path to the currently loaded alignments file.
The number of frames that appear in the alignments
data.Trueif an alignments file exists at locationfileotherwiseFalse.The mask type names stored in the alignments
dataas key with the number of faces which possess the mask type as value.The low resolution thumbnail images that exist within the alignments file
The alignments file version number.
The frame meta data stored in the alignments file.
Methods Summary
add_face(frame_name, face)Add a new face for the given frame_name in
dataand return it's index.backup()Create a backup copy of the alignments
file.count_faces_in_frame(frame_name)Return number of faces that appear within
datafor the given frame_name.delete_face_at_index(frame_name, face_index)Delete the face for the given frame_name at the given face index from
data.filter_faces(filter_dict[, filter_out])Remove faces from
databased on a given filter list.frame_exists(frame_name)Check whether a given frame_name exists within the alignments
data.frame_has_faces(frame_name)Check whether a given frame_name exists within the alignments
dataand contains at least 1 face.frame_has_multiple_faces(frame_name)Check whether a given frame_name exists within the alignments
dataand contains more than 1 face.get_faces_in_frame(frame_name)Obtain the faces from
dataassociated with a given frame_name.mask_is_valid(mask_type)Ensure the given
mask_typeis valid for the alignmentsdata.save()Write the contents of
dataand_metato a serialized.fsafile at the locationfile.save_video_meta_data(pts_time, keyframes)Save video meta data to the alignments file.
update_face(frame_name, face_index, face)Update the face for the given frame_name at the given face index in
data.update_from_dict(data)Replace all alignments with the contents of the given dictionary
update_legacy_has_source(filename)Update legacy alignments files when we have the source filename available.
Generator to obtain all faces with meta information from
data.Attributes Documentation
- file
The full path to the currently loaded alignments file.
- mask_summary
The mask type names stored in the alignments
dataas key with the number of faces which possess the mask type as value.
- thumbnails
The low resolution thumbnail images that exist within the alignments file
- version
The alignments file version number.
- Type:
float
- video_meta_data
The frame meta data stored in the alignments file. If data does not exist in the alignments file then
Noneis returned
Methods Documentation
- add_face(frame_name: str, face: FileAlignments) int
Add a new face for the given frame_name in
dataand return it’s index.- Parameters:
frame_name (str) – The frame name to add the face to. This should be the base name of the frame, not the full path
face (FileAlignments) – The face information to add to the given frame_name, correctly formatted for storing in
data
- Return type:
The index of the newly added face within
datafor the given frame_name
- backup() None
Create a backup copy of the alignments
file.Creates a copy of the serialized alignments
fileappending a timestamp onto the end of the file name and storing in the same folder as the originalfile.- Return type:
None
- count_faces_in_frame(frame_name: str) int
Return number of faces that appear within
datafor the given frame_name.- Parameters:
frame_name (str) – The frame name to return the count for. This should be the base name of the frame, not the full path
- Return type:
The number of faces that appear in the given frame_name
- delete_face_at_index(frame_name: str, face_index: int) bool
Delete the face for the given frame_name at the given face index from
data.- Parameters:
frame_name (str) – The frame name to remove the face from. This should be the base name of the frame, not the full path
face_index (int) – The index number of the face within the given frame_name to remove
- Return type:
Trueif a face was successfully deleted otherwiseFalse
- filter_faces(filter_dict: dict[str, list[int]], filter_out: bool = False) None
Remove faces from
databased on a given filter list.- Parameters:
filter_dict (dict[str, list[int]]) – Dictionary of source filenames as key with a list of face indices to filter as value.
filter_out (bool) –
Trueif faces should be removed fromdatawhen there is a corresponding match in the given filter_dict.Falseif faces should be kept indatawhen there is a corresponding match in the given filter_dict, but removed if there is no match. Default:False
- Return type:
None
- frame_exists(frame_name: str) bool
Check whether a given frame_name exists within the alignments
data.- Parameters:
frame_name (str) – The frame name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataotherwiseFalse
- Return type:
bool
- frame_has_faces(frame_name: str) bool
Check whether a given frame_name exists within the alignments
dataand contains at least 1 face.- Parameters:
frame_name (str) – The frame name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataand has at least1 face associated with it, otherwise
False
- Return type:
bool
- frame_has_multiple_faces(frame_name: str) bool
Check whether a given frame_name exists within the alignments
dataand contains more than 1 face.- Parameters:
frame_name (str) – The frame_name name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataand has morethan 1 face associated with it, otherwise
False
- Return type:
bool
- get_faces_in_frame(frame_name: str) list[FileAlignments]
Obtain the faces from
dataassociated with a given frame_name.- Parameters:
frame_name (str) – The frame name to return faces for. This should be the base name of the frame, not the full path
- Return type:
The list of face dictionaries that appear within the requested frame_name
- mask_is_valid(mask_type: str) bool
Ensure the given
mask_typeis valid for the alignmentsdata.Every face in the alignments
datamust have the given mask type to successfully pass the test.- Parameters:
mask_type (str) – The mask type to check against the current alignments
data- Returns:
Trueif all faces in the current alignments possess the givenmask_typeotherwiseFalse
- Return type:
bool
- save() None
Write the contents of
dataand_metato a serialized.fsafile at the locationfile.- Return type:
None
- save_video_meta_data(pts_time: list[int], keyframes: list[int]) None
Save video meta data to the alignments file.
If the alignments file does not have an entry for every frame (e.g. if Extract Every N was used) then the frame is added to the alignments file with no faces, so that they video meta data can be stored.
- Parameters:
pts_time (list[int]) – A list of presentation timestamps (int) in frame index order for every frame in the input video
keyframes (list[int]) – A list of frame indices corresponding to the key frames in the input video
- Return type:
None
- update_face(frame_name: str, face_index: int, face: FileAlignments) None
Update the face for the given frame_name at the given face index in
data.- Parameters:
frame_name (str) – The frame name to update the face for. This should be the base name of the frame, not the full path
face_index (int) – The index number of the face within the given frame_name to update
face (FileAlignments) – The face information to update to the given frame_name at the given face_index, correctly formatted for storing in
data
- Return type:
None
- update_from_dict(data: dict[str, AlignmentsEntry]) None
Replace all alignments with the contents of the given dictionary
- Parameters:
data (dict[str, AlignmentsEntry]) – The alignments, in correctly formatted dictionary form, to be populated into this
Alignments- Return type:
None
- update_legacy_has_source(filename: str) None
Update legacy alignments files when we have the source filename available.
Updates here can only be performed when we have the source filename
- Parameters:
filename (str) – The filename/folder of the original source images/video for the current alignments
- Return type:
None
- yield_faces() Generator[tuple[str, list[FileAlignments], int, str], None, None]
Generator to obtain all faces with meta information from
data. The results are yielded by frame.Notes
The yielded order is non-deterministic.
- Yields:
frame_name – The frame name that the face belongs to. This is the base name of the frame, as it appears in
data, not the full pathfaces – The list of face dict objects that exist for this frame
face_count – The number of faces that exist within
datafor this frameframe_fullname – The full path (folder and filename) for the yielded frame
- Return type:
Generator[tuple[str, list[FileAlignments], int, str], None, None]
- add_face(frame_name: str, face: FileAlignments) int
Add a new face for the given frame_name in
dataand return it’s index.- Parameters:
frame_name (str) – The frame name to add the face to. This should be the base name of the frame, not the full path
face (FileAlignments) – The face information to add to the given frame_name, correctly formatted for storing in
data
- Return type:
The index of the newly added face within
datafor the given frame_name
- backup() None
Create a backup copy of the alignments
file.Creates a copy of the serialized alignments
fileappending a timestamp onto the end of the file name and storing in the same folder as the originalfile.- Return type:
None
- count_faces_in_frame(frame_name: str) int
Return number of faces that appear within
datafor the given frame_name.- Parameters:
frame_name (str) – The frame name to return the count for. This should be the base name of the frame, not the full path
- Return type:
The number of faces that appear in the given frame_name
- property data: dict[str, AlignmentsEntry]
The loaded alignments
filein dictionary form.
- delete_face_at_index(frame_name: str, face_index: int) bool
Delete the face for the given frame_name at the given face index from
data.- Parameters:
frame_name (str) – The frame name to remove the face from. This should be the base name of the frame, not the full path
face_index (int) – The index number of the face within the given frame_name to remove
- Return type:
Trueif a face was successfully deleted otherwiseFalse
- property file: str
The full path to the currently loaded alignments file.
- filter_faces(filter_dict: dict[str, list[int]], filter_out: bool = False) None
Remove faces from
databased on a given filter list.- Parameters:
filter_dict (dict[str, list[int]]) – Dictionary of source filenames as key with a list of face indices to filter as value.
filter_out (bool) –
Trueif faces should be removed fromdatawhen there is a corresponding match in the given filter_dict.Falseif faces should be kept indatawhen there is a corresponding match in the given filter_dict, but removed if there is no match. Default:False
- Return type:
None
- frame_exists(frame_name: str) bool
Check whether a given frame_name exists within the alignments
data.- Parameters:
frame_name (str) – The frame name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataotherwiseFalse
- Return type:
bool
- frame_has_faces(frame_name: str) bool
Check whether a given frame_name exists within the alignments
dataand contains at least 1 face.- Parameters:
frame_name (str) – The frame name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataand has at least1 face associated with it, otherwise
False
- Return type:
bool
- frame_has_multiple_faces(frame_name: str) bool
Check whether a given frame_name exists within the alignments
dataand contains more than 1 face.- Parameters:
frame_name (str) – The frame_name name to check. This should be the base name of the frame, not the full path
- Returns:
Trueif the given frame_name exists within the alignmentsdataand has morethan 1 face associated with it, otherwise
False
- Return type:
bool
- get_faces_in_frame(frame_name: str) list[FileAlignments]
Obtain the faces from
dataassociated with a given frame_name.- Parameters:
frame_name (str) – The frame name to return faces for. This should be the base name of the frame, not the full path
- Return type:
The list of face dictionaries that appear within the requested frame_name
- property have_alignments_file: bool
Trueif an alignments file exists at locationfileotherwiseFalse.
- mask_is_valid(mask_type: str) bool
Ensure the given
mask_typeis valid for the alignmentsdata.Every face in the alignments
datamust have the given mask type to successfully pass the test.- Parameters:
mask_type (str) – The mask type to check against the current alignments
data- Returns:
Trueif all faces in the current alignments possess the givenmask_typeotherwiseFalse
- Return type:
bool
- property mask_summary: dict[str, int]
The mask type names stored in the alignments
dataas key with the number of faces which possess the mask type as value.
- save() None
Write the contents of
dataand_metato a serialized.fsafile at the locationfile.- Return type:
None
- save_video_meta_data(pts_time: list[int], keyframes: list[int]) None
Save video meta data to the alignments file.
If the alignments file does not have an entry for every frame (e.g. if Extract Every N was used) then the frame is added to the alignments file with no faces, so that they video meta data can be stored.
- Parameters:
pts_time (list[int]) – A list of presentation timestamps (int) in frame index order for every frame in the input video
keyframes (list[int]) – A list of frame indices corresponding to the key frames in the input video
- Return type:
None
- property thumbnails: Thumbnails
The low resolution thumbnail images that exist within the alignments file
- update_face(frame_name: str, face_index: int, face: FileAlignments) None
Update the face for the given frame_name at the given face index in
data.- Parameters:
frame_name (str) – The frame name to update the face for. This should be the base name of the frame, not the full path
face_index (int) – The index number of the face within the given frame_name to update
face (FileAlignments) – The face information to update to the given frame_name at the given face_index, correctly formatted for storing in
data
- Return type:
None
- update_from_dict(data: dict[str, AlignmentsEntry]) None
Replace all alignments with the contents of the given dictionary
- Parameters:
data (dict[str, AlignmentsEntry]) – The alignments, in correctly formatted dictionary form, to be populated into this
Alignments- Return type:
None
- update_legacy_has_source(filename: str) None
Update legacy alignments files when we have the source filename available.
Updates here can only be performed when we have the source filename
- Parameters:
filename (str) – The filename/folder of the original source images/video for the current alignments
- Return type:
None
- property version: float
The alignments file version number.
- Type:
float
- property video_meta_data: dict[Literal['pts_time', 'keyframes'], list[int]] | None
The frame meta data stored in the alignments file. If data does not exist in the alignments file then
Noneis returned
- yield_faces() Generator[tuple[str, list[FileAlignments], int, str], None, None]
Generator to obtain all faces with meta information from
data. The results are yielded by frame.Notes
The yielded order is non-deterministic.
- Yields:
frame_name – The frame name that the face belongs to. This is the base name of the frame, as it appears in
data, not the full pathfaces – The list of face dict objects that exist for this frame
face_count – The number of faces that exist within
datafor this frameframe_fullname – The full path (folder and filename) for the yielded frame
- Return type:
Generator[tuple[str, list[FileAlignments], int, str], None, None]