garmentiq.utils.clean_detection_dict
Trimming a detection record down to its reportable fields.
1"""Trimming a detection record down to its reportable fields.""" 2import os 3from garmentiq.utils import compute_measurement_distances 4 5 6def clean_detection_dict(class_name: str, image_name: str, detection_dict: dict): 7 """ 8 Cleans and reformats a detection dictionary, computes measurement distances, 9 and nests the cleaned data under the image name, with class name stored inside. 10 11 Args: 12 class_name (str): The name of the garment class. 13 image_name (str): The original filename of the image. 14 detection_dict (dict): The raw detection dictionary containing landmark and measurement data. 15 16 Returns: 17 dict: A dictionary structured as {image_name: {..., "class": class_name}}. 18 """ 19 20 transformed_name = os.path.splitext(image_name)[0] 21 22 # Compute distances and get a fresh copy of detection_dict 23 _, clean_dict = compute_measurement_distances(detection_dict) 24 25 # Safely extract and clean the content under the class_name 26 original_data = clean_dict.get(class_name, {}) 27 28 # Clean landmarks 29 if "landmarks" in original_data: 30 for lm_id in list(original_data["landmarks"].keys()): 31 lm = original_data["landmarks"][lm_id] 32 original_data["landmarks"][lm_id] = { 33 k: lm[k] for k in ("x", "y", "conf") if k in lm 34 } 35 36 # Clean measurements 37 if "measurements" in original_data: 38 for m_id in list(original_data["measurements"].keys()): 39 m = original_data["measurements"][m_id] 40 original_data["measurements"][m_id] = { 41 k: m[k] for k in ("landmarks", "distance") if k in m 42 } 43 44 # Insert the class name as metadata 45 original_data["class"] = class_name 46 47 # Return dict keyed by image name 48 final_dict = {image_name: original_data} 49 return final_dict
def
clean_detection_dict(class_name: str, image_name: str, detection_dict: dict):
7def clean_detection_dict(class_name: str, image_name: str, detection_dict: dict): 8 """ 9 Cleans and reformats a detection dictionary, computes measurement distances, 10 and nests the cleaned data under the image name, with class name stored inside. 11 12 Args: 13 class_name (str): The name of the garment class. 14 image_name (str): The original filename of the image. 15 detection_dict (dict): The raw detection dictionary containing landmark and measurement data. 16 17 Returns: 18 dict: A dictionary structured as {image_name: {..., "class": class_name}}. 19 """ 20 21 transformed_name = os.path.splitext(image_name)[0] 22 23 # Compute distances and get a fresh copy of detection_dict 24 _, clean_dict = compute_measurement_distances(detection_dict) 25 26 # Safely extract and clean the content under the class_name 27 original_data = clean_dict.get(class_name, {}) 28 29 # Clean landmarks 30 if "landmarks" in original_data: 31 for lm_id in list(original_data["landmarks"].keys()): 32 lm = original_data["landmarks"][lm_id] 33 original_data["landmarks"][lm_id] = { 34 k: lm[k] for k in ("x", "y", "conf") if k in lm 35 } 36 37 # Clean measurements 38 if "measurements" in original_data: 39 for m_id in list(original_data["measurements"].keys()): 40 m = original_data["measurements"][m_id] 41 original_data["measurements"][m_id] = { 42 k: m[k] for k in ("landmarks", "distance") if k in m 43 } 44 45 # Insert the class name as metadata 46 original_data["class"] = class_name 47 48 # Return dict keyed by image name 49 final_dict = {image_name: original_data} 50 return final_dict
Cleans and reformats a detection dictionary, computes measurement distances, and nests the cleaned data under the image name, with class name stored inside.
Arguments:
- class_name (str): The name of the garment class.
- image_name (str): The original filename of the image.
- detection_dict (dict): The raw detection dictionary containing landmark and measurement data.
Returns:
dict: A dictionary structured as {image_name: {..., "class": class_name}}.