HumanSignal / label-studio-converter

Tools for converting Label Studio annotations into common dataset formats
https://labelstud.io/
262 stars 131 forks source link

can you add labelme JSON for annotation format? #217

Open MJWu-wizard opened 1 year ago

makseq commented 1 year ago

Label Studio does not have built-in support for the Labelme JSON format. However, you can convert the Labelme JSON format to the Label Studio JSON format using a custom script. Here's an example of how you can do this:

  1. First, let's assume you have a Labelme JSON file named labelme_annotations.json with the following content:
{
  "shapes": [
    {
      "label": "dog",
      "points": [
        [100, 100],
        [200, 100],
        [200, 200],
        [100, 200]
      ]
    },
    {
      "label": "cat",
      "points": [
        [300, 300],
        [400, 300],
        [400, 400],
        [300, 400]
      ]
    }
  ],
  "imagePath": "example.jpg"
}
  1. Create a Python script named convert_labelme_to_labelstudio.py with the following content:
import json

def convert_labelme_to_labelstudio(labelme_json):
    labelstudio_json = {
        "image": labelme_json["imagePath"],
        "annotations": []
    }

    for shape in labelme_json["shapes"]:
        annotation = {
            "id": len(labelstudio_json["annotations"]) + 1,
            "type": "rectanglelabels",
            "value": {
                "x": shape["points"][0][0],
                "y": shape["points"][0][1],
                "width": shape["points"][1][0] - shape["points"][0][0],
                "height": shape["points"][2][1] - shape["points"][1][1],
                "rectanglelabels": [shape["label"]]
            }
        }
        labelstudio_json["annotations"].append(annotation)

    return labelstudio_json

if __name__ == "__main__":
    with open("labelme_annotations.json", "r") as f:
        labelme_json = json.load(f)

    labelstudio_json = convert_labelme_to_labelstudio(labelme_json)

    with open("labelstudio_annotations.json", "w") as f:
        json.dump(labelstudio_json, f, indent=2)
  1. Run the script to convert the Labelme JSON file to the Label Studio JSON format:
python convert_labelme_to_labelstudio.py
  1. The script will create a new file named labelstudio_annotations.json with the following content:
{
  "image": "example.jpg",
  "annotations": [
    {
      "id": 1,
      "type": "rectanglelabels",
      "value": {
        "x": 100,
        "y": 100,
        "width": 100,
        "height": 100,
        "rectanglelabels": [
          "dog"
        ]
      }
    },
    {
      "id": 2,
      "type": "rectanglelabels",
      "value": {
        "x": 300,
        "y": 300,
        "width": 100,
        "height": 100,
        "rectanglelabels": [
          "cat"
        ]
      }
    }
  ]
}
  1. Now you can import the labelstudio_annotations.json file into Label Studio.

Please note that this example assumes you are working with rectangular bounding boxes. You might need to adjust the script to handle other annotation types supported by Labelme and Label Studio.