How to Batch Rename Annotations for Your Instance Segmentation Data
utilizing Visual Studio Code
There are cases when you have to change the annotation label from the image data that has already been annotated. Let’s say that we have annotated images with “cat”, and then we want to change that annotation to “kitten”.
Single image
Here’s how to change a single image annotation in Labelme. Right-click on the polygon labels list and then choose “Edit Label”, continue by typing a new name for the annotation.
It’s still doable for 10 images, but what if you already have annotated 10.000 image data? It will be frustrating to change the annotation label one by one.
Visual Studio Code
Because Labelme does not provide a batch rename feature, we will utilize another software which is Visual Studio Code.
So why we use Visual Studio Code?
The annotation output is in JSON (JavaScript Object Notation) format. Which basically is a text file. Visual studio code is a popular code editor for programmers. You can use it to code in Python, Javascript, PHP, and many more including JSON files.
You can get it here if you haven’t:
How to batch rename in VS Code
Open the VS Code and open the folder containing the image data that have been annotated. Make sure all the images are in the same folder. Here’s how the JSON of annotation output looks like.
We can see the text “label”: “cat”. What we are about to do is basically just find and replace this text across all files within the folder.
Press ⌘command+⇧shift+h on the keyboard. It will open the search and replace box on the sidebar. Please note that the VS Code sidebar by default is on the left. In this picture below I have customized mine to the right.
Type the text that we want to replace on the search box. In this case, we can type “label”: “cat”. Then type the new label on the replace box, which is “label”: “kitten”. Finally hit the replace-all button, it’s the little button on the right side of the replace-box. Press ⌘command+⌥option+s to save all the changes that we’ve made. That’s it.
Reopen the image data in Labelme, and we’ll see that all the annotations have been renamed.