Reputation: 5
I have a dataset with the following annotations structure:
`<annotation>
<folder>images</folder>
<filename>maksssksksss0.png</filename>
<size>
<width>512</width>
<height>366</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>without_mask</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>79</xmin>
<ymin>105</ymin>
<xmax>109</xmax>
<ymax>142</ymax>
</bndbox>
</object>
<object>
<name>with_mask</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>185</xmin>
<ymin>100</ymin>
<xmax>226</xmax>
<ymax>144</ymax>
</bndbox>
</object>
<object>
<name>without_mask</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>325</xmin>
<ymin>90</ymin>
<xmax>360</xmax>
<ymax>141</ymax>
</bndbox>
</object>
`
What i wanted to do was to edit all the object labels 'with_mask' to 'face_mask' and to delete all the object labels 'without_mask' from all the dataset files. Any tips on where to begin? libraries and such
Upvotes: 0
Views: 520
Reputation: 80
Take a look at the lxml library and import etree from there. For searching you can use .findall() and .find(). You also should take a look aswell on how basic xpath constraints work. To remove a element from a tree you can use the .remove function. If you put it all togehter it could look something like this:
from lxml import etree
#Open you xml
tree = etree.parse("yourxml.xml")
root = tree.getroot()
#Find all objects in your tree
for obj in root.findall('.//object'):
# Get the first child of the object which is called name
name = obj.find("name")
# Use .txt to access the value
if name.text == "with_mask":
# Change the text
obj.attrib["name"] = "face_mask"
if name.text == "without_mask":
#Remove the object from the tree
root.remove(obj)
# Write the tree
with open('yourxml2.xml', 'wb') as f:
tree.write(f)
Upvotes: 0