Skip to content

Commit

Permalink
feat: Option to skip an error when downloading images from ls (#361)
Browse files Browse the repository at this point in the history
* add error raise option to skip because sometimes there is an issue on the image URL

* option to skip errors in order to avoid download interruption

* rename to error_raise

* rename to error_raise
  • Loading branch information
baslia authored Nov 18, 2024
1 parent b310445 commit 0a127ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ml_utils/ml_utils_cli/cli/apps/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def export(
"provided (typically, if the source is Label Studio)"
),
] = 0.8,
error_raise: Annotated[
bool,
typer.Option(
help="Raise an error if an image download fails, only for Ultralytics"
),
] = True,
):
"""Export Label Studio annotation, either to Hugging Face Datasets or
local files (ultralytics format)."""
Expand Down Expand Up @@ -204,6 +210,7 @@ def export(
label_names_list,
typing.cast(int, project_id),
train_ratio=train_ratio,
error_raise=error_raise,
)

elif from_ == ExportSource.hf:
Expand All @@ -212,6 +219,7 @@ def export(
typing.cast(str, repo_id),
typing.cast(Path, output_dir),
download_images=download_images,
error_raise=error_raise,
)
else:
raise typer.BadParameter("Unsupported export format")
9 changes: 6 additions & 3 deletions ml_utils/ml_utils_cli/cli/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def export_from_ls_to_ultralytics(
category_names: list[str],
project_id: int,
train_ratio: float = 0.8,
error_raise: bool = True,
):
"""Export annotations from a Label Studio project to the Ultralytics
format.
Expand Down Expand Up @@ -161,7 +162,7 @@ def export_from_ls_to_ultralytics(
has_valid_annotation = True

if has_valid_annotation:
download_output = download_image(image_url, return_bytes=True)
download_output = download_image(image_url, return_bytes=True, error_raise=error_raise)
if download_output is None:
logger.error("Failed to download image: %s", image_url)
continue
Expand All @@ -182,7 +183,9 @@ def export_from_ls_to_ultralytics(


def export_from_hf_to_ultralytics(
repo_id: str, output_dir: Path, download_images: bool = True
repo_id: str, output_dir: Path,
download_images: bool = True,
error_raise: bool = True,
):
"""Export annotations from a Hugging Face dataset project to the
Ultralytics format.
Expand All @@ -207,7 +210,7 @@ def export_from_hf_to_ultralytics(
image_url = sample["meta"]["image_url"]

if download_images:
download_output = download_image(image_url, return_bytes=True)
download_output = download_image(image_url, return_bytes=True, error_raise=error_raise)
if download_output is None:
logger.error("Failed to download image: %s", image_url)
continue
Expand Down

0 comments on commit 0a127ad

Please sign in to comment.