HumanSignal/labelImg

BUG: Editing label of image annotation erases old labels in classes.txt

sohang3112 opened this issue ยท 7 comments

I have a folder of images and annotations, that has this classes.txt:

injectable
pills
smoking

I opened this folder in labelImg and in an image, edited the label of its existing annotation from injectable to pills, and saved changes.
When I reloaded classes.txt, all items except pills had been erased by labelImg:

pills
  • OS: Windows 11
  • PyQt version: 5.15.9
  • Python version: 3.11
YA2JA commented

The reason why this happens is that the image doesn't load the "classes.txt" file and creates a new one instead. If you want to preserve your old file, you need to rename it to "predefined_classes.txt".

Although the project itself is fine, the fact that 30% of users are encountering the same issue is problematic. This highlights the importance of clear documentation.

If you want to preserve your old file, you need to rename it to "predefined_classes.txt".

@YA2JA I tried this, but when I opened the folder again in labelImg, there was a FileNotFoundError as classes.txt file was not present. So I'm not sure how this is useful.

I found another solution - explicitly providing the path to classes.txt works: labelImg /path/to/images_and_labels_folder /path/to/classes.txt.

This works but it shouldn't be required in the first place - ideally labelImg should not overwrite existing classes.txt Also, I'm not sure if there is any way to explicitly specify classes.txt from the GUI.

I'm having this problem too, it's simply a serious functionality defect

@sohang3112 Thanks to your method, but I feel that it is really necessary for the official to add a setting to the GUI to solve this problem

@kiitio I agree, we should be able to do this from the GUI

FWIW (it's not pretty, but it works for me)
I'm exporting to YOLO format.

I've made these edits;
In labelimg/libs/yolo_io.py on line 57, 62 and 91 change path to the root dir of your images folder to prevent labelimg from creating multiple 'classes.txt' files when you're labeling in subfolders.
= "C:/path_to_project_folder/images/classes.txt"

In labelimg/labelimg.py on line 1699 change path to your now static classes.txt file to prevent labelimg from overwriting your labels.
default= "C:/path_to_project_folder/images/classes.txt",

Make sure you delete the original file in labelimg/data

And if you run into this error:
TypeError: expected str, bytes or os.PathLike object, not NoneType
Delete the settings file in your Users folder. (User folder of your OS)
.labelImgSettings.pkl

*You can also increase the zoom-in factor with mouse wheel scrolling in labelimg.py on lines 1048-1051

IMO, this is one of the fastest methods to annotate images with a bounding box.
*Fast scroll, A - D (prev-next), W (bounding box), single class mode, auto save, etc...

I get through images way faster than in label studio because they put binds all the way on the right of the keyboard.

Hopefully that helps someone.

FWIW (it's not pretty, but it works for me) I'm exporting to YOLO format.

I've made these edits; In labelimg/libs/yolo_io.py on line 57, 62 and 91 change path to the root dir of your images folder to prevent labelimg from creating multiple 'classes.txt' files when you're labeling in subfolders. = "C:/path_to_project_folder/images/classes.txt"

In labelimg/labelimg.py on line 1699 change path to your now static classes.txt file to prevent labelimg from overwriting your labels. default= "C:/path_to_project_folder/images/classes.txt",

Make sure you delete the original file in labelimg/data

And if you run into this error: TypeError: expected str, bytes or os.PathLike object, not NoneType Delete the settings file in your Users folder. (User folder of your OS) .labelImgSettings.pkl

*You can also increase the zoom-in factor with mouse wheel scrolling in labelimg.py on lines 1048-1051

IMO, this is one of the fastest methods to annotate images with a bounding box. *Fast scroll, A - D (prev-next), W (bounding box), single class mode, auto save, etc...

I get through images way faster than in label studio because they put binds all the way on the right of the keyboard.

Hopefully that helps someone.

Thanks for your help. I did everything you said but,
when I close and reopen LabelImg, the contents of classes.txt are still being overwritten with new labels. For example, I label my class as 'apple.' I close the program and reopen it. When I label 'orange,' it assigns '0' label for both classes.
How can i fix it ?