import os import gflags import sys import xml.etree.ElementTree as ET from fetch_resource import get_all_img_and_xml_contained_in_root_path Flags = gflags.FLAGS gflags.DEFINE_string( 'input_dir', 'C:/Users/mailb/Desktop/19s_0914/train/sku001/20181229_1316', 'input_dir') gflags.DEFINE_string( 'output_dir', 'C:/Users/mailb/Desktop/19s_0914/train/sku001/txt1/', 'output_dir') def change_xml_sku(input_dir,output_dir): xmls, _, _ = get_all_img_and_xml_contained_in_root_path(input_dir) for xml in xmls: tree = ET.parse(xml) roots = tree.getroot() filename = roots.find('filename') filename = filename.text[:-4] print(filename) for oj in roots.findall('object'): bndboxs = oj.findall('bndbox') for box in bndboxs: xmin = float(box.find('xmin').text) ymin = float(box.find('ymin').text) xmax = float(box.find('xmax').text) ymax = float(box.find('ymax').text) center_x = (xmin + xmax)/2 center_y = (ymin + ymax)/2 width = xmax- xmin height = ymax- ymin dir = output_dir + 'sample_' + filename + '.txt' f = open(dir, "w") f.write(str(center_x) + '\n') f.write(str(center_y) + '\n') f.write(str(width) + '\n') f.write(str(height)) f.close() def main(argv): Flags(argv) input_dir = Flags.input_dir output_dir = Flags.output_dir change_xml_sku(input_dir, output_dir) if __name__ == '__main__': main(sys.argv)
创建txt文件,读取xml变量,并将其写进txt文件
最新推荐文章于 2022-03-15 16:47:04 发布