0

I'm using this code to create csv file:

def xml_to_csv(path):
  classes_names = []
  xml_list = []

  for xml_file in glob.glob(path + '/*.xml'):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    for member in root.findall('object'):
      classes_names.append(member[0].text)
      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) 
  classes_names = list(set(classes_names))
  classes_names.sort()
  return xml_df, classes_names

for label_path in ['train_labels', 'test_labels']:
  image_path = os.path.join(os.getcwd(), label_path)
  xml_df, classes = xml_to_csv(label_path)
  xml_df.to_csv(f'{label_path}.csv', index=None)
  print(f'Successfully converted {label_path} xml to csv.')

label_map_path = os.path.join("label_map.pbtxt")
pbtxt_content = ""

for i, class_name in enumerate(classes):
    pbtxt_content = (
        pbtxt_content
        + "item {{\n    id: {0}\n    name: '{1}'\n}}\n\n".format(i + 1, class_name)
    )
pbtxt_content = pbtxt_content.strip()
with open(label_map_path, "w") as f:
    f.write(pbtxt_content)
    print('Successfully created label_map.pbtxt ')   

When I use this code I'm get this error:

ParseError: junk after document element: line 3, column 0

My xml files starts like this:

<?xml version="1.0" encoding="UTF-8"?>
<version>5.1.1</version>
<flags></flags>
<shapes>
    <array>
        <label>implant</label>
        <points>
            <array>400.18181818181813</array>
            <array>246.72727272727272</array>
        </points>
        <points>
            <array>389.6363636363636</array>
            <array>259.8181818181818</array>
        </points>
        <points>
            <array>390.0</array>
            <array>264.54545454545456</array>
        </points>

I do not see any junk in line 3, column 0? I assume there must be another reason for the error. When I labeled my images the data is stored as json file so I used an online converter to change it to xml file, here is the link to that online converter https://www.oxygenxml.com/xml_editor/xml_json_converter.html

I'm doing all this in google colab, Please help me with a detailed explanation, Thank you

0 Answers0