zoltasaur
zoltasaur

Reputation: 1

Reading labelimg (bounding box) files into pandas dataframe

Hey all couldn't find a good answer to this but I made a little method that worked for me. hope it helps if anyone else is looking. Its a bit hacky but will serve.

Upvotes: -1

Views: 382

Answers (1)

zoltasaur
zoltasaur

Reputation: 1

```
import pandas as pd
import xml.etree.ElementTree as ET


test_xml = '/Users/cole/PycharmProjects/Birds/VisualizeConvsTest/Robin.xml'

tree = ET.parse(test_xml)
root = tree.getroot()

labelimg_params = {}
#takes an empty dict and adds in the params as we recursively go through the xml file
def find_children(root, params):

    children = root.getchildren()
    for child in children:
        if child.getchildren():
            find_children(child, params)
        params[child.tag] = child.text
        #print(child.tag, child.text)
    return labelimg_params

xml_dict = find_children(root, labelimg_params)
df = pd.DataFrame([xml_dict])
print(df.columns.to_list())
bbox = df[['xmin', 'ymin', 'xmax', 'ymax']]
print(bbox)
```

['folder', 'filename', 'path', 'database', 'source', 'width', 'height', 'depth', 'size', 'segmented', 'name', 'pose', 'truncated', 'difficult', 'xmin', 'ymin', 'xmax', 'ymax', 'bndbox', 'object']

xmin ymin xmax ymax

0 87 104 501 375

Upvotes: 0

Related Questions