Skip to content

Commit c35e4cc

Browse files
authored
Merge pull request #495 from superannotateai/get_annotations
get annotations uses assets provider
2 parents 08aeade + 65573c1 commit c35e4cc

File tree

5 files changed

+348
-238
lines changed

5 files changed

+348
-238
lines changed

src/superannotate/lib/core/serviceproviders.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -421,30 +421,32 @@ def list_annotation_classes(
421421

422422
@abstractmethod
423423
async def download_big_annotation(
424-
self,
425-
project_id: int,
426-
team_id: int,
427-
download_path: str,
428-
postfix: str,
429-
item: dict,
430-
callback: Callable = None,
424+
self,
425+
project_id: int,
426+
team_id: int,
427+
download_path: str,
428+
postfix: str,
429+
item: dict,
430+
callback: Callable = None,
431431
):
432432
raise NotImplementedError
433433

434434
@abstractmethod
435435
async def download_small_annotations(
436-
self,
437-
project_id: int,
438-
team_id: int,
439-
folder_id: int,
440-
reporter: Reporter,
441-
download_path: str,
442-
postfix: str,
443-
items: List[str] = None,
444-
callback: Callable = None,
436+
self,
437+
project_id: int,
438+
team_id: int,
439+
folder_id: int,
440+
reporter: Reporter,
441+
download_path: str,
442+
postfix: str,
443+
items: List[str] = None,
444+
callback: Callable = None,
445445
):
446446
raise NotImplementedError
447447

448448
@abstractmethod
449-
def sort_items_by_size(self, item_names: List[str], team_id: int, project_id: int, folder_id: int):
450-
raise NotImplementedError
449+
def sort_items_by_size(
450+
self, item_names: List[str], team_id: int, project_id: int, folder_id: int
451+
):
452+
raise NotImplementedError

src/superannotate/lib/core/usecases/annotations.py

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -775,36 +775,95 @@ def validate_item_names(self):
775775
self._item_names = [item.name for item in self._images.get_all(condition)]
776776

777777
def _prettify_annotations(self, annotations: List[dict]):
778+
restruct = {}
779+
778780
if self._item_names_provided:
781+
for annotation in annotations:
782+
restruct[annotation["metadata"]["name"]] = annotation
779783
try:
780-
data = []
781-
for annotation in annotations:
782-
data.append(
783-
(
784-
self._item_names.index(annotation["metadata"]["name"]),
785-
annotation,
786-
)
787-
)
788-
return [i[1] for i in sorted(data, key=lambda x: x[0])]
784+
return [restruct[x] for x in self._item_names if x in restruct]
789785
except KeyError:
790786
raise AppException("Broken data.")
787+
788+
return annotations
789+
790+
async def get_big_annotation(
791+
self,
792+
):
793+
794+
large_annotations = []
795+
while True:
796+
item = await self._big_annotations_queue.get()
797+
if not item:
798+
await self._big_annotations_queue.put(None)
799+
break
800+
801+
large_annotation = await self._client.get_big_annotation(
802+
team_id=self._project.team_id,
803+
project_id=self._project.id,
804+
folder_id=self._folder.uuid,
805+
item=item,
806+
reporter=self.reporter,
807+
)
808+
809+
large_annotations.append(large_annotation)
810+
811+
return large_annotations
812+
813+
async def get_small_annotations(self, item_names):
814+
small_annotations = await self._client.get_small_annotations(
815+
team_id=self._project.team_id,
816+
project_id=self._project.id,
817+
folder_id=self._folder.uuid,
818+
items=item_names,
819+
reporter=self.reporter,
820+
)
821+
822+
return small_annotations
823+
824+
async def distribute_to_queue(self, big_annotations):
825+
826+
for item in big_annotations:
827+
await self._big_annotations_queue.put(item)
828+
829+
await self._big_annotations_queue.put(None)
830+
831+
async def run_workers(self, big_annotations, small_annotations):
832+
833+
annotations = await asyncio.gather(
834+
self.distribute_to_queue(big_annotations),
835+
self.get_small_annotations(small_annotations),
836+
self.get_big_annotation(),
837+
self.get_big_annotation(),
838+
self.get_big_annotation(),
839+
return_exceptions=True,
840+
)
841+
842+
annotations = [i for x in annotations[1:] for i in x if x]
791843
return annotations
792844

793845
def execute(self):
794846
if self.is_valid():
847+
self._big_annotations_queue = asyncio.Queue()
795848
items_count = len(self._item_names)
796849
self.reporter.log_info(
797850
f"Getting {items_count} annotations from "
798851
f"{self._project.name}{f'/{self._folder.name}' if self._folder.name != 'root' else ''}."
799852
)
800853
self.reporter.start_progress(items_count, disable=not self._show_process)
801-
annotations = self._client.get_annotations(
854+
855+
items = self._client.sort_items_by_size(
856+
item_names=self._item_names,
802857
team_id=self._project.team_id,
803-
project_id=self._project.id,
804858
folder_id=self._folder.uuid,
805-
items=self._item_names,
806-
reporter=self.reporter,
859+
project_id=self._project.id,
860+
)
861+
862+
small_annotations = [x["name"] for x in items["small"]]
863+
annotations = asyncio.run(
864+
self.run_workers(items["large"], small_annotations)
807865
)
866+
808867
received_items_count = len(annotations)
809868
self.reporter.finish_progress()
810869
if items_count > received_items_count:
@@ -1202,7 +1261,6 @@ def execute(self):
12021261
if not folder.is_root and self._folder.is_root:
12031262
new_export_path += f"/{folder.name}"
12041263

1205-
12061264
# TODO check
12071265
if not item_names:
12081266
continue

0 commit comments

Comments
 (0)