|
| 1 | +from collections import defaultdict |
| 2 | +from typing import List |
| 3 | + |
| 4 | + |
| 5 | +def map_annotation_classes_name(annotation_classes, logger=None) -> dict: |
| 6 | + classes_data = defaultdict(dict) |
| 7 | + for annotation_class in annotation_classes: |
| 8 | + class_info = {"id": annotation_class.uuid} |
| 9 | + if annotation_class.attribute_groups: |
| 10 | + for attribute_group in annotation_class.attribute_groups: |
| 11 | + attribute_group_data = defaultdict(dict) |
| 12 | + for attribute in attribute_group["attributes"]: |
| 13 | + if logger and attribute["name"] in attribute_group_data.keys(): |
| 14 | + logger.warning( |
| 15 | + f"Duplicate annotation class attribute name {attribute['name']}" |
| 16 | + f" in attribute group {attribute_group['name']}. " |
| 17 | + "Only one of the annotation class attributes will be used. " |
| 18 | + "This will result in errors in annotation upload." |
| 19 | + ) |
| 20 | + attribute_group_data[attribute["name"]] = attribute["id"] |
| 21 | + if logger and attribute_group["name"] in class_info.keys(): |
| 22 | + logger.warning( |
| 23 | + f"Duplicate annotation class attribute group name {attribute_group['name']}." |
| 24 | + " Only one of the annotation class attribute groups will be used." |
| 25 | + " This will result in errors in annotation upload." |
| 26 | + ) |
| 27 | + class_info["attribute_groups"] = { |
| 28 | + attribute_group["name"]: { |
| 29 | + "id": attribute_group["id"], |
| 30 | + "attributes": attribute_group_data, |
| 31 | + } |
| 32 | + } |
| 33 | + if logger and annotation_class.name in classes_data.keys(): |
| 34 | + logger.warning( |
| 35 | + f"Duplicate annotation class name {annotation_class.name}." |
| 36 | + f" Only one of the annotation classes will be used." |
| 37 | + " This will result in errors in annotation upload.", |
| 38 | + ) |
| 39 | + classes_data[annotation_class.name] = class_info |
| 40 | + return classes_data |
| 41 | + |
| 42 | + |
| 43 | +def fill_annotation_ids(annotations: dict, annotation_classes_name_maps: dict, templates: List[dict], logger=None): |
| 44 | + annotation_classes_name_maps = annotation_classes_name_maps |
| 45 | + if "instances" not in annotations: |
| 46 | + return |
| 47 | + missing_classes = set() |
| 48 | + missing_attribute_groups = set() |
| 49 | + missing_attributes = set() |
| 50 | + unknown_classes = dict() |
| 51 | + report = { |
| 52 | + "missing_classes": missing_classes, |
| 53 | + "missing_attribute_groups": missing_attribute_groups, |
| 54 | + "missing_attributes": missing_attributes, |
| 55 | + } |
| 56 | + for annotation in [i for i in annotations["instances"] if "className" in i]: |
| 57 | + if "className" not in annotation: |
| 58 | + return |
| 59 | + annotation_class_name = annotation["className"] |
| 60 | + if annotation_class_name not in annotation_classes_name_maps.keys(): |
| 61 | + if annotation_class_name not in unknown_classes: |
| 62 | + missing_classes.add(annotation_class_name) |
| 63 | + unknown_classes[annotation_class_name] = { |
| 64 | + "id": -(len(unknown_classes) + 1), |
| 65 | + "attribute_groups": {}, |
| 66 | + } |
| 67 | + annotation_classes_name_maps.update(unknown_classes) |
| 68 | + template_name_id_map = {template["name"]: template["id"] for template in templates} |
| 69 | + for annotation in ( |
| 70 | + i for i in annotations["instances"] if i.get("type", None) == "template" |
| 71 | + ): |
| 72 | + annotation["templateId"] = template_name_id_map.get( |
| 73 | + annotation.get("templateName", ""), -1 |
| 74 | + ) |
| 75 | + |
| 76 | + for annotation in [i for i in annotations["instances"] if "className" in i]: |
| 77 | + annotation_class_name = annotation["className"] |
| 78 | + if annotation_class_name not in annotation_classes_name_maps.keys(): |
| 79 | + if logger: |
| 80 | + logger.warning( |
| 81 | + f"Couldn't find annotation class {annotation_class_name}" |
| 82 | + ) |
| 83 | + continue |
| 84 | + annotation["classId"] = annotation_classes_name_maps[annotation_class_name]["id"] |
| 85 | + for attribute in annotation["attributes"]: |
| 86 | + if ( |
| 87 | + attribute["groupName"] |
| 88 | + not in annotation_classes_name_maps[annotation_class_name]["attribute_groups"] |
| 89 | + ): |
| 90 | + if logger: |
| 91 | + logger.warning( |
| 92 | + f"Couldn't find annotation group {attribute['groupName']}." |
| 93 | + ) |
| 94 | + missing_attribute_groups.add(attribute["groupName"]) |
| 95 | + continue |
| 96 | + attribute["groupId"] = annotation_classes_name_maps[annotation_class_name][ |
| 97 | + "attribute_groups" |
| 98 | + ][attribute["groupName"]]["id"] |
| 99 | + if ( |
| 100 | + attribute["name"] |
| 101 | + not in annotation_classes_name_maps[annotation_class_name][ |
| 102 | + "attribute_groups" |
| 103 | + ][attribute["groupName"]]["attributes"] |
| 104 | + ): |
| 105 | + del attribute["groupId"] |
| 106 | + if logger: |
| 107 | + logger.warning( |
| 108 | + f"Couldn't find annotation name {attribute['name']} in" |
| 109 | + f" annotation group {attribute['groupName']}", |
| 110 | + ) |
| 111 | + missing_attributes.add(attribute["name"]) |
| 112 | + continue |
| 113 | + attribute["id"] = annotation_classes_name_maps[annotation_class_name][ |
| 114 | + "attribute_groups" |
| 115 | + ][attribute["groupName"]]["attributes"][attribute["name"]] |
| 116 | + return report |
0 commit comments