XML Dosyası
<annotation>
<folder>test</folder>
<filename>10_split_no_flash_nt_IMG_0009.jpg</filename>
<path>\images\10_split_no_flash_nt_IMG_0009.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>692</width>
<height>692</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>split</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>225</xmin>
<ymin>191</ymin>
<xmax>260</xmax>
<ymax>222</ymax>
</bndbox>
</object>
<object>
<name>split</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>294</xmin>
<ymin>456</ymin>
<xmax>319</xmax>
<ymax>476</ymax>
</bndbox>
</object>
<object>
<name>split</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>478</xmin>
<ymin>380</ymin>
<xmax>515</xmax>
<ymax>411</ymax>
</bndbox>
</object>
</annotation>
Python XML İşlemleri işlemleri için xml.etree.ElementTree
kütüphanesini kullanacağız.
İmport
import xml.etree.ElementTree as ET
mytree = ET.parse('sample.xml')
myroot = mytree.getroot()
print(myroot)
#<Element 'annotation' at 0x0000023962F5B680>
findall
import xml.etree.ElementTree as ET
mytree = ET.parse('sample.xml')
myroot = mytree.getroot()
print(myroot)#<Element 'annotation' at 0x000001FEEDCE7D10>
print(myroot.tag)#annotation
print(myroot.tag[0:4])#anno
print(myroot.attrib)#{}
for x in myroot.findall('object'):
name = x.find('name').text
pose = x.find('pose').text
truncated = x.find('truncated').text
difficult = x.find('difficult').text
#print(name, pose, truncated, difficult)
for y in x.find('bndbox'):
print("\t", y.tag, ":", y.text)
Find and Replace in XML files
import glob
import xml.etree.ElementTree as ET
#file dizininde sadece .xml uzantılı dosyaları okumak istersek
PATH="C:\\Users\\MAHMUT-PC\\Desktop\\doktora\\goruntu-isleme\\2023\\allXMLfilesandImages\\test\\*.xml"
#file dizininde tüm dosyaları okuyacak
#PATH="C:\\Users\\USER-PC\\Desktop\\file\\"
path=glob.glob(PATH)
for file in path:
mytree = ET.parse(file)
myroot = mytree.getroot()
#print(myroot)
print(myroot)
#print(file)
for x in myroot.findall('path'):
name = x.text
try:
x.text = x.text.replace('my-project-name', 'test')
#x.text = x.text.replace('FEATURE NUMBER', '123456')
except AttributeError:
pass
mytree.write(file, encoding='latin-1')
print("\t", name)
XML to csv
# based on https://github.com/datitran/raccoon_dataset/blob/master/xml_to_csv.py
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 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.')
main()