The use of Labelme segmentation labels (very good)

The blog is transferred from: Use of Labelme segmentation and labeling software

1. Use of Labelme

Here I suggest that you prepare the data in advance according to the directory format I provided, and then start labelme in the root directory ( 注意start directory location, because the path of the image stored in the label json file is relative to this directory)

├── img_data: 存放你要标注的所有图片
├── data_annotated: 存放后续标注好的所有json文件
└── label.txt: 所有类别信息

1.1 Create label label file

Although it is possible to add labels when labeling in labelme, I personally strongly recommend creating a label.txt label in advance (placed in the above location), and then read it directly when starting labelme. The label format is as follows:

__ignore__
_background_
dog
cat

Each line represents the name of a type, and the first two lines are fixed format __ignore__ and _background_, otherwise an error will be reported when using the conversion script provided by the author (converting to PASCAL VOC format and MS COCO format). That is, starting from the third line is the target category we need to segment. Here we take the segmentation of cats and dogs as an example.

1.2 start labelme

After creating the label, start labelme and read the label file (note the startup root directory), which --labelsspecifies the path of the label file

labelme --labels label.txt

After reading the label, we can see on the right side of the interface that Label Listthe label file we just created has been loaded, and different categories are 不同的颜色represented by .
insert image description here

1.3 Open file/folder

Click Open or OpenDir on the left side of the interface to open the file or folder, and here select the img_data we just mentioned (this folder stores all the pictures that need to be marked later):
insert image description here

1.4 Set the save result path

Develop a good habit, first set the save path.

First click File in the upper left corner Change Output Dirto set the storage directory for the annotation results, and here it is set to data_annotated as mentioned above.
The suggestion will Save With Image Databe unchecked, it is selected by default. If selected, it will also be saved in the .json file in the saved annotation results 图像数据(I personally think it is unnecessary, and it still takes up space).
insert image description here

1.5 Labeling Targets

First click the button on the left CreatePolygonsto start drawing a polygon, and then use the mouse to mark points one by one to mark the target boundary (place the mouse on the first point, and click it to automatically close the boundary). After marking, a selection box for selecting a category will pop up, just select the corresponding category.
If you want to modify the target boundary after marking a target, you can click EditPolygonsthe button on the left side of the tool, then select the target to be modified, and drag the boundary point to make fine adjustments. If you want to add a point on the boundary, put the mouse on the boundary and click the right mouse button to select Add Point to Edgeto add a boundary point. If you want to delete a point, put the mouse on the boundary point and click the right mouse button to select Remove Selected Pointto delete the boundary point.

insert image description here
You can also directly press the reverse button on the picture to select other annotation graphics, besides Polygons, there are rectangles such as Retangle, Circle, Point, etc.
insert image description here
After marking a picture, click the Save button on the left side of the interface to save the marking result. By default, the marking information of each picture is stored in a json file.

1.6 Save the json file format

The format of the json file obtained by labeling is as follows. The coordinates of all objects in a picture are stored in the shapes list. Each element in the list corresponds to an object, and the label records the category name of the object. points record the left and right coordinate information of a target. Other information will not be repeated. According to the following information, in fact, you can write a script to read the target information.

{
    
    
  "version": "4.5.9",
  "flags": {
    
    },
  "shapes": [
    {
    
    
      "label": "dog",
      "points": [
        [
          108.09090909090907,
          687.1818181818181
        ],
        ....
        [
          538.090909090909,
          668.090909090909
        ],
        [
          534.4545454545454,
          689.0
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {
    
    }
    }
  ],
  "imagePath": "../img_data/1.jpg",
  "imageData": null,
  "imageHeight": 690,
  "imageWidth": 690
}

2 format conversion

2.1 Converting Semantic Segmentation Labels

For convenience, the original author also provided a script to help us easily convert the json file into the semantic segmentation tag format of PASCAL VOC. Example project link: https://github.com/wkentaro/labelme/tree/master/examples/semantic_segmentation.
There is a script in this link labelme2voc.py. After downloading the script, put it in the root directory of the above project and execute the following instructions That’s it (note that the root directory of executing the script must be the same as the root directory of labelme just started, otherwise there will be an error that the image cannot be found). Among them, data_annotated is the json label folder that has just been annotated and saved, and data_dataset_voc is the directory of the output PASCAL VOC` format data.

python labelme2voc.py data_annotated data_dataset_voc --labels label.txt

After execution, the following directory will be generated:

- data_dataset_voc/JPEGImages
- data_dataset_voc/SegmentationClass
- data_dataset_voc/SegmentationClassPNG
- data_dataset_voc/SegmentationClassVisualization
- data_dataset_voc/class_names.txt

Among them, JPEGImages is the same as the previous explanation of PASCAL VOC data , which is to store the original image file. SegmentationClassPNG is the PNG label image that needs to be used for semantic segmentation.
insert image description here

2.2 Converting Instance Segmentation Labels

For convenience, the original author provides two scripts here to help us easily convert the json file into the PASCAL VOC instance segmentation tag format and the MS COCO instance segmentation tag format. Example project link: https://github.com/wkentaro/labelme/tree/master/examples/instance_segmentation .
There is a script in this link labelme2voc.py. After downloading the script, execute the following instructions (note that the execution of the script The root directory must be the same as the root directory that just started labelme, otherwise there will be an error that the image cannot be found). Among them, data_annotated is the json label folder that has just been marked and saved, and data_dataset_voc is the directory where PASCAL VOC data is generated.

python labelme2voc.py data_annotated data_dataset_voc --labels label.txt

After execution, the following directory will be generated:

- data_dataset_voc/JPEGImages
- data_dataset_voc/SegmentationClass
- data_dataset_voc/SegmentationClassPNG
- data_dataset_voc/SegmentationClassVisualization
- data_dataset_voc/SegmentationObject
- data_dataset_voc/SegmentationObjectPNG
- data_dataset_voc/SegmentationObjectVisualization
- data_dataset_voc/class_names.txt

In addition to the semantic segmentation folder just mentioned, label files for instance segmentation are also generated, mainly the SegmentationObjectPNG directory:
insert image description here
there is a script in this link labelme2coco.py, after downloading the script, execute the following instructions (note, execute the script The root directory of the labelme must be the same as the root directory of the labelme just started, otherwise there will be an error that the picture cannot be found). Among them, data_annotated is the json label folder that has just been marked and saved, and data_dataset_coco is the directory that generates the MS COCO data type.

python labelme2coco.py data_annotated data_dataset_coco --labels label.txt

If you are prompted to install the pycocotools package during execution, then just install it with pip.
Linux system directly:

pip install pycocotools

Windows system use:

pip install pycocotools-windows

After execution, the following directory will be generated:

- data_dataset_coco/JPEGImages
- data_dataset_coco/annotations.json

Among them is the tag data file of MS COCO. If you don’t know it, you can read the introduction of MS COCOannotations.json written by the original author.

Guess you like

Origin blog.csdn.net/weixin_38346042/article/details/128462359