-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml2txt.py
47 lines (42 loc) · 1.42 KB
/
xml2txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""
根据xml生成txt文件
@author: bai
"""
import sys
import os
import xml.etree.ElementTree as ET
import argparse
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='xml2txt demo')
parser.add_argument('--txt','-T', dest='txt_path', help='txt path to save',
default='None')
parser.add_argument('--Annotations','-A', dest='xml_path', help='xml path to read',
default='None')
args = parser.parse_args()
return args
args=parse_args()
path_to_folder = args.xml_path
if path_to_folder[-1] != '/':
path_to_folder += '/'
# create VOC format files
xml_list = os.listdir(path_to_folder)
if len(xml_list) == 0:
print("Error: no .xml files found in ground-truth")
sys.exit()
for tmp_file in xml_list:
#print(tmp_file)
# 1. create new file (VOC format)
with open(os.path.join(args.txt_path,tmp_file.replace(".xml", ".txt")), "w") as new_f:
xmlpath=os.path.join(args.xml_path,tmp_file)
root = ET.parse(xmlpath).getroot()
for obj in root.findall('object'):
obj_name = obj.find('name').text
bndbox = obj.find('bndbox')
left = bndbox.find('xmin').text
top = bndbox.find('ymin').text
right = bndbox.find('xmax').text
bottom = bndbox.find('ymax').text
new_f.write(obj_name + " " + left + " " + top + " " + right + " " + bottom)
print("Conversion completed!")