Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

IndexError: child index out of range in parsing xml for object detection

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(), 'images/{}'.format(directory))
        xml_df = xml_to_csv(image_path)
        xml_df.to_csv('data/{}_labels.csv'.format(directory), index=None)
        print('Successfully converted xml to csv.')


main()
Comment

PREVIOUS NEXT
Code Example
Python :: list of thing same condition 
Python :: pandas difference of consecutive values 
Python :: Encapsulation in Python using public members 
Python :: python 2.0 
Python :: dbscan clustering of latitudes and longitudes 
Python :: accessing location of a csv cell in python 
Python :: django column to have duplicate of other 
Python :: numpy move columns 
Python :: what is certifi module in python 
Python :: how to discover which index labels are in other 
Python :: lekht valenca poland 
Python :: how to save multiple choices in django site:stackoverflow.com 
Python :: django create view filter options 
Python :: python dijkstra implementation stack 
Python :: is Cross policy an issue with puppeteer / headless chrome? 
Python :: python excel zelle schreiben 
Python :: extract label from tf data 
Python :: inspect last 5 rows of dataframe 
Python :: map dataframe parallel 
Python :: how to create fibonacci sequence in python 
Python :: EMAIL_BACKEND where to read 
Python :: get all methods of an instance 
Python :: sum of values with none 
Python :: invalid literal for int() with base 10 python 
Python :: how to print multiple lines in one line python 
Python :: download python for windows 7 32-bit 
Python :: Solve abstract model relations conflicts while using inheritance 
Python :: # to check if the list is empty use len(l) or not 
Python :: pairplot legend position 
Python :: zip list python first element 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =