憨憨程序員,其實是有做讀取舊classes保存到新classes功能的,但是看完代碼發(fā)現就啟動程序初始化的時候調用了一次,change save dir的時候根本沒有調用。
我實力有限,只能靠比較愚蠢的方法解決了。
首先找到我們安裝labelImg的地址,比如我就是放到conda環(huán)境里面,所以在這個目錄下。不同安裝會有不同目錄所以只能做參考。
如上圖,進入環(huán)境envs文件夾后,選擇你安裝labelImg的環(huán)境,進入Lib/site-packages/labelImg文件夾。
用IDE啥的,打開labelImg.py
翻到1230行(版本1.8.5),
def change_save_dir_dialog(self, _value=False):
這個函數里面,改成這個樣子:
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 dir_path is not None and len(dir_path) > 1: self.default_save_dir = dir_path temp0=(self.default_save_dir+"/_classes.txt") with open(self.default_save_dir+"/classes.txt","r",encoding='mbcs') as tempfile0: allinclasses=tempfile0.readlines() with open(temp0,"w",encoding="utf-8")as _tempfile0: for line in allinclasses: _tempfile0.write(line) # print(temp0,type(temp0)) self.load_predefined_classes(temp0)# outway!! self.statusBar().showMessage('%s . Annotation will be saved to %s' % ('Change saved folder', self.default_save_dir)) self.statusBar().show()
具體實現方式就是,把打開文件調用的那個函數拿過來,然后在改變輸出文件夾的時候再調用一遍。
為什么會這么復雜呢?原因就是很多人可能會出現:
UnicodeDecodeError: ‘gbk’ codec can’t decode byte xxxxx………………
這種情況。因為默認classes的編碼格式是ANSI,但是正常寫txt文件用的是utf-8。所以classes文件必須是ansi才行。
但是啊但是,這個憨憨函數調用,要求輸入的是utf-8的txt文件。于是我直接就創(chuàng)建一個_classes文件,用utf-8的格式,然后再把classes里面的東西全部搬進來,很原始的方法,但是很有效。
需要注意的是,我的水平有限,它的魯棒性并沒有那么高,所以,只有在點擊change_save_dir按鈕以后,它才會識別classes文件。如果是直接點open dir自動的標簽,還是會出錯的。文章來源:http://www.zghlxwxcb.cn/news/detail-856840.html
因此,強烈建議復制一個classes.txt作為備份,原因是windows創(chuàng)建ansi格式的txt文件并不怎么方便,自己創(chuàng)建的classes.txt是默認utf-8的。文章來源地址http://www.zghlxwxcb.cn/news/detail-856840.html
到了這里,關于labelImg無法保存classes文件的解決方法(IndexError: list index out of range)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!