utils

utils

Functions

Name Description
aggregate_embeddings Aggregates multiple embeddings into a single embedding.
annotate_img_with_kps Annotate an image with keypoints.
compute_ss_ds Compute cosine similarities between the cartesian product of two arrays X and Z and
cosine_score Compute cosine similarity between two 1D embeddings.
transform_keypoints Transforms keypoints from the original image space to the aligned image space.

aggregate_embeddings

utils.aggregate_embeddings(embeddings, weights=None, method='mean')

Aggregates multiple embeddings into a single embedding.

Parameters

embeddings : np.ndarray

A 2D array of shape (num_embeddings, embedding_dim) containing the embeddings to be aggregated.

weights : np.ndarray | None = None

A 1D array of shape (num_embeddings,) containing the weights to assign to each embedding. If not provided, all embeddings are equally weighted.

method : str = 'mean'

Aggregation method. Possible values are "mean" and "median".

Returns

: np.ndarray

np.ndarray: A 1D array of shape (embedding_dim,) containing the

: np.ndarray

aggregated embedding.

annotate_img_with_kps

utils.annotate_img_with_kps(
    bgr_img,
    kps,
    colors=DEFAULT_KEYPOINT_COLORS,
    radius=2,
)

Annotate an image with keypoints.

Parameters: bgr_img (numpy.ndarray): The input image in BGR format. kps (numpy.ndarray): A numpy array of shape (5, 2) containing the keypoints. colors (tuple[str, str, str, str, str], optional): The color of each keypoint. By default, keypoint index 1 is red and the others are green. Options are ‘red’, ‘blue’, ‘green’, ‘white’, ‘black’. radius (int, optional): The radius of the keypoints. Default is 2.

Returns: numpy.ndarray: The image with keypoints annotated.

compute_ss_ds

utils.compute_ss_ds(X, x_id, x_names=None, Z=None, z_id=None, z_names=None)

Compute cosine similarities between the cartesian product of two arrays X and Z and return same-source (ss) and different-source (ds) scores. If only the array X and x_id are provided, compute the cosine similarities between all pairwise combination in X. Also return the names of the files associated with each score, is x_names and z_names are provided.

Parameters

X : np.ndarray

2D numpy array with one embedding per row.

x_id : np.ndarray

1D numpy array with identity labels for X.

x_names : np.ndarray | None = None

Optional 1D numpy array with names of files associated with the embeddings in X.

Z : np.ndarray | None = None

Optional 2D numpy array with one embedding per row.

z_id : np.ndarray | None = None

Optional 1D numpy array with identity labels for Z.

z_names : np.ndarray | None = None

Optional 1D numpy array with names of files associated with the embeddings in Z.

Returns

: np.ndarray

tuple[np.ndarray, np.ndarray, list[tuple] | None]: Scores, same-source

: np.ndarray

and different-source labels, and optional file-name pairs.

cosine_score

utils.cosine_score(x, z)

Compute cosine similarity between two 1D embeddings.

transform_keypoints

utils.transform_keypoints(keypoints, M)

Transforms keypoints from the original image space to the aligned image space.

Parameters

keypoints : np.ndarray

A 2D array of shape (5, 2) representing the original keypoints.

M : np.ndarray

The 2x3 affine transformation matrix.

Returns

: np.ndarray

np.ndarray: A 2D array of shape (5, 2) representing the transformed keypoints.