import os import xml.etree.ElementTree as ET dirpath = r'F:\ljwdata\data\Gabiondata\anno' # 原来存放xml文件的目录 newdir = r'F:\ljwdata\data\Gabiondata\anno_txt' # 修改label后形成的txt目录 if not os.path.exists(newdir): os.makedirs(newdir) for fp in os.listdir(dirpath): root = ET.parse(os.path.join(dirpath, fp)).getroot() xmin, ymin, xmax, ymax = 0, 0, 0, 0 sz = root.find('size') width = float(sz[0].text) height = float(sz[1].text) filename = root.find('filename').text print(fp) with open(os.path.join(newdir, fp.split('.')[0] + '.txt'), 'a+') as f: for child in root.findall('object'): # 找到图片中的所有框 sub = child.find('bndbox') # 找到框的标注值并进行读取 sub_label = child.find('name') xmin = float(sub[0].text) ymin = float(sub[1].text) xmax = float(sub[2].text)
将xml文件转为txt文件并将多个txt文件合并成一个txt文件
最新推荐文章于 2024-02-07 13:13:14 发布