garmentiq.landmark.refine
1import numpy as np 2import cv2 3import copy 4from garmentiq.landmark.refinement import refine_landmark_with_blur 5from garmentiq.landmark.utils import ( 6 find_instruction_landmark_index, 7 fill_instruction_landmark_coordinate, 8) 9 10 11def refine( 12 class_name: str, 13 detection_np: np.array, 14 detection_conf: np.array, 15 detection_dict: dict, 16 mask: np.array, 17 window_size: int = 5, 18 ksize: tuple = (11, 11), 19 sigmaX: float = 0.0, 20): 21 """ 22 Refines detected landmarks using a blurred mask and updates the detection dictionary. 23 24 This function applies Gaussian blur to the given mask, then refines landmark coordinates 25 based on their confidence scores and local intensity structure. Only landmarks with a 26 confidence score greater than 0 are refined. The refined coordinates are used to update 27 predefined landmarks in the detection dictionary. 28 29 Args: 30 class_name (str): The name of the class to access in the detection dictionary. 31 detection_np (np.array): The initial landmark predictions. Shape: (1, N, 2). 32 detection_conf (np.array): Confidence scores for each predicted landmark. Shape: (1, N, 1). 33 detection_dict (dict): Dictionary containing landmark data for each class. 34 mask (np.array): Grayscale mask image used to guide refinement. 35 window_size (int, optional): Size of the window used in the refinement algorithm. Defaults to 5. 36 ksize (tuple, optional): Kernel size for Gaussian blur. Must be odd integers. Defaults to (11, 11). 37 sigmaX (float, optional): Gaussian kernel standard deviation in the X direction. Defaults to 0.0. 38 39 Returns: 40 tuple: 41 - refined_detection_np (np.array): Array of the same shape as `detection_np` with refined coordinates. 42 - detection_dict (dict): Updated detection dictionary with refined landmark coordinates. 43 """ 44 blurred_mask = cv2.GaussianBlur(mask, ksize, sigmaX) 45 46 refined_detection_np = np.zeros(detection_np.shape) 47 48 for i, coord in enumerate(detection_np[0]): 49 if detection_conf[0, i, 0] > 0: 50 refined_x, refined_y = refine_landmark_with_blur( 51 coord[0], coord[1], blurred_mask, window_size 52 ) 53 refined_detection_np[0, i] = [refined_x, refined_y] 54 55 predefined_index = find_instruction_landmark_index( 56 detection_dict[class_name]["landmarks"], predefined=True 57 ) 58 59 preds = refined_detection_np[:, predefined_index, :] 60 61 detection_dict_copy = copy.deepcopy(detection_dict) 62 63 detection_dict_copy[class_name]["landmarks"] = fill_instruction_landmark_coordinate( 64 instruction_landmarks=detection_dict_copy[class_name]["landmarks"], 65 index=predefined_index, 66 fill_in_value=preds, 67 ) 68 69 return refined_detection_np, detection_dict_copy
def
refine( class_name: str, detection_np: <built-in function array>, detection_conf: <built-in function array>, detection_dict: dict, mask: <built-in function array>, window_size: int = 5, ksize: tuple = (11, 11), sigmaX: float = 0.0):
12def refine( 13 class_name: str, 14 detection_np: np.array, 15 detection_conf: np.array, 16 detection_dict: dict, 17 mask: np.array, 18 window_size: int = 5, 19 ksize: tuple = (11, 11), 20 sigmaX: float = 0.0, 21): 22 """ 23 Refines detected landmarks using a blurred mask and updates the detection dictionary. 24 25 This function applies Gaussian blur to the given mask, then refines landmark coordinates 26 based on their confidence scores and local intensity structure. Only landmarks with a 27 confidence score greater than 0 are refined. The refined coordinates are used to update 28 predefined landmarks in the detection dictionary. 29 30 Args: 31 class_name (str): The name of the class to access in the detection dictionary. 32 detection_np (np.array): The initial landmark predictions. Shape: (1, N, 2). 33 detection_conf (np.array): Confidence scores for each predicted landmark. Shape: (1, N, 1). 34 detection_dict (dict): Dictionary containing landmark data for each class. 35 mask (np.array): Grayscale mask image used to guide refinement. 36 window_size (int, optional): Size of the window used in the refinement algorithm. Defaults to 5. 37 ksize (tuple, optional): Kernel size for Gaussian blur. Must be odd integers. Defaults to (11, 11). 38 sigmaX (float, optional): Gaussian kernel standard deviation in the X direction. Defaults to 0.0. 39 40 Returns: 41 tuple: 42 - refined_detection_np (np.array): Array of the same shape as `detection_np` with refined coordinates. 43 - detection_dict (dict): Updated detection dictionary with refined landmark coordinates. 44 """ 45 blurred_mask = cv2.GaussianBlur(mask, ksize, sigmaX) 46 47 refined_detection_np = np.zeros(detection_np.shape) 48 49 for i, coord in enumerate(detection_np[0]): 50 if detection_conf[0, i, 0] > 0: 51 refined_x, refined_y = refine_landmark_with_blur( 52 coord[0], coord[1], blurred_mask, window_size 53 ) 54 refined_detection_np[0, i] = [refined_x, refined_y] 55 56 predefined_index = find_instruction_landmark_index( 57 detection_dict[class_name]["landmarks"], predefined=True 58 ) 59 60 preds = refined_detection_np[:, predefined_index, :] 61 62 detection_dict_copy = copy.deepcopy(detection_dict) 63 64 detection_dict_copy[class_name]["landmarks"] = fill_instruction_landmark_coordinate( 65 instruction_landmarks=detection_dict_copy[class_name]["landmarks"], 66 index=predefined_index, 67 fill_in_value=preds, 68 ) 69 70 return refined_detection_np, detection_dict_copy
Refines detected landmarks using a blurred mask and updates the detection dictionary.
This function applies Gaussian blur to the given mask, then refines landmark coordinates based on their confidence scores and local intensity structure. Only landmarks with a confidence score greater than 0 are refined. The refined coordinates are used to update predefined landmarks in the detection dictionary.
Arguments:
- class_name (str): The name of the class to access in the detection dictionary.
- detection_np (np.array): The initial landmark predictions. Shape: (1, N, 2).
- detection_conf (np.array): Confidence scores for each predicted landmark. Shape: (1, N, 1).
- detection_dict (dict): Dictionary containing landmark data for each class.
- mask (np.array): Grayscale mask image used to guide refinement.
- window_size (int, optional): Size of the window used in the refinement algorithm. Defaults to 5.
- ksize (tuple, optional): Kernel size for Gaussian blur. Must be odd integers. Defaults to (11, 11).
- sigmaX (float, optional): Gaussian kernel standard deviation in the X direction. Defaults to 0.0.
Returns:
tuple: - refined_detection_np (np.array): Array of the same shape as
detection_np
with refined coordinates. - detection_dict (dict): Updated detection dictionary with refined landmark coordinates.