import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
import getopt, sys


def xml_to_csv(path):
    xml_list = []
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     member[0].text,
                     int(member[4][0].text),
                     int(member[4][1].text),
                     int(member[4][2].text),
                     int(member[4][3].text)
                     )
            xml_list.append(value)
    column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
    xml_df = pd.DataFrame(xml_list, columns=column_name)
    return xml_df


def main(xml_path):
    image_path = xml_path #os.path.join(os.getcwd(), 'annotations')
    xml_df = xml_to_csv(image_path)
    xml_df.to_csv(os.path.join(xml_path,'..','speed_labels.csv'), index=None)
    print('Successfully converted xml to csv: \n  Output file: speed_labels.csv')


#comment
# read commandline arguments, first
fullCmdArguments = sys.argv

# - further arguments
argumentList = fullCmdArguments[1:]

unixOptions = "xml"
gnuOptions = ["xml_path"]

# try:
#     arguments, values = getopt.getopt(argumentList, unixOptions, gnuOptions)
# except getopt.error as err:
#     # output error, and return with an error code
#     print (str(err))
#     sys.exit(2)

# for currentArgument, currentValue in arguments:
#     if currentArgument in ("-xml", "--xml_path"):
#         xml_path = currentValue
#         print ("Taken")
#         xml_path = "C:\\Users\\apoca\\Desktop\\Thesis Offline\\KPEC\\speed\\detection_annotations\\xml"
#         main(xml_path)
#     else:
#         print( "Please add an argument containing path to the xml annotations directory \n xml_to_csv.py -xml /path/to/xml_dir")
        
xml_path = "C:\\Users\\apoca\\Desktop\\Thesis Offline\\KPEC\\speed\\detection_annotations\\xml"
main(xml_path)
