@@ -116,6 +116,35 @@ def scale_segmentation(
116116 return seg_array_with_cat
117117
118118
119+ def convert_bboxes (
120+ annotations : list [list [float ]],
121+ ) -> list [list [float ]]:
122+ """
123+ Converts annotations in YOLO detection format (class_id, cx, cy, w, h) or YOLO segmentation format \
124+ (class_id, x1, y1, x2, y2, ..., xn, yn) to YOLO segmentation format.
125+
126+ Args:
127+ annotations (list[list[float]]): List of annotations in any YOLO format.
128+
129+ Returns:
130+ list[list[float]]: List of annotations in any YOLO segmentation format.
131+ """
132+ segmentation_data = []
133+
134+ for anno in annotations :
135+ # YOLO segmentation format
136+ if len (anno ) > 5 :
137+ segmentation_data .append (anno )
138+ continue
139+
140+ # YOLO detection format
141+ category_id , cx , cy , w , h = anno
142+ x1 , y1 , x2 , y2 = cx - w / 2 , cy - h / 2 , cx + w / 2 , cy + h / 2
143+ segmentation_data .append ([category_id , x1 , y1 , x2 , y1 , x2 , y2 , x1 , y2 ])
144+
145+ return segmentation_data
146+
147+
119148def tensorlize (data ):
120149 try :
121150 img_paths , bboxes , img_ratios = zip (* data )
0 commit comments