def list_annotation_libraries():
     annotation_libraries = AnnotationLibrary.discover("data")
     return {
         "success": [True, None],
         "annotation_libraries":
         [al.as_json_minimal() for al in annotation_libraries]
     }
        def get_annotation_library(annotation_library_name):
            try:
                annotation_library = AnnotationLibrary.load(
                    annotation_library_name, read_only=True)
            except FileNotFoundError:
                return {"success": [False, "Annotation Library not found"]}

            return {
                "success": [True, None],
                "annotation_library": annotation_library.as_json()
            }
def execute_transform(annotation_library_name, key, deduplicate,
                      deduplicate_mode):
    annotation_library = AnnotationLibrary.load(annotation_library_name,
                                                read_only=True)

    # Get the Bounding Boxes
    bounding_boxes = annotation_library.get_bounding_boxes(key)

    # Index the Bounding Boxes
    bounding_box_index = dict()

    for bounding_box in bounding_boxes:
        index = f"{bounding_box['y0']}-{bounding_box['x0']}-{bounding_box['y1']}-{bounding_box['x1']}"
        bounding_box_index[index] = bounding_box

    filtered_bounding_boxes = list()

    # Deduplication
    if deduplicate:
        bounding_box_groups = dict()

        if deduplicate_mode == BoundingBoxDeduplicateMode.ALL:
            bounding_box_groups["ALL"] = bounding_boxes
        elif deduplicate_mode == BoundingBoxDeduplicateMode.USER:
            for bounding_box in bounding_boxes:
                if bounding_box["meta"] not in bounding_box_groups:
                    bounding_box_groups[bounding_box["meta"]] = list()

                bounding_box_groups[bounding_box["meta"]].append(bounding_box)

        for _, boxes in bounding_box_groups.items():
            if len(boxes) <= 5:
                filtered_bounding_boxes += boxes
                continue

            input_boxes = list()

            for box in boxes:
                input_boxes.append(
                    [box["y0"], box["x0"], box["y1"], box["x1"]])

            input_boxes = np.array(input_boxes)

            # Doing it twice cleans up boxes that are too close to boundaries of clusters
            output_boxes = cluster_bounding_boxes(input_boxes)
            output_boxes = cluster_bounding_boxes(output_boxes)

            for output_box in output_boxes:
                index = f"{output_box[0]}-{output_box[1]}-{output_box[2]}-{output_box[3]}"
                filtered_bounding_boxes.append(bounding_box_index[index])

    return key, filtered_bounding_boxes, (len(bounding_boxes) -
                                          len(filtered_bounding_boxes))
    def build(self, name):
        if not len(self.targets):
            return None

        annotation_library = AnnotationLibrary(name)

        for target in self.targets:
            with open(target, "r") as f:
                entries = json.loads(f.read())

            for entry in entries:
                # Exception for craters legacy files...
                if "craters" in entry:
                    entry["bounding_boxes"] = entry["craters"]

                    for bounding_box in entry["bounding_boxes"]:
                        bounding_box["annotation_class"] = "crater"

                entry[
                    "file_location"] = f"{self.base_directory}/{entry['file_location']}"
                annotation_library.add_complete_entry(entry)

        annotation_library.commit()

        return annotation_library
        def get_annotation_library_entry(annotation_library_name, entry_index):
            try:
                annotation_library = AnnotationLibrary.load(
                    annotation_library_name, read_only=True)
            except FileNotFoundError:
                return {"success": [False, "Annotation Library not found"]}

            try:
                annotation_library_entry = annotation_library.as_json_entry(
                    entry_index)
            except Exception:
                return {
                    "success": [
                        False,
                        f"Error while generating the Annotation Library entry for index {entry_index}"
                    ]
                }

            return {"success": [True, None], **annotation_library_entry}
    def build(self, name):
        if not len(self.targets):
            return None

        annotation_library = AnnotationLibrary(name)

        for target in self.targets:
            print(f"Collecting data for user '{target}'...")

            # Build a set of existing image files
            self.existing_images = self._scan_for_existing_images()

            # Fetch the target user
            user = self.models["User"].get(name=target)

            # Fetch the target user's marks within the scoped in application
            marks = self.models["Mark"].select(
                lambda m: m.application.name == self.application and m.user ==
                user)[:]

            # Build a set of the unique encoutered images
            images = [
                self.image_urls[image_id]
                for image_id in set([m.image.id for m in marks])
            ]

            print(
                f"Found {len(marks)} marks in {len(images)} images for user '{target}' in application '{self.application}'...",
                end="\n\n")

            # Synchronize images to disk, for those we don't have yet
            self._sync_images(images)

            # Generate and append the bounding boxes from marks
            self._generate_bounding_boxes(marks, target)

        for file_name, bounding_boxes in self.bounding_boxes.items():
            entry = {
                "file_location": f"data/images/{self.application}/{file_name}",
                "width": self.image_shape[1],
                "height": self.image_shape[0],
                "bounding_boxes": bounding_boxes
            }

            annotation_library.add_complete_entry(entry)

        annotation_library.commit()

        return annotation_library
def execute_transform(annotation_library_file_path, key,
                      image_augmentation_pipeline, augmentation_count):
    annotation_library = AnnotationLibrary.load(annotation_library_file_path,
                                                read_only=True)
    image_augmentation_pipeline = IMAGE_AUGMENTATION_PIPELINES[
        image_augmentation_pipeline]()

    # Get the Image Data
    image_array = annotation_library.get_image_array(key)
    height, width, channels = image_array.shape

    # Get the Bounding Boxes
    bounding_boxes = annotation_library.get_bounding_boxes(key)

    # Prepare the Bounding Boxes for Image Augmentation
    ia_bounding_boxes = list()

    for bounding_box in bounding_boxes:
        y0 = float(bounding_box["y0"])
        x0 = float(bounding_box["x0"])
        y1 = float(bounding_box["y1"])
        x1 = float(bounding_box["x1"])

        is_valid = y0 >= 0 and x0 >= 0 and y1 < height and x1 < width

        if not is_valid:
            continue

        ia_bounding_boxes.append(
            ia.BoundingBox(
                x1=x0,
                y1=y0,
                x2=x1,
                y2=y1,
                label=f"{bounding_box['label']}###{bounding_box['meta']}"))

    ia_bounding_boxes = ia.BoundingBoxesOnImage(ia_bounding_boxes,
                                                shape=image_array.shape)

    # Image Augmentation
    result = list()

    for i in range(augmentation_count):
        new_key = f"{key}_{i}"
        pipeline = image_augmentation_pipeline.to_deterministic()

        augmented_image = pipeline.augment_images([image_array])[0]
        augmented_bounding_boxes = pipeline.augment_bounding_boxes(
            [ia_bounding_boxes])[0]

        # Format data for inter-process communication
        image = Image.fromarray(augmented_image)

        image_bytes = io.BytesIO()
        image.save(image_bytes, format="PNG")

        image_bytes.seek(0)
        image_bytes = image_bytes.read()

        formatted_bounding_boxes = list()

        for bounding_box in augmented_bounding_boxes.bounding_boxes:
            label, meta = bounding_box.label.split("###")

            formatted_bounding_boxes.append([
                bounding_box.y1, bounding_box.x1, bounding_box.y2,
                bounding_box.x2,
                label.encode("utf-8"),
                meta.encode("utf-8")
            ])

        result.append((new_key, image_bytes, (height, width, channels),
                       formatted_bounding_boxes))

    return result