From 2883a04ed81e57da342ac164c5ec98b5709f4cb1 Mon Sep 17 00:00:00 2001 From: vorozhkog Date: Fri, 29 Nov 2024 04:48:10 +0000 Subject: [PATCH] add async --- local.env | 11 +++++++---- src/main.py | 29 +++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/local.env b/local.env index d244913..cac8a5a 100644 --- a/local.env +++ b/local.env @@ -1,10 +1,13 @@ -TASK_ID=43318 # ⬅️ use for production launch -TEAM_ID=506 # ⬅️ change it -WORKSPACE_ID=942 # ⬅️ change it -PROJECT_ID=27502 # ⬅️ change it +#TASK_ID=43318 # ⬅️ use for production launch +#TEAM_ID=506 # ⬅️ change it +#WORKSPACE_ID=942 # ⬅️ change it +#PROJECT_ID=27502 # ⬅️ change it # DATASET_ID=75110 # ⬅️ specify when exporting from dataset SLY_APP_DATA_DIR="results/" # ⬅️ path to directory for local debugging +TEAM_ID = 431 +WORKSPACE_ID = 1019 +PROJECT_ID = 40721 # options: "images" "annotations" modal.state.selectedOutput="images" diff --git a/src/main.py b/src/main.py index 6e0a241..d2b62a3 100644 --- a/src/main.py +++ b/src/main.py @@ -10,6 +10,9 @@ import functions as f import workflow as w +import asyncio +from tinytimer import Timer + if sly.is_development(): load_dotenv("local.env") load_dotenv(os.path.expanduser("~/supervisely.env")) @@ -56,15 +59,29 @@ def process(self, context: sly.app.Export.Context): coco_ann = {} + if selected_output == "images": + image_ids = [image_info.id for image_info in images] + paths = [os.path.join(img_dir, image_info.name) for image_info in images] + if api.server_address.startswith("https://"): + semaphore = asyncio.Semaphore(100) + else: + semaphore = None + + with Timer() as t: + coro = api.image.download_paths_async(image_ids, paths, semaphore) + loop = sly.utils.get_or_create_event_loop() + if loop.is_running(): + future = asyncio.run_coroutine_threadsafe(coro, loop) + future.result() + else: + loop.run_until_complete(coro) + sly.logger.info( + f"Downloading time: {t.elapsed:.4f} seconds per {len(image_ids)} images ({t.elapsed/len(image_ids):.4f} seconds per image)" + ) + pbar = sly.Progress(f"Converting dataset: {dataset.name}", total_cnt=len(images)) for batch in sly.batched(images): - image_ids = [image.id for image in batch] ann_infos = api.annotation.download_batch(dataset.id, image_ids) - img_paths = [os.path.join(img_dir, img_info.name) for img_info in batch] - - if selected_output == "images": - api.image.download_paths(dataset.id, image_ids, img_paths) - anns = [] for ann_info, img_info in zip(ann_infos, batch): ann = f.check_sly_annotations(ann_info, img_info, project_meta)