0

I've a CSV as given below. I'm trying to convert it to CSV and then from CSV to tfrecord for training a model in TensorFlow.

<annotation>
    <folder>imgs</folder>
    <filename>steve_jobs.jpg</filename>
    <path>C:/Users/kulkaa/PythonProjects/tensor_android/imgs/steve_jobs.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>183</width>
        <height>276</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>steve_jobs</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>8</xmin>
            <ymin>13</ymin>
            <xmax>178</xmax>
            <ymax>227</ymax>
        </bndbox>
    </object>
</annotation>  

I wrote a python program to convert that XML to CSV. I even created an empty directory named as data.

import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET


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():
    for directory in ['train','test']:
        image_path = os.path.join(os.getcwd(), 'imgs/{}'.format(directory))
        if image_path is None:
            print('Error!')
        xml_df = xml_to_csv(image_path)
        #Storing the csv file into the data directory.
        xml_df.to_csv('data/{}.csv'.format(directory), index=None)
        print('Successfully converted xml to csv.')

main()  

When I run it in Spyder, it gets compiled successfully, but converted CSV is always empty as shown in below image...
CSV
How can I fix it?

2
  • anyone here who can answer this question? Commented Jan 24, 2019 at 5:41
  • sometimes your path of image is different.make sure that you have train & test folder inside images . reserch/object_detection/training/images-train & test folder Commented Jan 29, 2021 at 5:11

1 Answer 1

1

Change your def main function to:

def main():
    for folder in ['train','test']:
        image_path = os.path.join(os.getcwd(), ('images/' + folder))
        xml_df = xml_to_csv(image_path)
        xml_df.to_csv(('images/' + folder + '_labels.csv'), index=None)
        print('Successfully converted xml to csv.')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.