Skip to content

Instantly share code, notes, and snippets.

@IMXENON
Last active June 25, 2022 17:14
Show Gist options
  • Save IMXENON/133010545fdc5fe64095ca01d06700ff to your computer and use it in GitHub Desktop.
Save IMXENON/133010545fdc5fe64095ca01d06700ff to your computer and use it in GitHub Desktop.
DOTA to YAML
## Files in input directory should be xml format. Leave a comment here.
# require xmltodict
import xmltodict
import os
class dota2yaml:
# Annotation dataset directory(absolute path needed)
input_path_root = ""
# YAML output dataset directory(anboslute path needed)
output_path_root = ""
def __init__(self, input_path_root, output_path_root):
self.input_path_root = input_path_root + "/"
self.output_path_root = output_path_root + "/"
def convert(self, file_name):
full_input_path = self.input_path_root + file_name
full_output_path = self.output_path_root + file_name.split(".")[0] + ".yaml"
f_i = open(full_input_path,'r')
input_text = f_i.read()
f_i.close()
f_o = open(full_output_path, 'w')
ann = xmltodict.parse(input_text)
objs = ann.get("annotation").get("objects").get("object")
# print(objs)
for obj in objs:
points = obj.get("points").get("point")
label = obj.get("possibleresult").get("name")
res = ""
for point in points:
print(point)
x, y = point.split(",")
res += x + " "
res += y + " "
res += label
res += '\n'
f_o.write(res)
# process
def full_convert(self):
files = os.listdir(self.input_path_root)
for file in files:
self.convert(file)
def execute():
i = "./in"
o = "./out"
instance = dota2yaml(input_path_root = i, output_path_root = o)
instance.full_convert()
if __name__ == "__main__":
execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment