Exemple #1
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
Exemple #2
0
def _to_tag_annotations_from_checklist(question: str,
                                       checklist) -> List[Annotation]:
    annotations: List[Annotation] = []
    for answer in checklist:
        val: str = answer.get("value")
        annotations.append(make_tag(f"{question}:{val}"))

    return annotations
Exemple #3
0
def parse_path(path: Path) -> Optional[List[dt.AnnotationFile]]:
    if path.suffix != ".csv":
        return None

    files = []

    file_annotation_map = {}
    with path.open() as f:
        reader = csv.reader(f)
        for row in reader:
            try:
                filename, tag, start_frame, end_frame = map(
                    lambda s: s.strip(), row)
            except ValueError:
                continue
            if filename == "":
                continue

            start_frame = int(start_frame)
            end_frame = int(end_frame)

            annotation = dt.make_tag(tag)
            frames = {i: annotation for i in range(start_frame, end_frame + 1)}
            keyframes = {
                i: i == start_frame
                for i in range(start_frame, end_frame + 1)
            }

            annotation = dt.make_video_annotation(frames, keyframes,
                                                  [[start_frame, end_frame]],
                                                  False)
            if filename not in file_annotation_map:
                file_annotation_map[filename] = []
            file_annotation_map[filename].append(annotation)
    for filename in file_annotation_map:
        annotations = file_annotation_map[filename]
        annotation_classes = set(
            [annotation.annotation_class for annotation in annotations])
        files.append(
            dt.AnnotationFile(path,
                              filename,
                              annotation_classes,
                              annotations,
                              is_video=True,
                              remote_path="/"))
    return files
def parse_file(path: Path) -> Optional[List[dt.AnnotationFile]]:
    if path.suffix != ".csv":
        return

    files = []
    with path.open() as f:
        reader = csv.reader(f)
        for row in reader:
            filename, *tags = map(lambda s: s.strip(), row)
            if filename == "":
                continue
            annotations = [dt.make_tag(tag) for tag in tags if len(tag) > 0]
            annotation_classes = set(
                [annotation.annotation_class for annotation in annotations])
            files.append(
                dt.AnnotationFile(path, filename, annotation_classes,
                                  annotations))
    return files
Exemple #5
0
def parse_json(path, data):
    annotations = data["annotations"]
    image_lookup_table = {image["id"]: image for image in data["images"]}
    category_lookup_table = {
        category["id"]: category
        for category in data["categories"]
    }
    image_annotations = {}
    image_tags = {}

    for annotation in annotations:
        image_id = annotation["image_id"]
        annotation["category_id"]
        annotation["segmentation"]
        if image_id not in image_annotations:
            image_annotations[image_id] = []
        image_annotations[image_id].append(
            parse_annotation(annotation, category_lookup_table))

    for tag in data["tag_categories"]:
        image_id = tag["image_id"]
        if image_id not in image_tags:
            image_tags[image_id] = []
        image_tags[image_id].append(dt.make_tag(tag["name"]))

    for image_id in image_annotations.keys():
        image = image_lookup_table[image_id]
        annotations = list(filter(None, image_annotations[image_id]))
        annotation_classes = set(
            [annotation.annotation_class for annotation in annotations])
        yield dt.AnnotationFile(path, image["file_name"], annotation_classes,
                                annotations)

    for image_id in image_tags.keys():
        print(image_id, image_tags[image_id])
        image = image_lookup_table[image_id]
        annotation_classes = set([
            annotation.annotation_class for annotation in image_tags[image_id]
        ])
        yield dt.AnnotationFile(path, image["file_name"], annotation_classes,
                                image_tags[image_id])
Exemple #6
0
def _to_tag_annotations_from_free_text(question: str,
                                       free_text: str) -> Annotation:
    return make_tag(question,
                    [SubAnnotation(annotation_type="text", data=free_text)])
Exemple #7
0
def _to_tag_annotations_from_radio_box(
        question: str, radio_button: Dict[str, Any]) -> Annotation:
    answer: str = str(radio_button.get("value"))
    return make_tag(f"{question}:{answer}")