Skip to content

Commit 06cb39b

Browse files
authored
Merge pull request #369 from superannotateai/friday-719
Add log
2 parents 9bea782 + 082361a commit 06cb39b

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ def upload_image_annotations(
21122112
mask=mask,
21132113
verbose=verbose,
21142114
)
2115-
if response.errors:
2115+
if response.errors and not response.errors == constances.INVALID_JSON_MESSAGE:
21162116
raise AppException(response.errors)
21172117

21182118

src/superannotate/lib/core/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@
117117
" Run 'pip install --upgrade superannotate' to"
118118
" upgrade from your version {} to {}"
119119
)
120+
121+
USE_VALIDATE_MESSAGE = (
122+
"Use the validate_annotations function to discover the possible reason(s) for "
123+
"which an annotation is invalid."
124+
)
125+
126+
INVALID_JSON_MESSAGE = "Invalid json"
127+
120128
__alL__ = (
121129
ProjectType,
122130
UserRole,

src/superannotate/lib/core/reporter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def __init__(
2424
self.custom_messages = defaultdict(set)
2525
self.progress_bar = None
2626

27+
def disable_warnings(self):
28+
self._log_warning = False
29+
30+
def enable_warnings(self):
31+
self._log_warning = True
32+
2733
def log_info(self, value: str):
2834
if self._log_info:
2935
self.logger.info(value)

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ def annotations_to_upload(self):
137137
self._annotations_to_upload = annotations_to_upload
138138
return self._annotations_to_upload
139139

140+
@property
141+
def missing_annotations(self):
142+
if not self._missing_annotations:
143+
self._missing_annotations = []
144+
return self._missing_annotations
145+
140146
def get_annotation_upload_data(
141147
self, image_ids: List[int]
142148
) -> UploadAnnotationAuthData:
@@ -162,6 +168,7 @@ def _upload_annotation(
162168
bucket,
163169
):
164170
try:
171+
self.reporter.disable_warnings()
165172
response = UploadAnnotationUseCase(
166173
project=self._project,
167174
folder=self._folder,
@@ -190,6 +197,8 @@ def _upload_annotation(
190197
except Exception as e:
191198
logger.debug(str(e), exc_info=True)
192199
return path, False
200+
finally:
201+
self.reporter.enable_warnings()
193202

194203
def get_bucket_to_upload(self, ids: List[int]):
195204
upload_data = self.get_annotation_upload_data(ids)
@@ -223,7 +232,9 @@ def _log_report(self):
223232
logger.warning(template.format("', '".join(values)))
224233
if self.reporter.custom_messages.get("invalid_jsons"):
225234
logger.warning(
226-
f"Couldn't validate {len(self.reporter.custom_messages['invalid_jsons'])}/{len(self._annotations_to_upload + self._missing_annotations)} annotations from {self._folder_path}."
235+
f"Couldn't validate {len(self.reporter.custom_messages['invalid_jsons'])}/"
236+
f"{len(self.annotations_to_upload + self.missing_annotations)} annotations from {self._folder_path}. "
237+
f"{constances.USE_VALIDATE_MESSAGE}"
227238
)
228239

229240
def execute(self):
@@ -281,7 +292,7 @@ def execute(self):
281292
self._response.data = (
282293
uploaded_annotations,
283294
failed_annotations,
284-
[annotation.path for annotation in self._missing_annotations],
295+
[annotation.path for annotation in self.missing_annotations],
285296
)
286297
return self._response
287298

@@ -477,12 +488,13 @@ def execute(self):
477488
)
478489
self._images.update(self._image)
479490
if self._verbose:
480-
logger.info(
481-
"Uploading annotations for image %s in project %s.",
482-
str(self._image.name),
483-
self._project.name,
491+
self.reporter.log_info(
492+
f"Uploading annotations for image {str(self._image.name)} in project {self._project.name}."
484493
)
485494
else:
486-
self._response.errors = "Invalid json"
495+
self._response.errors = constances.INVALID_JSON_MESSAGE
487496
self.reporter.store_message("invalid_jsons", self._annotation_path)
497+
self.reporter.log_warning(
498+
f"Couldn't validate annotations. {constances.USE_VALIDATE_MESSAGE}"
499+
)
488500
return self._response

src/superannotate/lib/infrastructure/controller.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ def team_id(self) -> int:
216216

217217
@property
218218
def default_reporter(self):
219-
if not self._reporter:
220-
self._reporter = Reporter()
221-
return self._reporter
219+
return Reporter()
222220

223221
@timed_lru_cache(seconds=3600)
224222
def get_auth_data(self, project_id: int, team_id: int, folder_id: int):

tests/integration/test_interface.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,23 @@ def test_download_fuse_without_classes(self):
156156
)
157157
self.assertIsNotNone(result)
158158

159+
def test_validate_log_for_single_uplaod(self):
160+
with self.assertLogs() as logs:
161+
sa.upload_image_to_project(self.PROJECT_NAME, f"{self.folder_path}/{self.EXAMPLE_IMAGE_1}")
162+
sa.upload_image_annotations(
163+
self.PROJECT_NAME, self.EXAMPLE_IMAGE_1, {
164+
"metadatas": {
165+
"name": "example_image_1.jpg",
166+
"width": 1024,
167+
"height": 683,
168+
"status": "Completed",
169+
},
170+
"instance": []
171+
}
172+
)
173+
self.assertEqual(len(logs[1][0]), 150)
174+
175+
159176

160177
class TestPixelInterface(BaseTestCase):
161178
PROJECT_NAME = "Interface Pixel test"

0 commit comments

Comments
 (0)