I am using a similar xml creation code set up to the answer by @ssokolow here. The code is running and the output is correct, however the format is different than a xml that is create by say labelImg. I am not sure what is happening to cause this. The differences are:
- There is no
<?xml version="1.0"?>
. Is this a big concern? - There is a blank space between
<filename>
and<path>
<path>
looks as it is indented to<filename>
. I do have it indented to<annotation>
- Lastly, it seems that the labelImg xml indentations are "bigger". I don't think this is an issue, just something to note.
Any help would be appreciated.
import pandas
import os
import xml.etree.ElementTree as ET
csv_path = 'D:/path_to_csv/'
for csv_file in os.listdir(csv_path):
df = pandas.read_csv(csv_path + csv_file)
folder = 'images'
filename = str('item' + df['tif_name'][0])
path = 'S:/path/' + filename
width = 1500
height = 1500
depth = 3
root = ET.Element("annnotation")
folder_elem = ET.SubElement(root, "folder").text = folder
filename_elem = ET.SubElement(root, "filename").text = filename
path_elem = ET.SubElement(root, "path").text = path
source_elem = ET.SubElement(root, "source")
database_elem = ET.SubElement(source_elem, "database").text = "Unknown"
size_elem = ET.SubElement(root, "size")
width_elem = ET.SubElement(size_elem, "width").text = str(width)
height_elem = ET.SubElement(size_elem, "height").text = str(height)
depth_elem = ET.SubElement(size_elem, "depth").text = str(depth)
segmented_elem = ET.SubElement(root, "segmented").text = str(0)
# Appending Function goes here
tree = ET.ElementTree(root)
tree.write('C:output_path/item' + str(csv_file[:-4] + '.xml'))