Skip to content

Commit 586f846

Browse files
committed
Fix non existing preannotation
1 parent 89cdbfa commit 586f846

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

superannotate/db/images.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,14 @@ def _get_image_pre_or_annotations(project, image_name, pre, project_type=None):
706706
annotation_classes
707707
)
708708
loc = "MAIN" if pre == "" else "PREANNOTATION"
709+
if loc not in res["annotations"]:
710+
logger.warning("%sannotation doesn't exist for %s.", pre, image_name)
711+
return {
712+
f"{pre}annotation_json": None,
713+
f"{pre}annotation_json_filename": None,
714+
f"{pre}annotation_mask": None,
715+
f"{pre}annotation_mask_filename": None
716+
}
709717
main_annotations = res["annotations"][loc][0]
710718
response = requests.get(
711719
url=main_annotations["annotation_json_path"]["url"],
@@ -779,7 +787,9 @@ def _download_image_pre_or_annotations(
779787

780788
if annotation[f"{pre}annotation_json_filename"] is None:
781789
image = get_image_metadata(project, image_name)
782-
logger.warning("No annotation found for image %s.", image["name"])
790+
logger.warning(
791+
"No %sannotation found for image %s.", pre, image["name"]
792+
)
783793
return None
784794
return_filepaths = []
785795
json_path = Path(local_dir_path

superannotate/db/projects.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ def __upload_annotations_thread(
11991199
"team_id": team_id,
12001200
"ids": [metadata["id"] for metadata in metadatas]
12011201
}
1202-
endpoint = '/images/getAnnotationsPathsAndTokens' if not pre else '/images/getPreAnnotationsPathsAndTokens'
1202+
endpoint = '/images/getAnnotationsPathsAndTokens' if pre == "" else '/images/getPreAnnotationsPathsAndTokens'
12031203
response = _api.send_request(
12041204
req_type='POST', path=endpoint, json_req=data
12051205
)
@@ -1288,48 +1288,27 @@ def upload_annotations_from_folder_to_project(
12881288
:rtype: tuple of list of strs
12891289
"""
12901290
return _upload_pre_or_annotations_from_folder_to_project(
1291-
project, folder_path, False, from_s3_bucket, recursive_subfolders
1291+
project, folder_path, "", from_s3_bucket, recursive_subfolders
12921292
)
12931293

12941294

12951295
def _upload_pre_or_annotations_from_folder_to_project(
12961296
project,
12971297
folder_path,
1298-
pre=False,
1298+
pre,
12991299
from_s3_bucket=None,
13001300
recursive_subfolders=False
13011301
):
1302-
"""Finds and uploads all JSON files in the folder_path as annotations to the project.
1303-
1304-
The JSON files should follow specific naming convention. For Vector
1305-
projects they should be named "<image_filename>___objects.json" (e.g., if
1306-
image is cats.jpg the annotation filename should be cats.jpg___objects.json), for Pixel projects
1307-
JSON file should be named "<image_filename>___pixel.json" and also second mask
1308-
image file should be present with the name "<image_name>___save.png". In both cases
1309-
image with <image_name> should be already present on the platform.
1310-
1311-
Existing annotations will be overwritten.
1312-
1313-
:param project: project name or metadata of the project to upload annotations to
1314-
:type project: str or dict
1315-
:param from_s3_bucket: AWS S3 bucket to use. If None then folder_path is in local filesystem
1316-
:type from_s3_bucket: str
1317-
:param recursive_subfolders: enable recursive subfolder parsing
1318-
:type recursive_subfolders: bool
1319-
1320-
:return: paths to annotations uploaded, could-not-upload, missing-images
1321-
:rtype: tuple of list of strs
1322-
"""
13231302
if recursive_subfolders:
13241303
logger.info(
1325-
"When using recursive subfolder parsing same name annotations in different subfolders will overwrite each other."
1304+
"When using recursive subfolder parsing same name %sannotations in different subfolders will overwrite each other.", pre
13261305
)
13271306

13281307
logger.info(
13291308
"The JSON files should follow specific naming convention. For Vector projects they should be named '<image_name>___objects.json', for Pixel projects JSON file should be names '<image_name>___pixel.json' and also second mask image file should be present with the name '<image_name>___save.png'. In both cases image with <image_name> should be already present on the platform."
13301309
)
13311310

1332-
logger.info("Existing annotations will be overwritten.")
1311+
logger.info("Existing %sannotations will be overwritten.", pre)
13331312
if not isinstance(project, dict):
13341313
project = get_project_metadata_bare(project)
13351314

@@ -1544,7 +1523,7 @@ def upload_preannotations_from_folder_to_project(
15441523
:rtype: tuple of list of strs
15451524
"""
15461525
return _upload_pre_or_annotations_from_folder_to_project(
1547-
project, folder_path, True, from_s3_bucket, recursive_subfolders
1526+
project, folder_path, "pre", from_s3_bucket, recursive_subfolders
15481527
)
15491528

15501529

tests/test_missing_annotation_upload.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ def test_missing_preannotation_upload(tmpdir):
5454
sa.create_annotation_classes_from_classes_json(
5555
project, from_folder / "classes" / "classes.json"
5656
)
57-
uploaded, couldnt_upload = sa.upload_preannotations_from_folder_to_project(
57+
uploaded, couldnt_upload, missing_images = sa.upload_preannotations_from_folder_to_project(
5858
project, from_folder
5959
)
6060
print(uploaded, couldnt_upload)
61-
assert len(uploaded) == 2
61+
assert len(uploaded) == 1
6262
assert len(couldnt_upload) == 2
63+
assert len(missing_images) == 1
6364

6465
assert "tests/sample_project_vector_for_checks/example_image_1.jpg___objects.json" in uploaded
65-
assert "tests/sample_project_vector_for_checks/example_image_5.jpg___objects.json" in uploaded
6666
assert "tests/sample_project_vector_for_checks/example_image_2.jpg___objects.json" in couldnt_upload
6767
assert "tests/sample_project_vector_for_checks/example_image_4.jpg___objects.json" in couldnt_upload
68+
assert "tests/sample_project_vector_for_checks/example_image_5.jpg___objects.json" in missing_images

0 commit comments

Comments
 (0)