Skip to content

Commit

Permalink
change image download method to async
Browse files Browse the repository at this point in the history
  • Loading branch information
vorozhkog committed Nov 28, 2024
1 parent a3cd12d commit eb82571
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os
from distutils.util import strtobool
import asyncio

import supervisely as sly
from dotenv import load_dotenv
Expand Down Expand Up @@ -80,15 +81,31 @@ def export_to_coco(api: sly.Api) -> None:
total_cnt=len(images),
min_report_percent=5,
)
for batch in sly.batched(images):
image_ids = [image_info.id for image_info in batch]
sly.logger.info(f"Working with batch of {len(batch)} images with ids: {image_ids}")

if selected_output == "images":
image_paths = [os.path.join(img_dir, image_info.name) for image_info in batch]
f.download_batch_with_retry(api, dataset.id, image_ids, image_paths)
dataset_path = os.path.join(img_dir, project.name, dataset.name)
os.makedirs(dataset_path, exist_ok=True)

if selected_output == "images":
image_ids = [image_info.id for image_info in images]
paths = [os.path.join(dataset_path, image_info.name) for image_info in images]
if api.server_address.startswith("https://"):
semaphore = asyncio.Semaphore(100)
else:
semaphore = None
# api._get_default_semaphore()
coro = api.image.download_paths_async(
image_ids, paths, semaphore, progress_cb=ds_progress.iters_done_report
)
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)

ann_infos = api.annotation.download_batch(dataset.id, image_ids)
for batch in sly.batched(images):
batch_ids = [image_info.id for image_info in batch]
ann_infos = api.annotation.download_batch(dataset.id, batch_ids)
anns = []
sly.logger.info(f"Preparing to convert {len(ann_infos)} annotations...")
for ann_info, img_info in zip(ann_infos, batch):
Expand All @@ -109,6 +126,36 @@ def export_to_coco(api: sly.Api) -> None:
include_captions,
RECTANGLE_MARK,
)

# for batch in sly.batched(images):
# image_ids = [image_info.id for image_info in batch]
# sly.logger.info(f"Working with batch of {len(batch)} images with ids: {image_ids}")

# if selected_output == "images":
# image_paths = [os.path.join(img_dir, image_info.name) for image_info in batch]
# f.download_batch_with_retry(api, dataset.id, image_ids, image_paths)

# ann_infos = api.annotation.download_batch(dataset.id, image_ids)
# anns = []
# sly.logger.info(f"Preparing to convert {len(ann_infos)} annotations...")
# for ann_info, img_info in zip(ann_infos, batch):
# ann = convert_geometry.convert_annotation(
# ann_info, img_info, project_meta, meta, RECTANGLE_MARK
# )
# anns.append(ann)
# sly.logger.info(f"{len(anns)} annotations converted...")
# coco_instances, label_id, coco_captions, caption_id = f.create_coco_annotation(
# categories_mapping,
# batch,
# anns,
# label_id,
# coco_instances,
# caption_id,
# coco_captions,
# ds_progress,
# include_captions,
# RECTANGLE_MARK,
# )
with open(os.path.join(ann_dir, "instances.json"), "w") as file:
json.dump(coco_instances, file)
if coco_captions is not None and include_captions:
Expand Down

0 comments on commit eb82571

Please sign in to comment.