from typing import OrderedDict


def create_object_detection_label(annot_dict):
    """
    Uses an annotation (parsed to a dictionary from an XML file) to create a
    python dictionary that represents a label for the object detection task.

    The resulting dictionary has the following format:
    {
        "image-size": {"depth": <depth:int>, "width": <width:int>, "height": <height:int>}
        "objects": [
            {
                "name": "<object-name>",
                "bndbox": [xmin:int, xmax:int, ymin:int, ymax:int],
                "difficult": <difficult:int>}
            },
            ...,
        ]
    }
    """

    annotation = annot_dict['annotation']

    annotation_objects = annotation["object"]
    if isinstance(annotation_objects, OrderedDict):
        annotation_objects = [annotation_objects]
    
    objects = []
    for annot_object in annotation_objects:
        obj = {"name": annot_object["name"]}
        obj["bndbox"] = [
            annot_object["bndbox"]["xmin"],
            annot_object["bndbox"]["xmax"],
            annot_object["bndbox"]["ymin"],
            annot_object["bndbox"]["ymax"],
            ]
        obj["bndbox"] = [round(float(x)) for x in obj["bndbox"]]
        obj["difficult"] = annot_object["difficult"] == '1'
        objects.append(obj)

    label = {
        "image-size": {k: int(v) for k, v in annotation["size"].items()},
        "objects": objects
    }

    return label