Skip to content

Commit cea3db0

Browse files
authored
Merge pull request #678 from superannotateai/FRIDAY-2706
fix upload_images_to_project
2 parents 10719ef + cf01198 commit cea3db0

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,25 +2048,21 @@ def upload_images_to_project(
20482048
from_s3_bucket=from_s3_bucket,
20492049
)
20502050

2051-
images_to_upload, duplicates = use_case.images_to_upload
2052-
if len(duplicates):
2053-
logger.warning(
2054-
f"{len(duplicates)} duplicated images found that won't be uploaded."
2055-
)
2051+
images_to_upload, existing_items = use_case.images_to_upload
20562052
logger.info(f"Uploading {len(images_to_upload)} images to project {project}.")
2057-
uploaded, failed_images, duplications = [], [], duplicates
2053+
uploaded, failed_images = [], []
20582054
if not images_to_upload:
2059-
return uploaded, failed_images, duplications
2055+
return uploaded, failed_images, existing_items
20602056
if use_case.is_valid():
20612057
with tqdm(
20622058
total=len(images_to_upload), desc="Uploading images"
20632059
) as progress_bar:
20642060
for _ in use_case.execute():
20652061
progress_bar.update(1)
2066-
uploaded, failed_images, duplications = use_case.data
2067-
if duplications:
2068-
logger.info(f"Duplicated images {', '.join(duplications)}")
2069-
return uploaded, failed_images, duplications
2062+
uploaded, failed_images, existing_items = use_case.data
2063+
if existing_items:
2064+
logger.info(f"Existing images {', '.join(existing_items)}")
2065+
return uploaded, failed_images, existing_items
20702066
raise AppException(use_case.response.errors)
20712067

20722068
def aggregate_annotations_as_df(

src/superannotate/lib/core/usecases/images.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,17 @@ def filter_paths(self, paths: List[str]):
10231023

10241024
filtered_paths = []
10251025
duplicated_paths = []
1026+
existing_items = []
10261027
for file_name in name_path_map:
10271028
if len(name_path_map[file_name]) > 1:
10281029
duplicated_paths.extend(name_path_map[file_name][1:])
10291030
filtered_paths.append(name_path_map[file_name][0])
10301031

1032+
if duplicated_paths:
1033+
logger.warning(
1034+
f"{len(duplicated_paths)} duplicate paths found that won't be uploaded."
1035+
)
1036+
10311037
image_list = []
10321038
for i in range(0, len(filtered_paths), CHUNK_SIZE):
10331039
response = self._service_provider.items.list_by_names(
@@ -1046,11 +1052,12 @@ def filter_paths(self, paths: List[str]):
10461052
images_to_upload = []
10471053

10481054
for path in filtered_paths:
1049-
if Path(path).name not in image_list:
1055+
image_name = Path(path).name
1056+
if image_name not in image_list:
10501057
images_to_upload.append(path)
10511058
else:
1052-
duplicated_paths.append(path)
1053-
return list(set(images_to_upload)), duplicated_paths
1059+
existing_items.append(image_name)
1060+
return list(set(images_to_upload)), existing_items
10541061

10551062
@property
10561063
def images_to_upload(self):
@@ -1060,7 +1067,7 @@ def images_to_upload(self):
10601067

10611068
def execute(self):
10621069
if self.is_valid():
1063-
images_to_upload, _ = self.images_to_upload
1070+
images_to_upload, existing_items = self.images_to_upload
10641071
images_to_upload = images_to_upload[: self.auth_data["availableImageCount"]]
10651072
if not images_to_upload:
10661073
return self._response
@@ -1083,7 +1090,7 @@ def execute(self):
10831090
yield
10841091

10851092
uploaded = []
1086-
duplications = [] # existing items
1093+
attach_duplications_list = []
10871094
for i in range(0, len(uploaded_images), 100):
10881095
response = AttachFileUrlsUseCase(
10891096
project=self._project,
@@ -1101,10 +1108,14 @@ def execute(self):
11011108
continue
11021109
attachments, attach_duplications = response.data
11031110
uploaded.extend(attachments)
1104-
duplications.extend(attach_duplications)
1111+
attach_duplications_list.extend(attach_duplications)
1112+
if attach_duplications_list:
1113+
logger.debug(
1114+
f"{len(attach_duplications_list)} item attachments duplicates found."
1115+
)
11051116
uploaded = [image["name"] for image in uploaded]
11061117
failed_images = [image.split("/")[-1] for image in failed_images]
1107-
self._response.data = uploaded, failed_images, duplications
1118+
self._response.data = uploaded, failed_images, existing_items
11081119
return self._response
11091120

11101121

tests/integration/test_image_upload.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ def test_multiple_image_upload(self):
6767
f"{self.folder_path}/example_image_1.jpg",
6868
f"{self.folder_path}/example_image_2.jpg",
6969
f"{self.folder_path}/example_image_3.jpg",
70-
f"{self.folder_path}/example_image_4.jpg",
7170
],
7271
annotation_status="InProgress",
7372
)
74-
self.assertEqual(len(uploaded), 4)
73+
self.assertEqual(len(uploaded), 3)
7574
self.assertEqual(len(could_not_upload), 0)
7675
self.assertEqual(len(existing_images), 0)
7776

@@ -85,9 +84,9 @@ def test_multiple_image_upload(self):
8584
],
8685
annotation_status="InProgress",
8786
)
88-
self.assertEqual(len(uploaded), 0)
87+
self.assertEqual(len(uploaded), 1)
8988
self.assertEqual(len(could_not_upload), 0)
90-
self.assertEqual(len(existing_images), 4)
89+
self.assertEqual(len(existing_images), 3)
9190

9291
uploaded, could_not_upload, existing_images = sa.upload_images_to_project(
9392
self.PROJECT_NAME, [f"{self.folder_path}/dd.jpg"]
@@ -115,12 +114,12 @@ def test_multiple_image_upload_with_duplicates(self):
115114
)
116115
assert (
117116
mock_log.records[0].msg
118-
== "2 duplicated images found that won't be uploaded."
117+
== "2 duplicate paths found that won't be uploaded."
119118
)
120119
assert (
121120
mock_log.records[1].msg
122121
== "Uploading 3 images to project test_multiple_image_upload."
123122
)
124123
self.assertEqual(len(uploaded), 3)
125124
self.assertEqual(len(could_not_upload), 0)
126-
self.assertEqual(len(existing_images), 2)
125+
self.assertEqual(len(existing_images), 0)

0 commit comments

Comments
 (0)