Created
April 27, 2020 14:17
-
-
Save yptheangel/6eb142b48f9a081bb63b32ff948297ff to your computer and use it in GitHub Desktop.
xmltotxt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script referenced from https://github.com/Cartucho/mAP/tree/master/scripts/extra | |
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!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment