@@ -654,83 +654,7 @@ def get_image_preannotations(project, image_name):
654654 "preannotation_json_filename": filename on server,
655655 :rtype: dict
656656 """
657- image = get_image_metadata (project , image_name )
658- team_id , project_id , image_id , folder_id = image ["team_id" ], image [
659- "project_id" ], image ["id" ], image ['folder_id' ]
660- if not isinstance (project , dict ):
661- project = get_project_metadata_bare (project )
662- project_type = project ["type" ]
663-
664- params = {
665- 'team_id' : team_id ,
666- 'project_id' : project_id ,
667- 'folder_id' : folder_id
668- }
669- response = _api .send_request (
670- req_type = 'GET' ,
671- path = f'/image/{ image_id } /annotation/getAnnotationDownloadToken' ,
672- params = params
673- )
674- if not response .ok :
675- raise SABaseException (response .status_code , response .text )
676- res = response .json ()
677-
678- annotation_classes = search_annotation_classes (project )
679- annotation_classes_dict = get_annotation_classes_id_to_name (
680- annotation_classes
681- )
682- if project_type == "Vector" :
683- res = res ['preannotation' ]
684- url = res ["url" ]
685- annotation_json_filename = url .rsplit ('/' , 1 )[- 1 ]
686- headers = res ["headers" ]
687- response = requests .get (url = url , headers = headers )
688- if not response .ok :
689- logger .warning (
690- "No preannotation available for image %s." , image_name
691- )
692- return {
693- "preannotation_json_filename" : None ,
694- "preannotation_json" : None
695- }
696- res_json = response .json ()
697- fill_class_and_attribute_names (res_json , annotation_classes_dict )
698- return {
699- "preannotation_json_filename" : annotation_json_filename ,
700- "preannotation_json" : res_json
701- }
702- else : # pixel
703- res_json = res ['preAnnotationJson' ]
704- url = res_json ["url" ]
705- preannotation_json_filename = url .rsplit ('/' , 1 )[- 1 ]
706- headers = res_json ["headers" ]
707- response = requests .get (url = url , headers = headers )
708- if not response .ok :
709- logger .warning ("No preannotation available." )
710- return {
711- "preannotation_json_filename" : None ,
712- "preannotation_json" : None ,
713- "preannotation_mask_filename" : None ,
714- "preannotation_mask" : None ,
715- }
716- preannotation_json = response .json ()
717- fill_class_and_attribute_names (
718- preannotation_json , annotation_classes_dict
719- )
720-
721- res_mask = res ['preAnnotationSavePng' ]
722- url = res_mask ["url" ]
723- preannotation_mask_filename = url .rsplit ('/' , 1 )[- 1 ]
724- annotation_json_filename = url .rsplit ('/' , 1 )[- 1 ]
725- headers = res_mask ["headers" ]
726- response = requests .get (url = url , headers = headers )
727- mask = io .BytesIO (response .content )
728- return {
729- "preannotation_json_filename" : preannotation_json_filename ,
730- "preannotation_json" : preannotation_json ,
731- "preannotation_mask_filename" : preannotation_mask_filename ,
732- "preannotation_mask" : mask
733- }
657+ return _get_image_pre_or_annotations (project , image_name , "pre" , "Vector" )
734658
735659
736660def get_image_annotations (project , image_name , project_type = None ):
@@ -748,6 +672,10 @@ def get_image_annotations(project, image_name, project_type=None):
748672 "annotation_mask_filename": mask filename on server
749673 :rtype: dict
750674 """
675+ return _get_image_pre_or_annotations (project , image_name , "" , project_type )
676+
677+
678+ def _get_image_pre_or_annotations (project , image_name , pre , project_type = None ):
751679 image = get_image_metadata (project , image_name )
752680 team_id , project_id , image_id , folder_id = image ["team_id" ], image [
753681 "project_id" ], image ["id" ], image ['folder_id' ]
@@ -777,7 +705,8 @@ def get_image_annotations(project, image_name, project_type=None):
777705 annotation_classes_dict = get_annotation_classes_id_to_name (
778706 annotation_classes
779707 )
780- main_annotations = res ["annotations" ]["MAIN" ][0 ]
708+ loc = "MAIN" if pre == "" else "PREANNOTATION"
709+ main_annotations = res ["annotations" ][loc ][0 ]
781710 response = requests .get (
782711 url = main_annotations ["annotation_json_path" ]["url" ],
783712 headers = main_annotations ["annotation_json_path" ]["headers" ]
@@ -789,9 +718,9 @@ def get_image_annotations(project, image_name, project_type=None):
789718 res_json = response .json ()
790719 fill_class_and_attribute_names (res_json , annotation_classes_dict )
791720 result = {
792- " annotation_json" :
721+ f" { pre } annotation_json" :
793722 response .json (),
794- " annotation_json_filename" :
723+ f" { pre } annotation_json_filename" :
795724 common .get_annotation_json_name (image_name , project_type )
796725 }
797726 if project_type == "Pixel" :
@@ -806,15 +735,14 @@ def get_image_annotations(project, image_name, project_type=None):
806735 "Couldn't load annotations" + response .text
807736 )
808737 mask = io .BytesIO (response .content )
809- result ["annotation_mask" ] = mask
810- result ["annotation_mask_filename" ] = common .get_annotation_png_name (
811- image_name
812- )
738+ result [f"{ pre } annotation_mask" ] = mask
739+ result [f"{ pre } annotation_mask_filename"
740+ ] = common .get_annotation_png_name (image_name )
813741 else :
814742 result .update (
815743 {
816- " annotation_mask" : None ,
817- " annotation_mask_filename" : None
744+ f" { pre } annotation_mask" : None ,
745+ f" { pre } annotation_mask_filename" : None
818746 }
819747 )
820748 return result
@@ -834,29 +762,40 @@ def download_image_annotations(project, image_name, local_dir_path):
834762 :return: paths of downloaded annotations
835763 :rtype: tuple
836764 """
765+ return _download_image_pre_or_annotations (
766+ project , image_name , local_dir_path , ""
767+ )
768+
769+
770+ def _download_image_pre_or_annotations (
771+ project , image_name , local_dir_path , pre
772+ ):
837773 if not isinstance (project , dict ):
838774 project = get_project_metadata_bare (project )
839775
840- annotation = get_image_annotations (project , image_name )
776+ annotation = _get_image_pre_or_annotations (
777+ project , image_name , pre , project ["type" ]
778+ )
841779
842- if annotation [" annotation_json_filename" ] is None :
780+ if annotation [f" { pre } annotation_json_filename" ] is None :
843781 image = get_image_metadata (project , image_name )
844782 logger .warning ("No annotation found for image %s." , image ["name" ])
845783 return None
846784 return_filepaths = []
847- json_path = Path (local_dir_path ) / annotation ["annotation_json_filename" ]
785+ json_path = Path (local_dir_path
786+ ) / annotation [f"{ pre } annotation_json_filename" ]
848787 return_filepaths .append (str (json_path ))
849788 if project ["type" ] == "Vector" :
850789 with open (json_path , "w" ) as f :
851- json .dump (annotation [" annotation_json" ], f , indent = 4 )
790+ json .dump (annotation [f" { pre } annotation_json" ], f , indent = 4 )
852791 else :
853792 with open (json_path , "w" ) as f :
854- json .dump (annotation [" annotation_json" ], f , indent = 4 )
855- if annotation [" annotation_mask_filename" ] is not None :
793+ json .dump (annotation [f" { pre } annotation_json" ], f , indent = 4 )
794+ if annotation [f" { pre } annotation_mask_filename" ] is not None :
856795 mask_path = Path (local_dir_path
857- ) / annotation [" annotation_mask_filename" ]
796+ ) / annotation [f" { pre } annotation_mask_filename" ]
858797 with open (mask_path , "wb" ) as f :
859- f .write (annotation [" annotation_mask" ].getbuffer ())
798+ f .write (annotation [f" { pre } annotation_mask" ].getbuffer ())
860799 else :
861800 mask_path = None
862801 return_filepaths .append (str (mask_path ))
@@ -878,26 +817,9 @@ def download_image_preannotations(project, image_name, local_dir_path):
878817 :return: paths of downloaded pre-annotations
879818 :rtype: tuple
880819 """
881- if not isinstance (project , dict ):
882- project = get_project_metadata_bare (project )
883- annotation = get_image_preannotations (project , image_name )
884- if annotation ["preannotation_json_filename" ] is None :
885- return (None , )
886- return_filepaths = []
887- json_path = Path (local_dir_path ) / annotation ["preannotation_json_filename" ]
888- return_filepaths .append (str (json_path ))
889- if project ["type" ] == "Vector" :
890- with open (json_path , "w" ) as f :
891- json .dump (annotation ["preannotation_json" ], f )
892- else :
893- with open (json_path , "w" ) as f :
894- json .dump (annotation ["preannotation_json" ], f )
895- mask_path = Path (local_dir_path
896- ) / annotation ["preannotation_mask_filename" ]
897- with open (mask_path , "wb" ) as f :
898- f .write (annotation ["preannotation_mask" ].getbuffer ())
899- return_filepaths .append (str (mask_path ))
900- return tuple (return_filepaths )
820+ return _download_image_pre_or_annotations (
821+ project , image_name , local_dir_path , "pre"
822+ )
901823
902824
903825def upload_image_annotations (
0 commit comments