Esempio n. 1
0
def _to_cuboid_annotation(cuboid: Dict[str, Any], classes: List[Dict[str, Any]]) -> Annotation:
    points: Dict[str, Dict[str, float]] = cast(Dict[str, Dict[str, float]], cuboid.get("points"))
    back_top_left_point: Dict[str, float] = cast(Dict[str, float], points.get("r1"))
    back_bottom_right_point: Dict[str, float] = cast(Dict[str, float], points.get("r2"))
    front_top_left_point: Dict[str, float] = cast(Dict[str, float], points.get("f1"))
    front_bottom_right_point: Dict[str, float] = cast(Dict[str, float], points.get("f2"))

    cuboid_data: CuboidData = {
        "back": {
            "h": abs(cast(float, back_top_left_point.get("y")) - cast(float, back_bottom_right_point.get("y"))),
            "w": abs(cast(float, back_bottom_right_point.get("x")) - cast(float, back_top_left_point.get("x"))),
            "x": cast(float, back_top_left_point.get("x")),
            "y": cast(float, back_top_left_point.get("y")),
        },
        "front": {
            "h": abs(cast(float, front_top_left_point.get("y")) - cast(float, front_bottom_right_point.get("y"))),
            "w": abs(cast(float, front_bottom_right_point.get("x")) - cast(float, front_top_left_point.get("x"))),
            "x": cast(float, front_top_left_point.get("x")),
            "y": cast(float, front_top_left_point.get("y")),
        },
    }
    class_id: int = cast(int, cuboid.get("classId"))

    instance_class: Dict[str, Any] = _find_class(class_id, classes)
    name: str = str(instance_class.get("name"))
    attributes: Optional[SubAnnotation] = _get_attributes(cuboid, instance_class)
    subannotations: Optional[List[SubAnnotation]] = None
    if attributes:
        subannotations = [attributes]

    return make_cuboid(f"{name}-cuboid", cuboid_data, subannotations)
Esempio n. 2
0
def parse_darwin_annotation(
        annotation: Dict[str, Any]) -> Optional[dt.Annotation]:
    name: str = annotation["name"]
    main_annotation: Optional[dt.Annotation] = None
    if "polygon" in annotation:
        bounding_box = annotation.get("bounding_box")
        if "additional_paths" in annotation["polygon"]:
            paths = [annotation["polygon"]["path"]
                     ] + annotation["polygon"]["additional_paths"]
            main_annotation = dt.make_complex_polygon(name, paths,
                                                      bounding_box)
        else:
            main_annotation = dt.make_polygon(name,
                                              annotation["polygon"]["path"],
                                              bounding_box)
    elif "complex_polygon" in annotation:
        bounding_box = annotation.get("bounding_box")
        if "additional_paths" in annotation["complex_polygon"]:
            paths = annotation["complex_polygon"]["path"] + annotation[
                "complex_polygon"]["additional_paths"]
            main_annotation = dt.make_complex_polygon(name, paths,
                                                      bounding_box)
        else:
            main_annotation = dt.make_complex_polygon(
                name, annotation["complex_polygon"]["path"], bounding_box)
    elif "bounding_box" in annotation:
        bounding_box = annotation["bounding_box"]
        main_annotation = dt.make_bounding_box(name, bounding_box["x"],
                                               bounding_box["y"],
                                               bounding_box["w"],
                                               bounding_box["h"])
    elif "tag" in annotation:
        main_annotation = dt.make_tag(name)
    elif "line" in annotation:
        main_annotation = dt.make_line(name, annotation["line"]["path"])
    elif "keypoint" in annotation:
        main_annotation = dt.make_keypoint(name, annotation["keypoint"]["x"],
                                           annotation["keypoint"]["y"])
    elif "ellipse" in annotation:
        main_annotation = dt.make_ellipse(name, annotation["ellipse"])
    elif "cuboid" in annotation:
        main_annotation = dt.make_cuboid(name, annotation["cuboid"])
    elif "skeleton" in annotation:
        main_annotation = dt.make_skeleton(name,
                                           annotation["skeleton"]["nodes"])

    if not main_annotation:
        print(f"[WARNING] Unsupported annotation type: '{annotation.keys()}'")
        return None

    if "instance_id" in annotation:
        main_annotation.subs.append(
            dt.make_instance_id(annotation["instance_id"]["value"]))
    if "attributes" in annotation:
        main_annotation.subs.append(
            dt.make_attributes(annotation["attributes"]))
    if "text" in annotation:
        main_annotation.subs.append(dt.make_text(annotation["text"]["text"]))

    return main_annotation