Skip to content

Commit 04fa2cb

Browse files
committed
define classes for segmentation annotation type
1 parent 7601c29 commit 04fa2cb

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

nucleus/annotation.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,51 @@
1616
VERTICES_KEY,
1717
)
1818

19+
class Segment:
20+
def __init__(self, label: str, index: int, metadata: Optional[dict] = None):
21+
self.label = label
22+
self.index = index
23+
self.metadata = metadata
24+
25+
def __str__(self):
26+
return self.to_payload()
27+
28+
@classmethod
29+
def from_json(cls, payload: dict):
30+
return cls(payload.get(LABEL_KEY), payload.get(INDEX_KEY), payload.get(METADATA_KEY, None))
31+
32+
def to_payload(self) -> dict:
33+
payload = {
34+
LABEL_KEY: self.label,
35+
INDEX_KEY: self.index,
36+
}
37+
if self.metadata is not None:
38+
payload[METADATA_KEY] = self.metadata
39+
return payload
40+
41+
class SegmentationAnnotation:
42+
def __init__(self, mask_url: str, annotations: List[Segment], reference_id: Optional[str] = None, item_id: Optional[str] = None):
43+
if bool(reference_id) == bool(item_id):
44+
raise Exception(
45+
"You must specify either a reference_id or an item_id for an annotation."
46+
)
47+
self.mask_url = mask_url
48+
self.annotations = annotations
49+
self.reference_id = reference_id
50+
self.item_id = item_id
51+
52+
53+
def __str__(self):
54+
55+
@classmethod
56+
def from_json(cls, payload: dict):
57+
58+
def to_payload(self) -> dict:
59+
payload = {
60+
MASK_URL_KEY: self.mask_url,
61+
ANNOTATIONS_KEY: [ann.to_payload for ann in self.annotations],
62+
}
63+
1964

2065
class AnnotationTypes(Enum):
2166
BOX = BOX_TYPE

nucleus/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@
4343
POLYGON_TYPE = "polygon"
4444
GEOMETRY_KEY = "geometry"
4545
AUTOTAGS_KEY = "autotags"
46+
MASK_URL_KEY = "mask_url"
47+
INDEX_KEY = "index_key"

0 commit comments

Comments
 (0)