i manually constructed a XML file with desired annotations / pixel coordinates of image dataset, therefore i want to parse those annotations to single annotation object. This is my XML file:
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>
<images>
<image file='pngCA43_01.jpg'>
<box top='673' left='92' width='875' height='508'/>
</image>
<image file='pngCA43_02.jpg'>
<box top='680' left='79' width='885' height='501'/>
</image>
<image file='pngCA43_03.jpg'>
<box top='677' left='86' width='876' height='501'/>
</image>
<image file='pngCA43_04.jpg'>
<box top='675' left='84' width='878' height='505'/>
</image>
<image file='pngCA43_05.jpg'>
<box top='658' left='87' width='879' height='511'/>
</image>
And goes on like this for 1000 lines. I want to access file, top, left, width and height parameters in the same loop. This is my python code:
import xml.etree.ElementTree as ET
class Annotation():
name = ""
top = 0
left = 0
width = 0
height = 0
def __init__(self, name, top, left, width, height):
self.name = name
self.top = top
self.left = left
self.width = width
self.height = height
annotations = []
root_xml = ET.parse("xml/idcard.xml").getroot()
i = 1
for type_tag in root_xml.iter("box"):
name = type_tag.get('file')
top = type_tag.get('top')
left = type_tag.get('left')
width = type_tag.get('width')
height = type_tag.get('height')
print(f'{i}. Name: {name} Top: {top} Left: {left} Width: {width} Height: {height}\n')
annotationObject = Annotation(name, top, left, width, height)
annotations.append(annotationObject)
i += 1
This snippet gives the output:
1. Name: None Top: 673 Left: 92 Width: 875 Height: 508
2. Name: None Top: 680 Left: 79 Width: 885 Height: 501
3. Name: None Top: 677 Left: 86 Width: 876 Height: 501
4. Name: None Top: 675 Left: 84 Width: 878 Height: 505
5. Name: None Top: 658 Left: 87 Width: 879 Height: 511
The parent node 'file' ( name of the image) is not present since i am iterating over the box node. However when i replace root_xml.iter("box")
with root_xml.iter()
, output is:
1. Name: None Top: None Left: None Width: None Height: None
2. Name: pngCA43_01.jpg Top: None Left: None Width: None Height: None
3. Name: None Top: 673 Left: 92 Width: 875 Height: 508
4. Name: pngCA43_02.jpg Top: None Left: None Width: None Height: None
5. Name: None Top: 680 Left: 79 Width: 885 Height: 501
6. Name: pngCA43_03.jpg Top: None Left: None Width: None Height: None
I can work with this by using 2 different loops and getting name from one loop and other attributes from second loop, but i'm sure there should be a way to do this, thanks for the help:) Stay safe!
Edit: Terms about XML might've been wrong, this is my first time working with XML's.
See below (find all 'image' elements and use a dataclass for Annotation)
import xml.etree.ElementTree as ET
from dataclasses import dataclass
XML = '''<images>
<image file='pngCA43_01.jpg'>
<box top='673' left='92' width='875' height='508'/>
</image>
<image file='pngCA43_02.jpg'>
<box top='680' left='79' width='885' height='501'/>
</image>
<image file='pngCA43_03.jpg'>
<box top='677' left='86' width='876' height='501'/>
</image>
<image file='pngCA43_04.jpg'>
<box top='675' left='84' width='878' height='505'/>
</image>
<image file='pngCA43_05.jpg'>
<box top='658' left='87' width='879' height='511'/>
</image>
</images>'''
@dataclass
class Annotation:
name: str
top: int
left: int
width: int
height: int
root = ET.fromstring(XML)
annotations = [Annotation(i.attrib['file'], int(i.find('box').attrib['top']), int(i.find('box').attrib['left']),
int(i.find('box').attrib['width'])
, int(i.find('box').attrib['height'])) for i in root.findall('.//image')]
print(annotations)
output
[Annotation(name='pngCA43_01.jpg', top=673, left=92, width=875, height=508), Annotation(name='pngCA43_02.jpg', top=680, left=79, width=885, height=501), Annotation(name='pngCA43_03.jpg', top=677, left=86, width=876, height=501), Annotation(name='pngCA43_04.jpg', top=675, left=84, width=878, height=505), Annotation(name='pngCA43_05.jpg', top=658, left=87, width=879, height=511)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.