forked from HouchangX-AI/Helmet-Detection
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_gt_xml.py
41 lines (37 loc) · 1.46 KB
/
convert_gt_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
import sys
import os
import glob
import xml.etree.ElementTree as ET
# make sure that the cwd() in the beginning is the location of the python script (so that every path makes sense)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# change directory to the one with the files to be changed
parent_path = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
parent_path = os.path.abspath(os.path.join(parent_path, os.pardir))
GT_PATH = os.path.join(parent_path, 'input','ground-truth')
#print(GT_PATH)
os.chdir(GT_PATH)
# old files (xml format) will be moved to a "backup" folder
## create the backup dir if it doesn't exist already
if not os.path.exists("backup"):
os.makedirs("backup")
# create VOC format files
xml_list = glob.glob('*.xml')
if len(xml_list) == 0:
print("Error: no .xml files found in ground-truth")
sys.exit()
for tmp_file in xml_list:
#print(tmp_file)
# 1. create new file (VOC format)
with open(tmp_file.replace(".xml", ".txt"), "a") as new_f:
root = ET.parse(tmp_file).getroot()
for obj in root.findall('object'):
obj_name = obj.find('name').text
bndbox = obj.find('bndbox')
left = bndbox.find('xmin').text
top = bndbox.find('ymin').text
right = bndbox.find('xmax').text
bottom = bndbox.find('ymax').text
new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom))
# 2. move old file (xml format) to backup
os.rename(tmp_file, os.path.join("backup", tmp_file))
print("Conversion completed!")