forked from Tony607/mmdetection_object_detection_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_xml.py
119 lines (103 loc) · 4.28 KB
/
change_xml.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from __future__ import print_function
from sys import argv
from os import listdir, path
import re
WIDTH_NEW = 800
HEIGHT_NEW = 600
DIMLINE_MASK = r'<(?P<type1>width|height)>(?P<size>\d+)</(?P<type2>width|height)>'
BBLINE_MASK = r'<(?P<type1>xmin|xmax|ymin|ymax)>(?P<size>\d+)</(?P<type2>xmin|xmax|ymin|ymax)>'
NAMELINE_MASK = r'<(?P<type1>filename)>(?P<size>\S+)</(?P<type2>filename)>'
PATHLINE_MASK = r'<(?P<type1>path)>(?P<size>.+)</(?P<type2>path)>'
#regular expression
def resize_file(file_lines):
#change file_lines[8] and file_lines[9] where there is the current width and height
#riga_w = file_lines[8][9:12]
#width_old = int(riga_w)
#print(width_old)
#riga_h = file_lines[9][10:13]
#height_old = int(riga_h)
new_lines = []
for line in file_lines:
match = re.search(DIMLINE_MASK, line) or re.search(BBLINE_MASK, line) or re.search(NAMELINE_MASK, line) or re.search(PATHLINE_MASK, line)
if match is not None:
size = match.group('size')
type1 = match.group('type1')
type2 = match.group('type2')
print(size)
if type1 != type2:
raise ValueError('Malformed line: {}'.format(line))
if type1.startswith('f'):
new_name = size[:-3] + 'jpg'
#print(new_size)
new_line = '\t<{}>{}</{}>\n'.format(type1, new_name, type1)
elif type1.startswith('p'):
new_size = '/u/l/lmeneghe/Laura/mmdetection_object_detection_demo/data/VOC2007/Annotations/' + new_name
#print(new_size)
new_line = '\t<{}>{}</{}>\n'.format(type1, new_size, type1)
elif type1.startswith('x'):
size = int(size)
new_size = int(round(size * WIDTH_NEW / width_old))
#print(new_size)
new_line = '\t\t\t<{}>{}</{}>\n'.format(type1, new_size, type1)
elif type1.startswith('y'):
size = int(size)
new_size = int(round(size * HEIGHT_NEW / height_old))
new_line = '\t\t\t<{}>{}</{}>\n'.format(type1, new_size, type1)
elif type1.startswith('w'):
size = int(size)
width_old = size
new_size = int(WIDTH_NEW)
new_line = '\t\t<{}>{}</{}>\n'.format(type1, new_size, type1)
elif type1.startswith('h'):
size = int(size)
height_old = size
new_size = int(HEIGHT_NEW)
new_line = '\t\t<{}>{}</{}>\n'.format(type1, new_size, type1)
else:
raise ValueError('Unknown type: {}'.format(type1))
#new_line = '\t\t\t<{}>{}</{}>\n'.format(type1, new_size, type1)
new_lines.append(new_line)
#elif match1 is not None:
# name = match.group('name')
# type1 = match.group('type1')
# type2 = match.group('type2')
# print(name)
# if type1 != type2:
# raise ValueError('Malformed line: {}'.format(line))
#
# if type1.startswith('f'):
# if name[-3:end] == 'png':
# print(name[-3:])
else:
new_lines.append(line)
return ''.join(new_lines)
if len(argv) < 2:
raise ValueError('No file submitted')
nome_file = argv[1]
if path.isdir(nome_file):
# Il primo argomento e` una directory
# Implementero` qui qualcosa di astuto per
# gestire questa situazione
files = listdir(nome_file)
for file in files:
file_path = path.join(nome_file, file)
file_name, file_ext = path.splitext(file)
#print(file_path, end='') # Questo non e` tanto astuto
if file_ext.lower() == '.xml':
#print(': CONVERTIMIIII!!!', end='')
with open(file_path,'r') as f:
righe = f.readlines()
nuovo_file = resize_file(righe)
print(nuovo_file)
with open(file_path,'w') as f:
f.write(nuovo_file)
#print()
else:
# Il primo argomento e` un file (spero!)
# Lo convertiro` come merita!
with open(nome_file,'r') as f:
righe = f.readlines()
nuovo_file = resize_file(righe)
print(nuovo_file)
with open(nome_file,'w') as f:
f.write(nuovo_file)