@@ -1712,17 +1712,67 @@ def upload_annotations(
17121712 }
17131713
17141714 """
1715- project , folder = self .controller .get_project_folder_by_path (project )
1716- response = self .controller .annotations .upload_multiple (
1717- project = project ,
1718- folder = folder ,
1719- annotations = annotations ,
1720- keep_status = keep_status ,
1721- user = self .controller .current_user ,
1715+ project_name , folder_name = extract_project_folder (project )
1716+ project = self .controller .get_project (project_name )
1717+ folder = project .get_folder (folder_name )
1718+
1719+ failed , skipped = [], []
1720+ name_annotation_map = {}
1721+ for annotation in annotations :
1722+ try :
1723+ name = annotation ["metadata" ]["name" ]
1724+ name_annotation_map [name ] = annotation
1725+ except KeyError :
1726+ failed .append (annotation )
1727+ logger .info (
1728+ f"Uploading { len (name_annotation_map )} /{ len (annotations )} "
1729+ f"annotations to the project { project .name } ."
17221730 )
1723- if response .errors :
1724- raise AppException (response .errors )
1725- return response .data
1731+
1732+ folder_items = folder .list_items (item_names = list (name_annotation_map .keys ()))
1733+ name_item_map = {i .name : i for i in folder_items }
1734+ len_existing , len_provided = len (folder_items ), len (name_annotation_map )
1735+ if len_existing < len_provided :
1736+ logger .warning (
1737+ f"Couldn't find { len_provided - len_existing } /{ len_provided } "
1738+ "items in the given directory that match the annotations."
1739+ )
1740+ item_id_annotation_pairs = []
1741+ item_id_name_map = {}
1742+ for annotation_name , annotation in name_annotation_map .items ():
1743+ item = name_item_map .get (annotation_name )
1744+ if item :
1745+ # Verifies value is not NaN for data integrity
1746+ try :
1747+ json .dumps (annotation , allow_nan = False )
1748+ except ValueError :
1749+ failed .append (annotation_name )
1750+ continue
1751+
1752+ item_id_annotation_pairs .append (
1753+ (item .id , annotation )
1754+ )
1755+ item_id_name_map [item .id ] = annotation_name
1756+ else :
1757+ skipped .append (annotation_name )
1758+
1759+ failed_ids = folder .upload_annotations (item_id_annotation_pairs )
1760+ failed .extend (item_id_name_map [i ] for i in failed_ids )
1761+ uploaded_annotations = list (set (item_id_name_map .values ()) - set (failed ).union (set (skipped )))
1762+ if uploaded_annotations and not keep_status :
1763+ try :
1764+ folder .set_items_annotation_statuses (
1765+ items = uploaded_annotations ,
1766+ annotation_status = constants .AnnotationStatus .IN_PROGRESS ,
1767+ )
1768+ except Exception :
1769+ raise AppException ("Failed to change status." )
1770+
1771+ return {
1772+ "succeeded" : uploaded_annotations ,
1773+ "failed" : failed ,
1774+ "skipped" : skipped ,
1775+ }
17261776
17271777 def upload_annotations_from_folder_to_project (
17281778 self ,
@@ -1764,6 +1814,8 @@ def upload_annotations_from_folder_to_project(
17641814 """
17651815
17661816 project_name , folder_name = extract_project_folder (project )
1817+ project = self .controller .get_project (project_name )
1818+ folder = project .get_folder (folder_name )
17671819 project_folder_name = project_name + (f"/{ folder_name } " if folder_name else "" )
17681820
17691821 if recursive_subfolders :
@@ -1783,11 +1835,9 @@ def upload_annotations_from_folder_to_project(
17831835 logger .info (
17841836 f"Uploading { len (annotation_paths )} annotations from { folder_path } to the project { project_folder_name } ."
17851837 )
1786- project , folder = self .controller .get_project_folder (project_name , folder_name )
17871838 response = self .controller .annotations .upload_from_folder (
17881839 project = project ,
17891840 folder = folder ,
1790- user = self .controller .current_user ,
17911841 annotation_paths = annotation_paths , # noqa: E203
17921842 client_s3_bucket = from_s3_bucket ,
17931843 folder_path = folder_path ,
@@ -1831,8 +1881,8 @@ def upload_image_annotations(
18311881 """
18321882
18331883 project_name , folder_name = extract_project_folder (project )
1834-
1835- project = self . controller . projects . get_by_name ( project_name ). data
1884+ project = self . controller . get_project ( project_name )
1885+ folder = project . get_folder ( folder_name )
18361886 if project .type not in constants .ProjectType .images :
18371887 raise AppException (LIMITED_FUNCTIONS [project .type ])
18381888
@@ -1851,7 +1901,6 @@ def upload_image_annotations(
18511901 if verbose :
18521902 logger .info ("Uploading annotations from %s." , annotation_json )
18531903 annotation_json = json .load (open (annotation_json ))
1854- folder = self .controller .get_folder (project , folder_name )
18551904 if not folder :
18561905 raise AppException ("Folder not found." )
18571906
0 commit comments