Skip to content

Commit

Permalink
Merge pull request #11 from aoxolotl/bboxAPI
Browse files Browse the repository at this point in the history
Add API for BoundingBox annotation
  • Loading branch information
ninthreezy authored Aug 21, 2023
2 parents da75c2f + 209ff62 commit 3cdd8e7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
64 changes: 64 additions & 0 deletions datatorch/api/entity/sources/image/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from ..source import Source

from typing import Optional
from .typings import Point2D
from datatorch.api import ApiClient
from ....entity.annotation import Annotation


__all__ = "BoundingBox"
Expand Down Expand Up @@ -32,3 +35,64 @@ def bottom_right(self) -> Point2D:
@property
def size(self) -> float:
return self.width * self.height

def from_points(self, top_left: Point2D, bottom_right: Point2D):
self.x = top_left[0]
self.y = top_left[1]
self.width = bottom_right[0] - top_left[0]
self.height = bottom_right[1] - top_left[1]

def create_new_bbox(self, label_id: str, file_id: str):
print("Creating new annotation")
new_annotation = Annotation()
new_annotation.label_id = label_id
new_annotation.file_id = file_id
new_annotation.create(ApiClient())

self.annotation_id = new_annotation.id
self.create(ApiClient())
print("BoundingBox created with annotation", new_annotation.id, flush=True)

def combine_bbox(self, annotation):
if self.annotation_id is None:
raise ValueError("No annotation id set")

self.annotation_id = annotation.id
existing_bbox = next(
x for x in annotation.get("sources") if x.get("type") == "PaperBox"
)

# Merge self and existing bbox
top_left = (
min(self.x, existing_bbox["x"]),
min(self.y, existing_bbox["y"]),
)
bottom_right = (
max(self.bottom_right[0], existing_bbox["x"] + existing_bbox["width"]),
max(self.bottom_right[1], existing_bbox["y"] + existing_bbox["height"]),
)

self.from_points(top_left, bottom_right)

self.save(ApiClient())
print(
f"Updated bounding box for annotation {annotation.id}",
flush=True,
)

def create_bbox_from_points(
self,
top_left: Point2D,
bottom_right: Point2D,
annotation=None,
label_id: Optional[str] = None,
file_id: Optional[str] = None,
):
if annotation is None and (label_id is None or file_id is None):
raise ValueError("Either annotation or label_id and file_id must be set")

self.from_points(top_left, bottom_right)
if annotation:
self.combine_bbox(annotation)
else:
self.create_new_bbox(label_id, file_id)
24 changes: 21 additions & 3 deletions datatorch/api/entity/sources/image/segmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ def combine_segmentations(self, annotation):
for polygon in multi:
self.path_data.append(list(polygon.exterior.coords[:-1]))

self.save(ApiClient())
print(
f"Updated segmentation for annotation {annotation.id}",
flush=True,
)

def create_new_annotation(self, label_id: str, file_id: str):
def create_new_segmentation(self, label_id: str, file_id: str):
print("Creating new annotation")
new_annotation = Annotation()
new_annotation.label_id = label_id
Expand All @@ -79,7 +80,24 @@ def create_new_annotation(self, label_id: str, file_id: str):

self.annotation_id = annotation_id
self.create(ApiClient())
print("Segmentation created")
print("Segmentation created with annotation", annotation_id, flush=True)

def create_segmentation_from_mask(
self,
mask: np.array,
simplify: int = 0,
annotation=None,
label_id: Optional[str] = None,
file_id: Optional[str] = None,
):
if annotation is None and (label_id is None or file_id is None):
raise ValueError("Either annotation or label_id and file_id must be set")

self.from_mask(mask, simplify=simplify)
if annotation:
self.combine_segmentations(annotation)
else:
self.create_new_segmentation(label_id, file_id)

def to_mask(self) -> np.array:
pass
raise NotImplementedError("to_mask not implemented")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

setup(
name="datatorch",
version="0.4.8.0",
version="0.4.8.1",
description="A CLI and library for interacting with DataTorch.",
author="DataTorch",
author_email="[email protected]",
Expand Down

0 comments on commit 3cdd8e7

Please sign in to comment.