Shellbrady/LivePortrait5
0
1 2"""3Functions to compute distance ratios between specific pairs of facial landmarks4"""5 6import numpy as np7 8 9def calculate_distance_ratio(lmk: np.ndarray, idx1: int, idx2: int, idx3: int, idx4: int, eps: float = 1e-6) -> np.ndarray:10 """11 Calculate the ratio of the distance between two pairs of landmarks.12 13 Parameters:14 lmk (np.ndarray): Landmarks array of shape (B, N, 2).15 idx1, idx2, idx3, idx4 (int): Indices of the landmarks.16 eps (float): Small value to avoid division by zero.17 18 Returns:19 np.ndarray: Calculated distance ratio.20 """21 return (np.linalg.norm(lmk[:, idx1] - lmk[:, idx2], axis=1, keepdims=True) /22 (np.linalg.norm(lmk[:, idx3] - lmk[:, idx4], axis=1, keepdims=True) + eps))23 24 25def calc_eye_close_ratio(lmk: np.ndarray, target_eye_ratio: np.ndarray = None) -> np.ndarray:26 """27 Calculate the eye-close ratio for left and right eyes.28 29 Parameters:30 lmk (np.ndarray): Landmarks array of shape (B, N, 2).31 target_eye_ratio (np.ndarray, optional): Additional target eye ratio array to include.32 33 Returns:34 np.ndarray: Concatenated eye-close ratios.35 """36 lefteye_close_ratio = calculate_distance_ratio(lmk, 6, 18, 0, 12)37 righteye_close_ratio = calculate_distance_ratio(lmk, 30, 42, 24, 36)38 if target_eye_ratio is not None:39 return np.concatenate([lefteye_close_ratio, righteye_close_ratio, target_eye_ratio], axis=1)40 else:41 return np.concatenate([lefteye_close_ratio, righteye_close_ratio], axis=1)42 43 44def calc_lip_close_ratio(lmk: np.ndarray) -> np.ndarray:45 """46 Calculate the lip-close ratio.47 48 Parameters:49 lmk (np.ndarray): Landmarks array of shape (B, N, 2).50 51 Returns:52 np.ndarray: Calculated lip-close ratio.53 """54 return calculate_distance_ratio(lmk, 90, 102, 48, 66)55 