@@ -187,22 +187,31 @@ def _generate_yolov5_image_labels_and_masks(self, file_name):
187
187
cv2 .putText (mask , self .label_list [label ], (bbox [0 ], bbox [1 ]), cv2 .FONT_HERSHEY_SIMPLEX , 1 , color , 2 )
188
188
elif self .annotation_type == ANNOTATION_TYPE .POLYGON :
189
189
segmentation = annotation ['segmentation' ]
190
- yolov5_segmentation = self ._convert_to_yolov5_segmentation_format (segmentation , image_width , image_height )
191
- label_txt .write (f"{ label } { yolov5_segmentation } \n " )
190
+ color = random_colors_palette [label ].tolist ()
191
+ instance_color = random .choice (random_colors_palette .tolist ())
192
+ for poly in segmentation :
193
+ yolov5_segmentation = self ._convert_to_yolov5_segmentation_format (poly , image_width , image_height )
194
+ label_txt .write (f"{ label } { yolov5_segmentation } \n " )
195
+ poly = np .array (poly ).reshape (- 1 , 2 )
196
+ poly = poly .astype (np .int32 )
197
+ if self .include_mask :
198
+
199
+ cv2 .fillPoly (mask , [poly ], color )
200
+ # cv2.rectangle(mask, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), color, 2)
201
+ # cv2.putText(mask, self.label_list[label], (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
202
+
203
+ if self .include_instance :
204
+ cv2 .fillPoly (instance_image , [poly ], instance_color )
205
+ # cv2.rectangle(instance_image, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), color, 2)
206
+ # cv2.putText(instance_image, self.label_list[label], (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
192
207
bbox = annotation ['bbox' ]
193
- segmentation = np .array (segmentation ).reshape (- 1 , 2 )
194
- segmentation = segmentation .astype (np .int32 )
208
+ # bbox = annotation['bbox']
195
209
if self .include_mask :
196
- color = random_colors_palette [label ].tolist ()
197
- cv2 .fillPoly (mask , [segmentation ], color )
198
- # cv2.rectangle(mask, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), color, 2)
199
- # cv2.putText(mask, self.label_list[label], (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
200
-
210
+ cv2 .rectangle (mask , (bbox [0 ], bbox [1 ]), (bbox [0 ]+ bbox [2 ], bbox [1 ]+ bbox [3 ]), color , 2 )
201
211
if self .include_instance :
202
- color = random .choice (random_colors_palette .tolist ())
203
- cv2 .fillPoly (instance_image , [segmentation ], color )
204
- # cv2.rectangle(instance_image, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), color, 2)
205
- # cv2.putText(instance_image, self.label_list[label], (bbox[0], bbox[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
212
+ # bbox = annotation['bbox']
213
+ cv2 .rectangle (instance_image , (bbox [0 ], bbox [1 ]), (bbox [0 ]+ bbox [2 ], bbox [1 ]+ bbox [3 ]), instance_color , 2 )
214
+
206
215
if self .include_img :
207
216
# cv2.fillPoly(image, [segmentation], (0, 0, 255))
208
217
# cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (0, 0, 255), 2)
@@ -453,14 +462,22 @@ def _generate_masks_and_images(self, annotation_data, image_info):
453
462
instance_image = np .zeros ((image_info ["height" ], image_info ["width" ],3 ), dtype = np .uint8 )
454
463
455
464
for annotation in annotation_data ['annotations' ]:
456
- segmentation = np .array (annotation ['segmentation' ]).reshape (- 1 , 2 )
457
- segmentation = segmentation .astype (np .int32 )
465
+ color = random_colors_palette [annotation ["category_id" ]].tolist ()
466
+ instance_color = random .choice (random_colors_palette .tolist ())
467
+ for poly in annotation ['segmentation' ]:
468
+ segmentation = np .array (poly ).reshape (- 1 , 2 )
469
+ segmentation = segmentation .astype (np .int32 )
470
+ if self .include_mask :
471
+ cv2 .fillPoly (mask , [segmentation ], color )
472
+ if self .include_instance :
473
+ cv2 .fillPoly (instance_image , [segmentation ], instance_color )
474
+ bbox = annotation ['bbox' ]
458
475
if self .include_mask :
459
- color = random_colors_palette [annotation ["category_id" ]].tolist ()
460
- cv2 .fillPoly (mask , [segmentation ], 255 )
476
+ cv2 .rectangle (mask , (bbox [0 ], bbox [1 ]), (bbox [0 ]+ bbox [2 ], bbox [1 ]+ bbox [3 ]), color , 2 )
461
477
if self .include_instance :
462
- color = random .choice (random_colors_palette .tolist ())
463
- cv2 .fillPoly (instance_image , [segmentation ], color )
478
+ # bbox = annotation['bbox']
479
+ cv2 .rectangle (instance_image , (bbox [0 ], bbox [1 ]), (bbox [0 ]+ bbox [2 ], bbox [1 ]+ bbox [3 ]), instance_color , 2 )
480
+
464
481
465
482
if self .include_img :
466
483
image = cv2 .addWeighted (image , 0.5 , mask , 0.5 , 0 )
0 commit comments