99
1010def df_to_annotations (df , output_dir ):
1111 """Converts and saves pandas DataFrame annotation info (see aggregate_annotations_as_df) in output_dir
12- The DataFrame should have columns: "imageName", "classNmae", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", trackingId", "probability", "pointLabels", "meta", "commentResolved", "classColor"
12+ The DataFrame should have columns: "imageName", "classNmae", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", trackingId", "probability", "pointLabels", "meta", "commentResolved", "classColor", "groupId"
1313
1414 Currently only works for Vector projects.
1515
16- :param df: pandas DataFrame
16+ :param df: pandas DataFrame of annotations possibly created by aggregate_annotations_as_df
1717 :type df: pandas.DataFrame
18- :param include_classes_wo_annotations: enables inclusion of classes without annotations info
19- :type include_classes_wo_annotations: bool
18+ :param output_dir: output dir for annotations and classes.json
19+ :type output_dir: str or Pathlike
2020
21- :return: DataFrame on annotations with columns: ["imageName", "classNmae", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", trackingId", "probability", "pointLabels", "meta", "commentResolved"]
22- :rtype: pandas DataFrame
2321 """
2422
2523 project_suffix = "objects.json"
@@ -50,6 +48,7 @@ def df_to_annotations(df, output_dir):
5048 )
5149 instance_annotation ["trackingId" ] = instance_df .iloc [0 ]["trackingId"
5250 ]
51+ instance_annotation ["groupId" ] = int (instance_df .iloc [0 ]["groupId" ])
5352 instance_annotation .update (annotation_meta )
5453 for _ , row in instance_df .iterrows ():
5554 if row ["attributeGroupName" ] is not None :
@@ -60,6 +59,14 @@ def df_to_annotations(df, output_dir):
6059 }
6160 )
6261 image_annotation .append (instance_annotation )
62+
63+ comments = image_df [image_df ["type" ] == "comment" ]
64+ for _ , comment in comments .iterrows ():
65+ comment_json = {"type" : "comment" }
66+ comment_json .update (comment ["meta" ])
67+ comment_json ["resolved" ] = comment ["commentResolved" ]
68+ image_annotation .append (comment_json )
69+
6370 json .dump (
6471 image_annotation ,
6572 open (output_dir / f"{ image } ___{ project_suffix } " , "w" ),
@@ -124,7 +131,7 @@ def aggregate_annotations_as_df(
124131 :param include_comments: enables inclusion of comments info as commentResolved column
125132 :type include_comments: bool
126133
127- :return: DataFrame on annotations with columns: "imageName", "instanceId" className", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", "trackingId", "probability", "pointLabels", "meta" (geometry information as string), "commentResolved", "classColor"
134+ :return: DataFrame on annotations with columns: "imageName", "instanceId" className", "attributeGroupName", "attributeName", "type", "error", "locked", "visible", "trackingId", "probability", "pointLabels", "meta" (geometry information as string), "commentResolved", "classColor", "groupId"
128135 :rtype: pandas DataFrame
129136 """
130137
@@ -148,6 +155,7 @@ def aggregate_annotations_as_df(
148155 "pointLabels" : [],
149156 "meta" : [],
150157 "classColor" : [],
158+ "groupId" : []
151159 }
152160
153161 if include_comments :
@@ -192,12 +200,9 @@ def __append_annotation(annotation_dict):
192200 annotation_image_name = annotation_path .name .split ("___" )[0 ]
193201 annotation_instance_id = 0
194202 for annotation in annotation_json :
195-
196203 annotation_type = annotation .get ("type" , "mask" )
197-
198204 if annotation_type in ['meta' , 'tag' ]:
199205 continue
200-
201206 if annotation_type == "comment" :
202207 if include_comments :
203208 comment_resolved = annotation ["resolved" ]
@@ -215,26 +220,18 @@ def __append_annotation(annotation_dict):
215220 }
216221 )
217222 continue
218-
219223 annotation_instance_id += 1
220-
221224 annotation_class_name = annotation .get ("className" )
222-
223- if annotation_class_name :
224- annotation_class_color = class_name_to_color [annotation_class_name ]
225- else :
225+ if annotation_class_name is None :
226226 raise SABaseException (
227227 0 , "Annotation class not found in classes.json"
228228 )
229-
229+ annotation_class_color = class_name_to_color [annotation_class_name ]
230+ annotation_group_id = annotation .get ("groupId" )
230231 annotation_locked = annotation .get ("locked" )
231-
232232 annotation_visible = annotation .get ("visible" )
233-
234233 annotation_tracking_id = annotation .get ("trackingId" )
235-
236234 annotation_meta = None
237-
238235 if annotation_type in ["bbox" , "polygon" , "polyline" , "cuboid" ]:
239236 annotation_meta = {"points" : annotation ["points" ]}
240237 elif annotation_type == "point" :
@@ -254,13 +251,9 @@ def __append_annotation(annotation_dict):
254251 "connections" : annotation ["connections" ],
255252 "points" : annotation ["points" ]
256253 }
257-
258254 annotation_error = annotation .get ('error' )
259-
260255 annotation_probability = annotation .get ("probability" )
261-
262256 annotation_point_labels = annotation .get ("pointLabels" )
263-
264257 attributes = annotation .get ("attributes" )
265258
266259 if not attributes :
@@ -277,7 +270,8 @@ def __append_annotation(annotation_dict):
277270 "error" : annotation_error ,
278271 "probability" : annotation_probability ,
279272 "pointLabels" : annotation_point_labels ,
280- "classColor" : annotation_class_color
273+ "classColor" : annotation_class_color ,
274+ "groupId" : annotation_group_id
281275 }
282276 )
283277
@@ -301,7 +295,8 @@ def __append_annotation(annotation_dict):
301295 "error" : annotation_error ,
302296 "probability" : annotation_probability ,
303297 "pointLabels" : annotation_point_labels ,
304- "classColor" : annotation_class_color
298+ "classColor" : annotation_class_color ,
299+ "groupId" : annotation_group_id
305300 }
306301 )
307302
0 commit comments