Skip to content

Commit b40f48c

Browse files
author
Matt Sokoloff
committed
converter demo working
1 parent 55a81a6 commit b40f48c

File tree

11 files changed

+396
-99
lines changed

11 files changed

+396
-99
lines changed

examples/annotation_types/annotation_type_basics.ipynb

Lines changed: 70 additions & 70 deletions
Large diffs are not rendered by default.

examples/annotation_types/converters.ipynb

Lines changed: 285 additions & 14 deletions
Large diffs are not rendered by default.

labelbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name = "labelbox"
22
__version__ = "2.7.0"
33

4+
from labelbox.schema.project import Project
45
from labelbox.client import Client
56
from labelbox.schema.bulk_import_request import BulkImportRequest
6-
from labelbox.schema.project import Project
77
from labelbox.schema.dataset import Dataset
88
from labelbox.schema.data_row import DataRow
99
from labelbox.schema.label import Label

labelbox/data/annotation_types/collection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from tqdm import tqdm
77

8-
from labelbox import OntologyBuilder
8+
from labelbox.schema import ontology
99
from labelbox.orm.model import Entity
1010
from ..generator import PrefetchGenerator
1111
from .label import Label
@@ -25,7 +25,8 @@ def __init__(self, data: Iterable[Label]):
2525
self._index = 0
2626

2727
def assign_schema_ids(
28-
self, ontology_builder: OntologyBuilder) -> "LabelCollection":
28+
self,
29+
ontology_builder: "ontology.OntologyBuilder") -> "LabelCollection":
2930
"""
3031
Adds schema ids to all FeatureSchema objects in the Labels.
3132
This is necessary for MAL.
@@ -166,7 +167,8 @@ def as_collection(self) -> "LabelCollection":
166167
return LabelCollection(data=list(self))
167168

168169
def assign_schema_ids(
169-
self, ontology_builder: OntologyBuilder) -> "LabelGenerator":
170+
self,
171+
ontology_builder: "ontology.OntologyBuilder") -> "LabelGenerator":
170172

171173
def _assign_ids(label: Label):
172174
label.assign_schema_ids(ontology_builder)

labelbox/data/annotation_types/geometry/mask.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from typing import Callable, Tuple, Union
1+
from typing import Callable, Optional, Tuple, Union
22

33
import numpy as np
44
from pydantic.class_validators import validator
55
from rasterio.features import shapes
66
from shapely.geometry import MultiPolygon, shape
7+
import cv2
78

89
from ..data.raster import RasterData
910
from .geometry import Geometry
@@ -25,21 +26,31 @@ def geometry(self):
2526
if val >= 1)
2627
return MultiPolygon(polygons).__geo_interface__
2728

28-
def raster(self, binary=False) -> np.ndarray:
29+
def raster(self,
30+
height: Optional[int] = None,
31+
width: Optional[int] = None,
32+
binary=False) -> np.ndarray:
2933
"""
3034
Removes all pixels from the segmentation mask that do not equal self.color
3135
36+
Args:
37+
height:
38+
3239
Returns:
3340
np.ndarray representing only this object
3441
"""
3542
mask = self.mask.data
3643
if len(mask.shape) == 2:
3744
mask = np.expand_dims(mask, axis=-1)
3845
mask = np.alltrue(mask == self.color, axis=2).astype(np.uint8)
46+
if height is not None or width is not None:
47+
mask = cv2.resize(mask,
48+
(width or mask.shape[1], height or mask.shape[0]))
49+
3950
if binary:
4051
return mask
4152
elif isinstance(self.color, int):
42-
return mask * self.color
53+
mask = mask * self.color
4354
else:
4455
color_image = np.zeros((mask.shape[0], mask.shape[1], 3),
4556
dtype=np.uint8)

labelbox/data/annotation_types/label.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pydantic import BaseModel
44

5-
from labelbox import Classification as OClassification, OntologyBuilder, Option
5+
from labelbox.schema import ontology
66
from labelbox.orm.model import Entity
77
from .classification import ClassificationAnswer
88
from .data import VideoData, TextData, RasterData
@@ -77,7 +77,8 @@ def create_data_row(self, dataset: "Entity.Dataset",
7777
self.data.external_id = data_row.external_id
7878
return self
7979

80-
def assign_schema_ids(self, ontology_builder: OntologyBuilder) -> "Label":
80+
def assign_schema_ids(
81+
self, ontology_builder: ontology.OntologyBuilder) -> "Label":
8182
"""
8283
Adds schema ids to all FeatureSchema objects in the Labels.
8384
This is necessary for MAL.
@@ -104,18 +105,18 @@ def assign_schema_ids(self, ontology_builder: OntologyBuilder) -> "Label":
104105
return self
105106

106107
def _get_feature_schema_lookup(
107-
self, ontology_builder: OntologyBuilder
108+
self, ontology_builder: ontology.OntologyBuilder
108109
) -> Tuple[Dict[str, str], Dict[str, str]]:
109110
tool_lookup = {}
110111
classification_lookup = {}
111112

112113
def flatten_classification(classifications):
113114
for classification in classifications:
114-
if isinstance(classification, OClassification):
115+
if isinstance(classification, ontology.OClassification):
115116
classification_lookup[
116117
classification.
117118
instructions] = classification.feature_schema_id
118-
elif isinstance(classification, Option):
119+
elif isinstance(classification, ontology.Option):
119120
classification_lookup[
120121
classification.value] = classification.feature_schema_id
121122
else:

labelbox/data/generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def __init__(self,
5353
thread.daemon = True
5454
thread.start()
5555

56+
# Can only iterate over once it the queue.get hangs forever.
57+
self.done = False
58+
5659
def _process(self, value) -> Any:
5760
raise NotImplementedError("Abstract method needs to be implemented")
5861

@@ -73,12 +76,15 @@ def __iter__(self):
7376
return self
7477

7578
def __next__(self) -> Any:
79+
if self.done:
80+
raise StopIteration
7681
value = self.queue.get()
7782
while value is None:
7883
self.completed_threads += 1
7984
if self.completed_threads == self.max_concurrency:
8085
for thread in self.threads:
8186
thread.join()
87+
self.done = True
8288
raise StopIteration
8389
value = self.queue.get()
8490
return value

labelbox/data/serialization/labelbox_v1/converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, examples, client):
7979
def _process(self, value):
8080
if 'frames' in value['Label']:
8181
req = self._request(value)
82-
value['Label'] = ndjson.loads(req.text)
82+
value['Label'] = ndjson.loads(req)
8383
return value
8484

8585
@retry.Retry(predicate=retry.if_exception_type(HTTPError))

labelbox/data/serialization/labelbox_v1/label.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def to_common(self) -> Label:
148148
else:
149149
annotations = self.label.to_common()
150150
data = self._infer_media_type()
151+
print(data)
151152

152153
return Label(data=data,
153154
annotations=annotations,

labelbox/schema/ontology.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from typing import Any, Dict, List, Optional, Union
66

7-
from labelbox.schema.project import Project
7+
#from labelbox.schema.project import Project
88
from labelbox.orm.db_object import DbObject
99
from labelbox.orm.model import Field, Relationship
1010
from labelbox.exceptions import InconsistentOntologyException
1111

12+
Project = "NONE"
13+
1214

1315
@dataclass
1416
class Option:

0 commit comments

Comments
 (0)