Skip to content

Instantly share code, notes, and snippets.

@ahmedbilal
Last active March 12, 2019 18:54
Show Gist options
  • Save ahmedbilal/de29384868652fe193eb5257e4ae1f94 to your computer and use it in GitHub Desktop.
Save ahmedbilal/de29384868652fe193eb5257e4ae1f94 to your computer and use it in GitHub Desktop.
CVAT XML To TXT - It only works for interpolation mode
import os
import argparse
from math import floor
import xml.etree.ElementTree as ET
# Parse Arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--input", type=str, help="Input XML file", required=True,
dest="input_xml")
arg_parser.add_argument("--output", type=str, help="Output .txt filename", required=True,
dest="output_txt")
arg_parser.add_argument("--frame_dir", type=str, help="Directory where frames exists", required=True,
dest="frame_dir")
arg_parser.add_argument("--frame_format_string", type=str, default="frame%06d.jpg")
args = arg_parser.parse_args()
xml_obj = ET.parse(args.input_xml)
root = xml_obj.getroot()
training_image_path = "frames"
tracks = [child for child in root if child.tag == "track"]
with open(args.output_txt, "w") as output_file:
for track in tracks:
for box in track:
obj = box.attrib
if obj['outside'] == "0":
filename = args.frame_format_string % int(obj['frame'])
filepath = os.path.join(args.frame_dir, filename)
formated_obj = [filepath,
floor(float(obj['xtl'])),
floor(float(obj['ytl'])),
floor(float(obj['xbr'])),
floor(float(obj['ybr'])),
track.attrib['label'].lower()]
formated_obj_str = list(map(str, formated_obj))
str_to_write = ",".join(formated_obj_str) + "\n"
output_file.write(str_to_write)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment