Skip to content

Commit b966620

Browse files
authored
Merge pull request #205 from Labelbox/ms/annotation-examples
annotation examples
2 parents d04fe7b + 261c5f9 commit b966620

File tree

11 files changed

+3010
-18
lines changed

11 files changed

+3010
-18
lines changed

examples/annotation_types/annotation_type_basics.ipynb

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

examples/annotation_types/converters.ipynb

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

examples/annotation_types/label_containers.ipynb

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

examples/annotation_types/mal_with_annotation_types.ipynb

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

labelbox/data/annotation_types/data/raster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RasterData(BaseData):
2222

2323
@classmethod
2424
def from_2D_arr(cls, arr: TypedArray[Literal['uint8']], **kwargs):
25-
if len(arr.shape):
25+
if len(arr.shape) != 2:
2626
raise ValueError(
2727
f"Found array with shape {arr.shape}. Expected two dimensions ([W,H])"
2828
)

labelbox/data/serialization/labelbox_v1/label.py

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

153152
return Label(data=data,
154153
annotations=annotations,

labelbox/schema/bulk_import_request.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import ndjson
1111
import requests
1212
from pydantic import BaseModel, validator
13-
from requests.api import request
1413
from typing_extensions import Literal
1514
from typing import (Any, List, Optional, BinaryIO, Dict, Iterable, Tuple, Union,
1615
Type, Set)

labelbox/schema/ontology.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
from labelbox.schema.project import Project
7+
from labelbox.schema import project
88
from labelbox.orm.db_object import DbObject
99
from labelbox.orm.model import Field, Relationship
1010
from labelbox.exceptions import InconsistentOntologyException
@@ -330,7 +330,7 @@ def _update_colors(self):
330330
self.tools[index].color = '#%02x%02x%02x' % rgb_color
331331

332332
@classmethod
333-
def from_project(cls, project: Project):
333+
def from_project(cls, project: "project.Project"):
334334
ontology = project.ontology().normalized
335335
return cls.from_dict(ontology)
336336

labelbox/schema/project.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from labelbox.orm.db_object import DbObject, Updateable, Deletable
1818
from labelbox.orm.model import Entity, Field, Relationship
1919
from labelbox.pagination import PaginatedCollection
20+
from labelbox.data.serialization import LBV1Converter
2021

2122
try:
2223
datetime.fromisoformat # type: ignore[attr-defined]
@@ -166,7 +167,33 @@ def export_queued_data_rows(self, timeout_seconds=120):
166167
self.uid)
167168
time.sleep(sleep_time)
168169

169-
def export_labels(self, timeout_seconds=60):
170+
def video_label_generator(self, timeout_seconds=60):
171+
"""
172+
Download video annotations
173+
174+
Returns:
175+
LabelGenerator for accessing labels for each video
176+
"""
177+
json_data = self.export_labels(download=True,
178+
timeout_seconds=timeout_seconds)
179+
if 'frames' not in json_data[0]['Label']:
180+
raise ValueError(
181+
"frames key not found in the first label. Cannot export video data."
182+
)
183+
return LBV1Converter.deserialize_video(json_data, self.client)
184+
185+
def label_generator(self, timeout_seconds=60):
186+
"""
187+
Download text and image annotations
188+
189+
Returns:
190+
LabelGenerator for accessing labels for each text or image
191+
"""
192+
json_data = self.export_labels(download=True,
193+
timeout_seconds=timeout_seconds)
194+
return LBV1Converter.deserialize(json_data)
195+
196+
def export_labels(self, download=False, timeout_seconds=60):
170197
""" Calls the server-side Label exporting that generates a JSON
171198
payload, and returns the URL to that payload.
172199
@@ -188,7 +215,13 @@ def export_labels(self, timeout_seconds=60):
188215
res = self.client.execute(query_str, {id_param: self.uid})
189216
res = res["exportLabels"]
190217
if not res["shouldPoll"]:
191-
return res["downloadUrl"]
218+
url = res['downloadUrl']
219+
if not download:
220+
return url
221+
else:
222+
response = requests.get(url)
223+
response.raise_for_status()
224+
return response.json()
192225

193226
timeout_seconds -= sleep_time
194227
if timeout_seconds <= 0:

labelbox/schema/user.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from labelbox.schema.project import Project
21
from labelbox.orm.db_object import DbObject
32
from labelbox.orm.model import Field, Relationship
43
from labelbox.schema.role import Role
4+
from labelbox.schema.project import Project
55

66

77
class User(DbObject):
@@ -41,7 +41,7 @@ class User(DbObject):
4141
org_role = Relationship.ToOne("OrgRole", False)
4242

4343
def update_org_role(self, role: Role):
44-
""" Updated the `User`s organization role.
44+
""" Updated the `User`s organization role.
4545
4646
See client.get_roles() to get all valid roles
4747
If you a user is converted from project level permissions to org level permissions and then convert back, their permissions will remain for each individual project
@@ -52,7 +52,7 @@ def update_org_role(self, role: Role):
5252
"""
5353
user_id_param = "userId"
5454
role_id_param = "roleId"
55-
query_str = """mutation SetOrganizationRolePyApi($%s: ID!, $%s: ID!) {
55+
query_str = """mutation SetOrganizationRolePyApi($%s: ID!, $%s: ID!) {
5656
setOrganizationRole(data: {userId: $userId, roleId: $roleId}) { id name }}
5757
""" % (user_id_param, role_id_param)
5858

@@ -61,7 +61,7 @@ def update_org_role(self, role: Role):
6161
role_id_param: role.uid
6262
})
6363

64-
def remove_from_project(self, project: Project):
64+
def remove_from_project(self, project: "Project"):
6565
""" Removes a User from a project. Only used for project based users.
6666
Project based user means their org role is "NONE"
6767
@@ -71,13 +71,13 @@ def remove_from_project(self, project: Project):
7171
"""
7272
self.upsert_project_role(project, self.client.get_roles()['NONE'])
7373

74-
def upsert_project_role(self, project: Project, role: Role):
74+
def upsert_project_role(self, project: "Project", role: Role):
7575
""" Updates or replaces a User's role in a project.
7676
7777
Args:
7878
project (Project): The project to update the users permissions for
7979
role (Role): The role to assign to this user in this project.
80-
80+
8181
"""
8282
org_role = self.org_role()
8383
if org_role.name.upper() != 'NONE':

0 commit comments

Comments
 (0)