Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/tools/Xml2Txt.py
Views: 475
import xml.etree.ElementTree as ET1from DOTA_devkit import dota_utils as util2import os3import shutil45def DroneVehicle2DOTA(xmlpath, txtpath, extractclassname, specified_class):6"""7Trans DroneVehicle xml farmat to DOTA labels format8Args:9xmlpath: the path of xml with DroneVehicle2DOTA format10txtpath: the path of txt with DOTA format11extractclassname: the category12specified_class: only output single category13"""14if os.path.exists(txtpath):15shutil.rmtree(txtpath) # delete output folder16os.makedirs(txtpath) # make new output folder17filelist = util.GetFileFromThisRootDir(xmlpath) # fileist=['/.../0001.xml', ...]18for xmlfile in filelist: # fullname='/.../0001.xml'19name = os.path.splitext(os.path.basename(xmlfile))[0] # name='0001'20out_file = open(os.path.join(txtpath, name + '.txt'), 'w')21tree = ET.parse(xmlfile)22root = tree.getroot()23for obj in root.iter('object'):24cls = obj.find('name')25if cls == None:26continue27cls = cls.text28diffcult = obj.find('difficult')29diffcult = int(diffcult.text) if diffcult != None else 03031if diffcult < 2:32# cls = cls.replace(' ', '_')33cls = specified_class34polygon = obj.find('polygon')35if polygon == None:36continue37polygon = [int(polygon.find(x).text) for x in ('x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4')]38out_file.write(" ".join([str(a) for a in (*polygon, cls, diffcult)]) + '\n')39else:40print(f'{cls} is not in the extractclassname or diffcult is {diffcult}')4142if __name__ == "__main__":43xmlpath = ['/media/test/DroneVehicle/val/raw/vallabel',44'/media/test/DroneVehicle/val/rays/vallabelr',45'/media/test/DroneVehicle/train/raw/trainlabel',46'/media/test/DroneVehicle/train/rays/trainlabelr']4748txtpath = ['/media/test/DroneVehicle/val/raw/vallabel_txt',49'/media/test/DroneVehicle/val/rays/vallabelr_txt',50'/media/test/DroneVehicle/train/raw/trainlabel_txt',51'/media/test/DroneVehicle/train/rays/trainlabelr_txt']52extractclassname = {'car', 'truck', 'feright_car', 'feright car', 'bus', 'van'}53specified_class = 'vehicle'54for (xml, txt) in zip(xmlpath, txtpath):55print(f"{xml} is converting to {txt}")56DroneVehicle2DOTA(xml, txt, extractclassname, specified_class)5758