Skip to content

Commit

Permalink
[IO-1661][external] Error messages were not clear to customers (#674)
Browse files Browse the repository at this point in the history
* Error messages improved.

* Update to error message

* Correct typo

---------

Co-authored-by: Owen <[email protected]>
  • Loading branch information
owencjones and Owen authored Oct 3, 2023
1 parent a0e4b6a commit a4ad270
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions darwin/importer/formats/coco.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from logging import getLogger
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional
from typing import Dict, Iterator, List, Optional

import deprecation
import orjson as json
Expand All @@ -19,6 +20,9 @@
"""


logger = getLogger(__name__)


def parse_path(path: Path) -> Optional[List[dt.AnnotationFile]]:
"""
Parses the given ``coco`` file and returns a ``List[dt.AnnotationFile]`` with the parsed
Expand All @@ -41,7 +45,7 @@ def parse_path(path: Path) -> Optional[List[dt.AnnotationFile]]:
return list(parse_json(path, data))


def parse_json(path: Path, data: Dict[str, Any]) -> Iterator[dt.AnnotationFile]:
def parse_json(path: Path, data: Dict[str, dt.UnknownType]) -> Iterator[dt.AnnotationFile]:
"""
Parses the given ``json`` structure into an ``Iterator[dt.AnnotationFile]``.
Expand All @@ -62,7 +66,7 @@ def parse_json(path: Path, data: Dict[str, Any]) -> Iterator[dt.AnnotationFile]:
category_lookup_table = {category["id"]: category for category in data["categories"]}
tag_categories = data.get("tag_categories") or []
tag_category_lookup_table = {category["id"]: category for category in tag_categories}
image_annotations: Dict[str, Any] = {}
image_annotations: Dict[str, dt.UnknownType] = {}

for image in data["images"]:
image_id = image["id"]
Expand Down Expand Up @@ -91,15 +95,17 @@ def parse_json(path: Path, data: Dict[str, Any]) -> Iterator[dt.AnnotationFile]:
yield dt.AnnotationFile(path, filename, annotation_classes, annotations, remote_path=remote_path)


def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str, Any]) -> Optional[dt.Annotation]:
def parse_annotation(
annotation: Dict[str, dt.UnknownType], category_lookup_table: Dict[str, dt.UnknownType]
) -> Optional[dt.Annotation]:
"""
Parses the given ``json`` dictionary into a darwin ``Annotation`` if possible.
Parameters
----------
annotation : Dict[str, Any]
annotation : Dict[str, dt.UnknownType]
The ``json`` dictionary to parse.
category_lookup_table : Dict[str, Any]
category_lookup_table : Dict[str, dt.UnknownType]
Dictionary with all the categories from the ``coco`` file.
Returns
Expand All @@ -112,7 +118,10 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str
iscrowd = annotation.get("iscrowd") == 1

if iscrowd:
print("Warning, unsupported RLE, skipping")
logger.warn(
f"Skipping annotation {annotation.get('id')} because it is a crowd "
"annotation, and Darwin does not support import of crowd annotations."
)
return None

if len(segmentation) == 0 and len(annotation["bbox"]) == 4:
Expand All @@ -122,7 +131,7 @@ def parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str
x, y, w, h = map(int, annotation["bbox"][0])
return dt.make_bounding_box(category["name"], x, y, w, h)
elif isinstance(segmentation, dict):
print("warning, converting complex coco rle mask to polygon, could take some time")
logger.warn("warning, converting complex coco rle mask to polygon, could take some time")
if isinstance(segmentation["counts"], list):
mask = rle_decode(segmentation["counts"], segmentation["size"][::-1])
else:
Expand Down

0 comments on commit a4ad270

Please sign in to comment.