-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_bndbox_object_xml.py
33 lines (25 loc) · 1.38 KB
/
update_bndbox_object_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
# -*- coding: utf-8 -*-
"""
@author: AliQadar
"""
import glob as glob
import codecs
from xml.dom import minidom
path=r"path to xml files"
files = glob.glob(path + "/*.xml") # Getting all xml files
for j in range(len(files)):
file_name = files[j].split("\\")[-1] # Extract filename
root = minidom.parse(file_name) # Parsing xml file
xmins = len(root.getElementsByTagName('xmin')) # Checking for number of available items in xmin
if xmins > 0:
for i in range(xmins):
root.getElementsByTagName('xmin')[i].firstChild.data=str(round(int(float(root.getElementsByTagName('xmin')[i].firstChild.data))))
root.getElementsByTagName('xmax')[i].firstChild.data=str(round(int(float(root.getElementsByTagName('xmax')[i].firstChild.data))))
root.getElementsByTagName('ymin')[i].firstChild.data=str(round(int(float(root.getElementsByTagName('ymin')[i].firstChild.data))))
root.getElementsByTagName('ymax')[i].firstChild.data=str(round(int(float(root.getElementsByTagName('ymax')[i].firstChild.data))))
with codecs.open(file_name, "w", encoding="utf-8", errors="xmlcharrefreplace") as xml_file:
root.writexml(xml_file) # writing out updated files
print("updated file {}".format(file_name))
else:
print('corrupt file {} '.format(file_name))
print("updated total {} file".format(len(files)))