NWPU VHR-10目标检测数据集中的ground truth,统一为256x256有时候在使用的时候很不方便,因此需要将宽和高指定为真实的宽和高,python的源代码如下,
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)