介绍
在使用开源标注工具roLabelImg对图像数据进行标注时,我们可能会遇到XML文件保存错误。这种错误通常发生在尝试保存修改后的XML文件时,具体错误信息如下:
Traceback (most recent call last):
File "roLabelImg.py", line 1165, in saveFile
self._saveFile(savedPath)
File "roLabelImg.py", line 1193, in _saveFile
if annotationFilePath and self.saveLabels(annotationFilePath):
File "roLabelImg.py", line 773, in saveLabels
self.labelFile.savePascalVocFormat(annotationFilePath, shapes, self.filePath, self.imageData,
File "E:\wyj\project\dam\roLabelImg-master\libs\labelFile.py", line 65, in savePascalVocFormat
writer.save(targetFile=filename)
File "E:\wyj\project\dam\roLabelImg-master\libs\pascal_voc_io.py", line 194, in save
prettifyResult = self.prettify(root)
File "E:\wyj\project\dam\roLabelImg-master\libs\pascal_voc_io.py", line 56, in prettify
root = etree.fromstring(rough_string)
File "E:\Software\miniconda\envs\wyj1\lib\xml\etree\ElementTree.py", line 1325, in XML
parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 2, column 108
问题分析
报错信息显示在解析XML时出现了格式错误。经过对XML文件的多次检查,并未发现明显的问题,这使得问题显得更为复杂。
解决方案
在GitHub的项目页面上GitHub - cgvict/roLabelImg: Label Rotated Rect On Images for training进一步查阅后,发现一个常被忽略的步骤可能是导致此问题的根本原因。在运行roLabelImg.py
之前,需要执行以下命令来生成必要的资源文件:
pyrcc5 -o resources.py resources.qrc
该命令将资源文件(如图标、界面布局等)编译成Python文件,这是roLabelImg正确运行所必需的。如果忽略这一步骤,可能会导致程序无法正确处理XML文件,从而触发上述错误。