-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_bbox.py
54 lines (49 loc) · 2.04 KB
/
change_bbox.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
import os
import re
import glob
import math
dir_ = "annots/"
width = 512
height = width
extension = 'xml'
def replacenth(string, sub, wanted, n):
where = [m.start() for m in re.finditer(sub, string)][n-1]
before = string[:where]
after = string[where:]
after = after.replace(sub, wanted, 1)
return before + after
for filename in glob.glob('input_/*.{}'.format(extension)):
if filename == '.DS_Store':
continue
with open(filename, "rt") as file:
xmlData = file.read()
for w in re.finditer(r"<width>\d+</width>",xmlData):
widthImage = int(re.findall(r"<width>\d+</width>",w.group(0))[0][7:-8])
before = xmlData[:w.start()+7]
after = xmlData[w.end()-8:]
xmlData = before + str(width) + after
for w in re.finditer(r"<height>\d+</height>",xmlData):
heightImage = int(re.findall(r"<height>\d+</height>",w.group(0))[0][8:-9])
before = xmlData[:w.start()+8]
after = xmlData[w.end()-9:]
xmlData = before + str(height) + after
itera = re.finditer("(<xmin>\d+</xmin>|<xmax>\d+</xmax>)",xmlData)
delay = 0
for data in itera:
val = int(re.findall(r"\d+",data.group(0))[0])
newData = math.ceil(val * width / widthImage)
before = xmlData[:data.start()+6-delay]
after = xmlData[data.end()-7-delay:]
delay = delay + len(str(val)) - len(str(newData))
xmlData = before + str(newData) + after
delay = 0
itera = re.finditer("(<ymin>\d+</ymin>|<ymax>\d+</ymax>)",xmlData)
for data in itera:
val = int(re.findall(r"\d+",data.group(0))[0])
newData = math.ceil(val * height / heightImage)
before = xmlData[:data.start()+6-delay]
after = xmlData[data.end()-7-delay:]
delay = delay + len(str(val)) - len(str(newData))
xmlData = before + str(newData) + after
with open(filename, "wt") as file:
file.write(xmlData)