forked from mmabadal/object_detection_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_to_txt.py
executable file
·73 lines (47 loc) · 1.76 KB
/
xml_to_txt.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import re
import glob
import sys
import argparse
import pandas as pd
import xml.etree.ElementTree as ET
'''
call:
python xml_to_txt.py --path_in a/b --path_out c/d
'''
def xml_to_txt(xml_file):
values = list()
tree = ET.parse(xml_file)
root = tree.getroot()
for instance in root.findall('object'):
if instance[0].text != "halimeda":
print("salgo con: " + str(instance[0].text))
value = (str(instance[0].text), # class
str(instance[4][0].text), # left
str(instance[4][1].text), # top
str(instance[4][2].text), # right
str(instance[4][3].text)) # bottom
values.append(value)
return values
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--path_in', help='xml input directory.')
parser.add_argument('--path_out', help='txt output directory.')
parsed_args = parser.parse_args(sys.argv[1:])
dir_in = parsed_args.path_in
dir_out = parsed_args.path_out
for file in os.listdir(dir_in):
if re.search("\.(xml)$", file): # if the file is an image
image_path = os.path.join(dir_in, file)
instances = xml_to_txt(image_path)
name_out = os.path.splitext(file)[0] + ".txt"
file_out = os.path.join(dir_out, name_out)
with open(file_out, 'w') as f:
for instance in instances:
f.write(instance[0] + " " +
str(instance[1]) + " " +
str(instance[2]) + " " +
str(instance[3]) + " " +
str(instance[4]) + "\n")
print('Successfully converted xml to txt.')
main()