"""
Usage:
# Create train data:
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/train -o [PATH_TO_ANNOTATIONS_FOLDER]/train_labels.csv

# Create test data:
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/test -o [PATH_TO_ANNOTATIONS_FOLDER]/test_labels.csv
"""

import os
import pandas as pd
import argparse
import xml.etree.ElementTree as ET
import os.path as osp


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """

    xml_list = []
    f = open(path, "r")
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line: break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file): file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult': idx = 5
            else: idx = 4
            value = (file_name,
                    int(root.find('size')[0].text),
                    int(root.find('size')[1].text),
                    member[0].text,
                    int(member[idx][0].text),
                    int(member[idx][1].text),
                    int(member[idx][2].text),
                    int(member[idx][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)
    f.close()
    return xml_df


def main():
    # Initiate argument parser
    parser = argparse.ArgumentParser(
        description="Sample TensorFlow XML-to-CSV converter")
    parser.add_argument("-i",
                        "--image_set",
                        default='train.txt',
                        help="Path to the .txt set file are stored",
                        type=str)
    parser.add_argument("-o",
                        "--output_csv",
                        help="Name of output .csv file (including path)",
                        type=str)
    args = parser.parse_args()

    assert osp.exists(args.image_set), 'Invalid image set path'
    if args.output_csv is None:
        csv_name = osp.basename(args.image_set) + '_labels.csv'
    else: csv_name = args.output_csv
    xml_df = xml_to_csv(args.image_set)
    xml_df.to_csv(csv_name, index=None)
    print('Successfully converted xml to csv.')


if __name__ == '__main__':
    main()
99a978e3fc55abf21d3159aa206a897d2244d7fb
<mask token>


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """
    xml_list = []
    f = open(path, 'r')
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line:
            break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file):
            file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult':
                idx = 5
            else:
                idx = 4
            value = file_name, int(root.find('size')[0].text), int(root.
                find('size')[1].text), member[0].text, int(member[idx][0].text
                ), int(member[idx][1].text), int(member[idx][2].text), int(
                member[idx][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)
    f.close()
    return xml_df


<mask token>

<mask token>


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """
    xml_list = []
    f = open(path, 'r')
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line:
            break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file):
            file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult':
                idx = 5
            else:
                idx = 4
            value = file_name, int(root.find('size')[0].text), int(root.
                find('size')[1].text), member[0].text, int(member[idx][0].text
                ), int(member[idx][1].text), int(member[idx][2].text), int(
                member[idx][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)
    f.close()
    return xml_df


def main():
    parser = argparse.ArgumentParser(description=
        'Sample TensorFlow XML-to-CSV converter')
    parser.add_argument('-i', '--image_set', default='train.txt', help=
        'Path to the .txt set file are stored', type=str)
    parser.add_argument('-o', '--output_csv', help=
        'Name of output .csv file (including path)', type=str)
    args = parser.parse_args()
    assert osp.exists(args.image_set), 'Invalid image set path'
    if args.output_csv is None:
        csv_name = osp.basename(args.image_set) + '_labels.csv'
    else:
        csv_name = args.output_csv
    xml_df = xml_to_csv(args.image_set)
    xml_df.to_csv(csv_name, index=None)
    print('Successfully converted xml to csv.')


<mask token>

<mask token>


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """
    xml_list = []
    f = open(path, 'r')
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line:
            break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file):
            file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult':
                idx = 5
            else:
                idx = 4
            value = file_name, int(root.find('size')[0].text), int(root.
                find('size')[1].text), member[0].text, int(member[idx][0].text
                ), int(member[idx][1].text), int(member[idx][2].text), int(
                member[idx][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)
    f.close()
    return xml_df


def main():
    parser = argparse.ArgumentParser(description=
        'Sample TensorFlow XML-to-CSV converter')
    parser.add_argument('-i', '--image_set', default='train.txt', help=
        'Path to the .txt set file are stored', type=str)
    parser.add_argument('-o', '--output_csv', help=
        'Name of output .csv file (including path)', type=str)
    args = parser.parse_args()
    assert osp.exists(args.image_set), 'Invalid image set path'
    if args.output_csv is None:
        csv_name = osp.basename(args.image_set) + '_labels.csv'
    else:
        csv_name = args.output_csv
    xml_df = xml_to_csv(args.image_set)
    xml_df.to_csv(csv_name, index=None)
    print('Successfully converted xml to csv.')


if __name__ == '__main__':
    main()

<mask token>
import os
import pandas as pd
import argparse
import xml.etree.ElementTree as ET
import os.path as osp


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """
    xml_list = []
    f = open(path, 'r')
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line:
            break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file):
            file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult':
                idx = 5
            else:
                idx = 4
            value = file_name, int(root.find('size')[0].text), int(root.
                find('size')[1].text), member[0].text, int(member[idx][0].text
                ), int(member[idx][1].text), int(member[idx][2].text), int(
                member[idx][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)
    f.close()
    return xml_df


def main():
    parser = argparse.ArgumentParser(description=
        'Sample TensorFlow XML-to-CSV converter')
    parser.add_argument('-i', '--image_set', default='train.txt', help=
        'Path to the .txt set file are stored', type=str)
    parser.add_argument('-o', '--output_csv', help=
        'Name of output .csv file (including path)', type=str)
    args = parser.parse_args()
    assert osp.exists(args.image_set), 'Invalid image set path'
    if args.output_csv is None:
        csv_name = osp.basename(args.image_set) + '_labels.csv'
    else:
        csv_name = args.output_csv
    xml_df = xml_to_csv(args.image_set)
    xml_df.to_csv(csv_name, index=None)
    print('Successfully converted xml to csv.')


if __name__ == '__main__':
    main()

"""
Usage:
# Create train data:
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/train -o [PATH_TO_ANNOTATIONS_FOLDER]/train_labels.csv

# Create test data:
python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/test -o [PATH_TO_ANNOTATIONS_FOLDER]/test_labels.csv
"""

import os
import pandas as pd
import argparse
import xml.etree.ElementTree as ET
import os.path as osp


def xml_to_csv(path):
    """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.

    Parameters:
    ----------
    path : {str}
        The path containing the .xml files
    Returns
    -------
    Pandas DataFrame
        The produced dataframe
    """

    xml_list = []
    f = open(path, "r")
    basepath = osp.dirname(osp.dirname(osp.dirname(path)))
    while True:
        line = f.readline().strip()
        if not line: break
        xml_file = osp.join(basepath, 'Annotations', line + '.xml')
        jpg_file = osp.join(basepath, 'JPEGImages', line + '.jpg')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        if os.path.isfile(jpg_file): file_name = jpg_file
        else:
            file_name = root.find('filename').text
        for member in root.findall('object'):
            if member[4].tag == 'difficult': idx = 5
            else: idx = 4
            value = (file_name,
                    int(root.find('size')[0].text),
                    int(root.find('size')[1].text),
                    member[0].text,
                    int(member[idx][0].text),
                    int(member[idx][1].text),
                    int(member[idx][2].text),
                    int(member[idx][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)
    f.close()
    return xml_df


def main():
    # Initiate argument parser
    parser = argparse.ArgumentParser(
        description="Sample TensorFlow XML-to-CSV converter")
    parser.add_argument("-i",
                        "--image_set",
                        default='train.txt',
                        help="Path to the .txt set file are stored",
                        type=str)
    parser.add_argument("-o",
                        "--output_csv",
                        help="Name of output .csv file (including path)",
                        type=str)
    args = parser.parse_args()

    assert osp.exists(args.image_set), 'Invalid image set path'
    if args.output_csv is None:
        csv_name = osp.basename(args.image_set) + '_labels.csv'
    else: csv_name = args.output_csv
    xml_df = xml_to_csv(args.image_set)
    xml_df.to_csv(csv_name, index=None)
    print('Successfully converted xml to csv.')


if __name__ == '__main__':
    main()