-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodify_xml.py
executable file
·108 lines (78 loc) · 3.18 KB
/
modify_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
# coding: utf-8
__author__ = "Margherita Di Leo"
__license__ = "GPL v.3"
__version__ = "0.1"
__email__ = "[email protected]"
import xml.etree.ElementTree as ET
import os
#FILENAME = ".."
# Source folder
FILEPATH = '/run/media/madi/TOSHIBA EXT/DMC/jpg_subset/' + os.sep
# Destination folder
DESTPATH = '/run/media/madi/TOSHIBA EXT/DMC/jpg_coarser/' + os.sep
# Original resolution
RES_ORIGINAL = 0.30
# Current resolution
RES_CURRENT = 0.60
DMC = True # Boolean. DMC files have been renamed. So if DMC, the xml
# file must be edited to reflect the name change. For other data, put False
#-----------------------------------------------------------------------
def CreateFileList(FILEPATH):
for filename in os.listdir(str(FILEPATH)):
if filename.endswith(".xml") and "aux" not in filename:
fileList.append(filename)
fileList.sort()
print fileList
return fileList
#-----------------------------------------------------------------------
def updateXml(FILENAME, FILEPATH, DESTPATH, DMC):
tree = ET.parse(FILEPATH + FILENAME)
root = tree.getroot()
if DMC == True:
orig_name = str(root.find("./filename").text)
firstPart, secondPart = orig_name.split("_")
curr_name = firstPart + "_dmc_" + secondPart
root.find("./filename").text = curr_name
root.find("./filename").set('updated', 'yes')
# Update size of current image
orig_img_width = int(root.find("./size/width").text)
curr_img_width = int(round(orig_img_width / factor))
root.find("./size/width").text = str(curr_img_width)
root.find("./size/width").set('updated', 'yes')
orig_img_height = int(root.find("./size/height").text)
curr_img_height = int(round(orig_img_height / factor))
root.find("./size/height").text = str(curr_img_height)
root.find("./size/height").set('updated', 'yes')
# Update size of each object
for obj in root.findall("./object/bndbox/xmin"):
orig_xmin = int(obj.text)
curr_xmin = int(round(orig_xmin / factor))
obj.text = str(curr_xmin)
obj.set('updated', 'yes')
for obj in root.findall("./object/bndbox/ymin"):
orig_ymin = int(obj.text)
curr_ymin = int(round(orig_ymin / factor))
obj.text = str(curr_ymin)
obj.set('updated', 'yes')
for obj in root.findall("./object/bndbox/xmax"):
orig_xmax = int(obj.text)
curr_xmax = int(round(orig_xmax / factor))
obj.text = str(curr_xmax)
obj.set('updated', 'yes')
for obj in root.findall("./object/bndbox/ymax"):
orig_ymax = int(obj.text)
curr_ymax = int(round(orig_ymax / factor))
obj.text = str(curr_ymax)
obj.set('updated', 'yes')
os.chdir(DESTPATH)
tree.write(FILENAME)
#-----------------------------------------------------------------------
if __name__ == "__main__":
factor = RES_CURRENT / RES_ORIGINAL
os.chdir(FILEPATH)
# Loop over files in source folders
fileList = []
fileList = CreateFileList(FILEPATH)
for filename in fileList:
updateXml(filename, FILEPATH, DESTPATH, DMC)
print "Updated xml files saved in ", DESTPATH