1+ import glob
12import json
23import logging
34from pathlib import Path
4- import glob
55
66import pandas as pd
7+
78from ..exceptions import SABaseException
9+
810logger = logging .getLogger ("superannotate-python-sdk" )
911
1012
@@ -29,7 +31,12 @@ def df_to_annotations(df, output_dir):
2931 image_height = None
3032 image_width = None
3133 image_df = df [df ["imageName" ] == image ]
32- image_annotation = []
34+ image_annotation = {
35+ "instances" : [],
36+ "metadata" : {},
37+ "tags" : [],
38+ "comments" : []
39+ }
3340 instances = image_df ["instanceId" ].dropna ().unique ()
3441 for instance in instances :
3542 instance_df = image_df [image_df ["instanceId" ] == instance ]
@@ -64,27 +71,31 @@ def df_to_annotations(df, output_dir):
6471 "name" : row ["attributeName" ]
6572 }
6673 )
67- image_annotation .append (instance_annotation )
74+ image_annotation [ "instances" ] .append (instance_annotation )
6875 image_width = image_width or instance_df .iloc [0 ]["imageWidth" ]
6976 image_height = image_height or instance_df .iloc [0 ]["imageHeight" ]
7077 image_pinned = image_pinned or instance_df .iloc [0 ]["imagePinned" ]
7178 image_status = image_status or instance_df .iloc [0 ]["imageStatus" ]
7279
7380 comments = image_df [image_df ["type" ] == "comment" ]
7481 for _ , comment in comments .iterrows ():
75- comment_json = {"type" : "comment" }
82+ comment_json = {}
7683 comment_json .update (comment ["meta" ])
84+ comment_json ["correspondence" ] = comment_json ["comments" ]
85+ del comment_json ["comments" ]
7786 comment_json ["resolved" ] = comment ["commentResolved" ]
78- image_annotation .append (comment_json )
87+ image_annotation ["comments" ].append (comment_json )
88+
89+ tags = image_df [image_df ["type" ] == "tag" ]
90+ for _ , tag in tags .iterrows ():
91+ image_annotation ["tags" ].append (tag ["tag" ])
7992
80- meta = {
81- "type" : "meta" ,
93+ image_annotation ["metadata" ] = {
8294 "width" : int (image_width ),
8395 "height" : int (image_height ),
8496 "status" : image_status ,
8597 "pinned" : bool (image_pinned )
8698 }
87- image_annotation .append (meta )
8899 json .dump (
89100 image_annotation ,
90101 open (output_dir / f"{ image } ___{ project_suffix } " , "w" ),
@@ -224,13 +235,11 @@ def __append_annotation(annotation_dict):
224235
225236 def __get_image_metadata (image_name , annotations ):
226237 image_metadata = {"imageName" : image_name }
227- for annotation in annotations :
228- if "type" in annotation and annotation ["type" ] == "meta" :
229- image_metadata ["imageHeight" ] = annotation .get ("height" )
230- image_metadata ["imageWidth" ] = annotation .get ("width" )
231- image_metadata ["imageStatus" ] = annotation .get ("status" )
232- image_metadata ["imagePinned" ] = annotation .get ("pinned" )
233- break
238+
239+ image_metadata ["imageHeight" ] = annotations ["metadata" ].get ("height" )
240+ image_metadata ["imageWidth" ] = annotations ["metadata" ].get ("width" )
241+ image_metadata ["imageStatus" ] = annotations ["metadata" ].get ("status" )
242+ image_metadata ["imagePinned" ] = annotations ["metadata" ].get ("pinned" )
234243 return image_metadata
235244
236245 def __get_user_metadata (annotation ):
@@ -277,38 +286,30 @@ def __get_user_metadata(annotation):
277286 image_name = annotation_path .name .split (type_postfix )[0 ]
278287 image_metadata = __get_image_metadata (image_name , annotation_json )
279288 annotation_instance_id = 0
280- for annotation in annotation_json :
289+ if include_comments :
290+ for annotation in annotation_json ["comments" ]:
291+ comment_resolved = annotation ["resolved" ]
292+ comment_meta = {
293+ "x" : annotation ["x" ],
294+ "y" : annotation ["y" ],
295+ "comments" : annotation ["correspondence" ]
296+ }
297+ annotation_dict = {
298+ "type" : "comment" ,
299+ "meta" : comment_meta ,
300+ "commentResolved" : comment_resolved ,
301+ }
302+ user_metadata = __get_user_metadata (annotation )
303+ annotation_dict .update (user_metadata )
304+ annotation_dict .update (image_metadata )
305+ __append_annotation (annotation_dict )
306+ if include_tags :
307+ for annotation in annotation_json ["tags" ]:
308+ annotation_dict = {"type" : "tag" , "tag" : annotation }
309+ annotation_dict .update (image_metadata )
310+ __append_annotation (annotation_dict )
311+ for annotation in annotation_json ["instances" ]:
281312 annotation_type = annotation .get ("type" , "mask" )
282- if annotation_type == "meta" :
283- continue
284- if annotation_type == "comment" :
285- if include_comments :
286- comment_resolved = annotation ["resolved" ]
287- comment_meta = {
288- "x" : annotation ["x" ],
289- "y" : annotation ["y" ],
290- "comments" : annotation ["comments" ]
291- }
292- annotation_dict = {
293- "type" : annotation_type ,
294- "meta" : comment_meta ,
295- "commentResolved" : comment_resolved ,
296- }
297- user_metadata = __get_user_metadata (annotation )
298- annotation_dict .update (user_metadata )
299- annotation_dict .update (image_metadata )
300- __append_annotation (annotation_dict )
301- continue
302- if annotation_type == "tag" :
303- if include_tags :
304- annotation_tag = annotation ["name" ]
305- annotation_dict = {
306- "type" : annotation_type ,
307- "tag" : annotation_tag
308- }
309- annotation_dict .update (image_metadata )
310- __append_annotation (annotation_dict )
311- continue
312313 annotation_class_name = annotation .get ("className" )
313314 if annotation_class_name is None or annotation_class_name not in class_name_to_color :
314315 logger .warning (
0 commit comments