Skip to content

Commit

Permalink
feat(client): adapt OpenAPI uploadMultiLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
zhen.chen committed Mar 7, 2022
1 parent 3a04358 commit 1cecb77
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions tensorbay/client/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

_STRATEGIES = {"abort", "override", "skip"}
_MASK_KEYS = ("semantic_mask", "instance_mask", "panoptic_mask")
_D = Union[Data, AuthData, RemoteData]


class SegmentClientBase:
Expand Down Expand Up @@ -328,6 +329,43 @@ def _upload_label(self, data: Union[AuthData, Data]) -> None:

self._client.open_api_do("PUT", "labels", self._dataset_id, json=post_data)

def _upload_multi_label(self, data: Iterable[Union[_D]]) -> None:
post_data: Dict[str, Any] = {
"segmentName": self.name,
"labels": [],
}
for single_data in data:
label = single_data.label.dumps()
if not label:
continue

remote_path = (
single_data.path
if isinstance(single_data, RemoteData)
else single_data.target_remote_path
)
post_data["label"].append({"remotePath": remote_path, "label": label})
post_data.update(self._status.get_status_info())

self._client.open_api_do("PUT", "multi/data/labels", self._dataset_id, json=post_data)

def upload_label(self, data: Union[_D, Iterable[_D]]) -> None:
"""Upload label with Data object to the draft.
Arguments:
data: The data object which represents the local file to upload.
"""
self._status.check_authority_for_draft()

if not isinstance(data, Iterable):
data = [data]

for chunked_data in chunked(data, 128):
self._upload_multi_label(chunked_data)
for single_data in chunked_data:
self._upload_mask_files(single_data.label)

@property
def name(self) -> str:
"""Return the segment name.
Expand Down Expand Up @@ -458,18 +496,6 @@ def upload_file(self, local_path: str, target_remote_path: str = "") -> None:

self._synchronize_upload_info((data.get_callback_body(),))

def upload_label(self, data: Data) -> None:
"""Upload label with Data object to the draft.
Arguments:
data: The data object which represents the local file to upload.
"""
self._status.check_authority_for_draft()

self._upload_mask_files(data.label)
self._upload_label(data)

def upload_data(self, data: Data) -> None:
"""Upload Data object to the draft.
Expand Down

0 comments on commit 1cecb77

Please sign in to comment.