garmentiq.utils.clean_detection_dict

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

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}}.