labelImg icon indicating copy to clipboard operation
labelImg copied to clipboard

Re-Opening the app resets classes.txt

Open bertalanimre opened this issue 5 years ago • 21 comments

I have a folder with (let'a say) 1000 images. About 200 of them was already processed with labelImg to label "tag" and "mini-tag". I close the app, classes.txt have the tag and mini-tag at the end nicely. The next day I open up the folder with "Open Dir", it recognises yesterday's work. When I want to add a new class "like weapon", I add the RectBox, add the class weapon ( At this time, strangely the list doesn't contain tag and mini-tag already ), save my job and what happens? Classes.txt gets erased, and now it looks like the default classes.txt + the class "weapon". tag and mini-tag have disappeared. Every time when I re-open the application I have to add random RectBoxes with the name of the previous classes, save it, then I can only continue my work.

Why does the app resets the classes.txt and does not read it properly when I open the folder containing it?

bertalanimre avatar Jul 08 '19 15:07 bertalanimre

Did you find a solution? facing the same issue

Mahaaveer avatar Jul 11 '19 16:07 Mahaaveer

Sorry, nothing yet. Waiting for the authors reply

bertalanimre avatar Jul 12 '19 07:07 bertalanimre

You can edit the data/predefined_classes.txt to load pre-defined classes

khaitranvan96kt avatar Oct 26 '19 02:10 khaitranvan96kt

Same issue here, unfortunately

dokluch avatar Feb 13 '21 10:02 dokluch

same issue

Niranjan2054 avatar May 27 '21 11:05 Niranjan2054

Same issue

alikaz3mi avatar Nov 19 '21 08:11 alikaz3mi

Seems simple to do:

In labelImg.py, do this:

def change_save_dir_dialog(self, _value=False):

    if self.default_save_dir is not None:
        path = ustr(self.default_save_dir)
    else:
        path = '.'

    dir_path = ustr(QFileDialog.getExistingDirectory(self,
                                                     '%s - Save annotations to the directory' % __appname__, path,  QFileDialog.ShowDirsOnly
                                                     | QFileDialog.DontResolveSymlinks))

    if os.path.exists(dir_path + "/classes.txt"):
        self.label_hist = None
        self.load_predefined_classes(dir_path + "/classes.txt")

    if dir_path is not None and len(dir_path) > 1:
        self.default_save_dir = dir_path

    self.statusBar().showMessage('%s . Annotation will be saved to %s' %
                                 ('Change saved folder', self.default_save_dir))
    self.statusBar().show()

CrucialDrew avatar Nov 27 '21 00:11 CrucialDrew

that gives an error by starting labelimg

@iMac-von-Arif labelImg % python3 labelImg.py File "/usr/local/lib/python3.9/site-packages/labelImg/labelImg.py", line 1283 if os.path.exists(dir_path + "/classes.txt"): IndentationError: unexpected indent arifcalhan@iMac-von-Arif labelImg %

doitauto avatar Nov 27 '21 00:11 doitauto

I'm not sure how to get the indentation to work in a github comment, you'll have to fix the indents.

The only change should be:

if os.path.exists(dir_path + "/classes.txt"):
    self.label_hist = None
    self.load_predefined_classes(dir_path + "/classes.txt")
    

And then keep in mind you have to do the "change save dir" before it will load the classes, also that the label_hist = None line removes the predefined (or existing classes), so remove that line if you want to keep them

CrucialDrew avatar Nov 27 '21 00:11 CrucialDrew

i get again same error:

iMac-von-Arif labelImg % python3 labelImg.py                       
  File "/usr/local/lib/python3.9/site-packages/labelImg/labelImg.py", line 1286
    if dir_path is not None and len(dir_path) > 1:
IndentationError: unexpected indent
arifcalhan@iMac-von-Arif labelImg % 
Bildschirmfoto 2021-11-27 um 01 28 29

doitauto avatar Nov 27 '21 00:11 doitauto

Yeah, well, your indentation is bad. Slide it all over so:

if os.path.exists(dir_path + "/classes.txt"):

lines up with dir_path (and the two lines below it are to the right of that) Screenshot from 2021-11-26 17-37-43

CrucialDrew avatar Nov 27 '21 00:11 CrucialDrew

thank you now it is working 👍

doitauto avatar Nov 27 '21 10:11 doitauto

Yeah, well, your indentation is bad. Slide it all over so:

if os.path.exists(dir_path + "/classes.txt"):

lines up with dir_path (and the two lines below it are to the right of that) Screenshot from 2021-11-26 17-37-43

Thank you so much, waste my 2-day work because of this. I wonder why this bug is still not fixed yet, since I move from labelMe (great tool with no bug) to use YOLO.

nqa112 avatar Jan 19 '22 14:01 nqa112

I had the same issue. Thanks for the solution!

christianepedersen avatar Feb 24 '22 19:02 christianepedersen

My solution:

  1. create predefined_classes.txt before labeling any images
  2. run command: labelImg $ImageFolder $PredefinedClassesTxtPath.

For example, if you created predefined_classes.txt under the images folder (step 1), you can just cd to the images folder, and run: labelImg . predefined_classes.txt

crclz avatar Apr 17 '22 06:04 crclz

@CrucialDrew @doitauto

I copied the code and paste it into "labelimg.py" but I am still facing the same error

image

I added print ("test") just to see if the program reaches that part of the code and it didn't print the word "test" at all

AbdulAzizHamka avatar Jul 05 '22 09:07 AbdulAzizHamka

This did not solve my issue

Ching87 avatar Jul 17 '22 09:07 Ching87

I'm not sure how to get the indentation to work in a github comment, you'll have to fix the indents.

The only change should be:

if os.path.exists(dir_path + "/classes.txt"):
    self.label_hist = None
    self.load_predefined_classes(dir_path + "/classes.txt")

And then keep in mind you have to do the "change save dir" before it will load the classes, also that the label_hist = None line removes the predefined (or existing classes), so remove that line if you want to keep them

Thank you so much for the clarification. I was plagued with this issue for almost an year and I just found the fix, it's working flawlessly.

amaalanoosucs avatar Aug 14 '23 07:08 amaalanoosucs

recap locate the file labelImg.py Line 1241: def change_save_dir_dialog add the following two lines after dir_path

        if os.path.exists(dir_path + "/classes.txt"):
            self.load_predefined_classes(dir_path + "/classes.txt")

The whole function will look like this:

def change_save_dir_dialog(self, _value=False):
        if self.default_save_dir is not None:
            path = ustr(self.default_save_dir)
        else:
            path = '.'

        dir_path = ustr(QFileDialog.getExistingDirectory(self,
                                                         '%s - Save annotations to the directory' % __appname__, path,  QFileDialog.ShowDirsOnly
                                                         | QFileDialog.DontResolveSymlinks))
        if os.path.exists(dir_path + "/classes.txt"):
            self.load_predefined_classes(dir_path + "/classes.txt")
        
        if dir_path is not None and len(dir_path) > 1:
            self.default_save_dir = dir_path

        self.statusBar().showMessage('%s . Annotation will be saved to %s' %
                                     ('Change saved folder', self.default_save_dir))
        self.statusBar().show()

Safouane avatar Oct 16 '23 21:10 Safouane

I ran labelImg with arguments as in python file and it worked without any code modification. labelImg <image_dir> <class_file> <save_dir>

daivuongktx13 avatar Dec 21 '23 02:12 daivuongktx13

def change_save_dir_dialog(self, _value=False): if self.default_save_dir is not None: path = ustr(self.default_save_dir) else: path = '.'

    dir_path = ustr(QFileDialog.getExistingDirectory(self,
                                                     '%s - Save annotations to the directory' % __appname__, path,  QFileDialog.ShowDirsOnly
                                                     | QFileDialog.DontResolveSymlinks))
    
    if os.path.exists(dir_path + "/classes.txt"):
        self.load_predefined_classes(dir_path + "/classes.txt")
        
    if dir_path is not None and len(dir_path) > 1:
        self.default_save_dir = dir_pathw

i wrote this lines in my code ,there is no error showing but labelimg open and after image selected from folder image opened but when i press w to create rect box labelimg dirrectly closed. why this is happening? and what should i do now

mrunalinipatil1996 avatar Feb 23 '24 07:02 mrunalinipatil1996