-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathxml_to_csv.py
38 lines (32 loc) · 1.46 KB
/
xml_to_csv.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
import os
import argparse
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = []
for file in os.scandir(path):
if file.is_file() and file.name.endswith(('.xml')):
xml_file = os.path.join(path, file.name)
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text) )
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate csv file from xml annotations')
parser.add_argument('--annot_dir', help='directory of input xml files', default='../data/xml_annot')
parser.add_argument('--out_csv_path', help='path to output csv file', default='../data/train.csv')
args = parser.parse_args()
xml_df = xml_to_csv(args.annot_dir)
xml_df.to_csv(args.out_csv_path, index=None)
print('Successfully converted xml to csv.')