博主在用原始的NWPU VHR-10数据集进行训练的时候遇到这个错误,最后发现是原始数据集中的标签文件的长和宽被统一成了256x256,与实际图片的尺寸不符合就会引发这个错误,将xml文件中的width和height设置为真实值就可以了。代码如下:
代码语言:javascript复制from xml.etree.ElementTree import ElementTree,Element
import os
import cv2
def read_xml(in_path):
tree = ElementTree()
tree.parse(in_path)
return tree
if __name__ == '__main__':
source_pic_root_path = '/home/user8/Desktop/Calculate the size of target/nwpu/JPEGImages/'
source_xml_root_path = '/home/user8/Desktop/Calculate the size of target/nwpu/Annotations/'
for parent, _, files in os.walk(source_pic_root_path):
for file in files:
pic_path = os.path.join(parent, file)
img = cv2.imread(pic_path)
size = img.shape
xml_path = os.path.join(source_xml_root_path, file[:-4] '.xml')
tree = read_xml(xml_path)
root = tree.getroot()
for h_node in root.iter('height'):
height = str(size[0])
h_node.text = height
for w_node in root.iter('width'):
width = str(size[1])
w_node.text = width
tree.write(xml_path)
出现这种错误一般都不是代码的问题。