Skip to content

Commit

Permalink
add async
Browse files Browse the repository at this point in the history
  • Loading branch information
vorozhkog committed Nov 29, 2024
1 parent 436e237 commit 2883a04
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
11 changes: 7 additions & 4 deletions local.env
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
29 changes: 23 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2883a04

Please sign in to comment.