Export in YOLO format after SAM annotation

Hi all,
I did some image segmentation annotations with SAM. When I export as YOLO annotations, the annotation files in the labels directory are empty. The only exporting format that keeps some annotations (in RLE format) is JSON.
How do I get the annotations in YOLO?

When you use the Segment Anything Model (SAM) in Label Studio for image segmentation, the annotations are stored as BrushLabels with Run-Length Encoding (RLE). The YOLO export in Label Studio supports only bounding boxes and polygons (from RectangleLabels and PolygonLabels), not brush-based segmentation masks from BrushLabels. This is why, when you try to export your annotations as YOLO format, the label files in the labels directory are empty.

To get your annotations in YOLO format, you’ll need to convert your BrushLabels (segmentation masks) into a format compatible with YOLO. Here are some options:

  1. Convert BrushLabels to PolygonLabels:
  • Process: Extract the contours from your segmentation masks and convert them into polygons.
  • Tools: Use libraries like OpenCV or scikit-image to process the RLE masks and extract polygon coordinates.
  • Export: Once converted to PolygonLabels, you can export your annotations in COCO format. From there, you might be able to convert them into YOLO format using additional scripts or tools.
  1. Generate Bounding Boxes from Segmentation Masks:
  • Process: Calculate the bounding boxes that enclose your segmentation masks.
  • Limitations: This method reduces detailed segmentation information to coarse bounding boxes, which may not be ideal for segmentation tasks but allows compatibility with YOLO format.
  • Export: After converting masks to bounding boxes (RectangleLabels), you can export your annotations directly in YOLO format.
  1. Use Python Scripts to Convert RLE Masks to YOLO Format:
  • Process: Write a script to decode the RLE masks from the JSON export and convert them into YOLO annotations.
  • Steps:
    • Decode the RLE masks to obtain binary mask images.
    • Use image processing to extract polygons or bounding boxes from the masks.
    • Format the extracted data into YOLO annotation files.
  • Considerations: This approach requires programming knowledge and might be time-consuming but offers flexibility.

Please note: Converting segmentation masks to bounding boxes or polygons may result in a loss of detailed segmentation information. YOLO format is primarily designed for object detection with bounding boxes, not for pixel-wise segmentation masks.

Alternative:

  • Use Formats That Support Segmentation Masks: If your end goal is to work with segmentation masks, consider using formats that support them, such as COCO segmentation or exporting the masks as PNG images or NumPy arrays. This might involve adjusting your downstream processes to accept these formats.

Links Used: