1212from lib .core .entities import FolderEntity
1313from lib .core .entities import ImageEntity
1414from lib .core .entities import ProjectEntity
15- from lib .core .exceptions import AppException
1615from lib .core .helpers import convert_to_video_editor_json
1716from lib .core .helpers import fill_annotation_ids
17+ from lib .core .helpers import fill_document_tags
1818from lib .core .helpers import map_annotation_classes_name
1919from lib .core .reporter import Reporter
2020from lib .core .service_types import UploadAnnotationAuthData
@@ -110,6 +110,7 @@ def annotations_to_upload(self):
110110 for idx , detail in enumerate (images_detail ):
111111 if detail .name == image_data .name :
112112 images_detail [idx ] = detail ._replace (id = image_data .uuid )
113+ break
113114
114115 missing_annotations = list (
115116 filter (lambda image_detail : image_detail .id is None , images_detail )
@@ -118,12 +119,9 @@ def annotations_to_upload(self):
118119 filter (lambda image_detail : image_detail .id is not None , images_detail )
119120 )
120121 if missing_annotations :
121- for missing in missing_annotations :
122- logger .warning (
123- f"Couldn't find image { missing .path } for annotation upload."
124- )
125- if not annotations_to_upload :
126- raise AppException ("No image to attach annotations." )
122+ logger .warning (
123+ f"Couldn't find { len (missing_annotations )} /{ len (annotations_to_upload + missing_annotations )} items on the platform that match the annotations you want to upload."
124+ )
127125 self ._missing_annotations = missing_annotations
128126 self ._annotations_to_upload = annotations_to_upload
129127 return self ._annotations_to_upload
@@ -196,9 +194,7 @@ def _log_report(self):
196194 template = "Could not find attribute groups matching existing attribute groups on the platform: [{}]"
197195 elif key == "missing_attributes" :
198196 template = "Could not find attributes matching existing attributes on the platform: [{}]"
199- logger .warning (
200- template .format ("', '" .join (values ))
201- )
197+ logger .warning (template .format ("', '" .join (values )))
202198
203199 def execute (self ):
204200 uploaded_annotations = []
@@ -207,21 +203,27 @@ def execute(self):
207203 iterations_range = range (
208204 0 , len (self .annotations_to_upload ), self .AUTH_DATA_CHUNK_SIZE
209205 )
210- self .reporter .start_progress (iterations_range , description = "Uploading Annotations" )
211- for _ in iterations_range :
206+ self .reporter .start_progress (
207+ len (self .annotations_to_upload ), description = "Uploading Annotations"
208+ )
209+ for step in iterations_range :
212210 annotations_to_upload = self .annotations_to_upload [
213- _ : _ + self .AUTH_DATA_CHUNK_SIZE # noqa: E203
214- ]
211+ step : step + self .AUTH_DATA_CHUNK_SIZE
212+ ] # noqa: E203
215213 upload_data = self .get_annotation_upload_data (
216214 [int (image .id ) for image in annotations_to_upload ]
217215 )
218- bucket = self .get_bucket_to_upload ([int (image .id ) for image in annotations_to_upload ])
216+ bucket = self .get_bucket_to_upload (
217+ [int (image .id ) for image in annotations_to_upload ]
218+ )
219219 if bucket :
220220 image_id_name_map = {
221221 image .id : image for image in self .annotations_to_upload
222222 }
223223 # dummy progress
224- for _ in range (len (annotations_to_upload ) - len (upload_data .images )):
224+ for _ in range (
225+ len (annotations_to_upload ) - len (upload_data .images )
226+ ):
225227 self .reporter .update_progress ()
226228 with concurrent .futures .ThreadPoolExecutor (
227229 max_workers = self .MAX_WORKERS
@@ -251,8 +253,6 @@ def execute(self):
251253 [annotation .path for annotation in self ._missing_annotations ],
252254 )
253255 self ._log_report ()
254- else :
255- self ._response .errors = "Could not find annotations matching existing items on the platform."
256256 return self ._response
257257
258258
@@ -337,13 +337,26 @@ def from_s3(self):
337337 def set_annotation_json (self ):
338338 if not self ._annotation_json :
339339 if self ._client_s3_bucket :
340- self ._annotation_json = json .load (self .get_s3_file (self .from_s3 , self ._annotation_path ))
340+ self ._annotation_json = json .load (
341+ self .get_s3_file (self .from_s3 , self ._annotation_path )
342+ )
341343 if self ._project .project_type == constances .ProjectType .PIXEL .value :
342- self ._mask = self .get_s3_file (self .from_s3 , self ._annotation_path .replace (constances .PIXEL_ANNOTATION_POSTFIX , constances .ANNOTATION_MASK_POSTFIX ))
344+ self ._mask = self .get_s3_file (
345+ self .from_s3 ,
346+ self ._annotation_path .replace (
347+ constances .PIXEL_ANNOTATION_POSTFIX ,
348+ constances .ANNOTATION_MASK_POSTFIX ,
349+ ),
350+ )
343351 else :
344352 self ._annotation_json = json .load (open (self ._annotation_path ))
345353 if self ._project .project_type == constances .ProjectType .PIXEL .value :
346- self ._mask = open (self ._annotation_path .replace (constances .PIXEL_ANNOTATION_POSTFIX , constances .ANNOTATION_MASK_POSTFIX ))
354+ self ._mask = open (
355+ self ._annotation_path .replace (
356+ constances .PIXEL_ANNOTATION_POSTFIX ,
357+ constances .ANNOTATION_MASK_POSTFIX ,
358+ )
359+ )
347360
348361 def _is_valid_json (self , json_data : dict ):
349362 use_case = ValidateAnnotationUseCase (
@@ -361,22 +374,28 @@ def prepare_annotations(
361374 templates : List [dict ],
362375 reporter : Reporter ,
363376 ) -> dict :
377+ annotation_classes_name_maps = map_annotation_classes_name (
378+ annotation_classes , reporter
379+ )
364380 if project_type in (
365381 constances .ProjectType .VECTOR .value ,
366382 constances .ProjectType .PIXEL .value ,
367383 constances .ProjectType .DOCUMENT .value ,
368384 ):
369385 fill_annotation_ids (
370386 annotations = annotations ,
371- annotation_classes_name_maps = map_annotation_classes_name (
372- annotation_classes , reporter
373- ),
387+ annotation_classes_name_maps = annotation_classes_name_maps ,
374388 templates = templates ,
375389 reporter = reporter ,
376390 )
377391 elif project_type == constances .ProjectType .VIDEO .value :
378392 annotations = convert_to_video_editor_json (
379- annotations , map_annotation_classes_name (annotation_classes , reporter ), reporter
393+ annotations , annotation_classes_name_maps , reporter
394+ )
395+ if project_type == constances .ProjectType .DOCUMENT .value :
396+ fill_document_tags (
397+ annotations = annotations ,
398+ annotation_classes = annotation_classes_name_maps ,
380399 )
381400 return annotations
382401
@@ -408,7 +427,10 @@ def execute(self):
408427 ],
409428 Body = json .dumps (annotation_json ),
410429 )
411- if self ._project .project_type == constances .ProjectType .PIXEL .value and self ._mask :
430+ if (
431+ self ._project .project_type == constances .ProjectType .PIXEL .value
432+ and self ._mask
433+ ):
412434 bucket .put_object (
413435 Key = self .annotation_upload_data .images [self ._image .uuid ][
414436 "annotation_bluemap_path"
0 commit comments