我正在尝试将XML文件解析为TXT文件。这是我的XML文件的样子:
<annotation>
<folder>training</folder>
<filename>106310488.jpg</filename>
<source>
<database>synthetic initialization</database>
<annotation>PASCAL VOC2007</annotation>
<image>synthetic</image>
<flickrid>none</flickrid>
</source>
<owner>
<flickrid>none</flickrid>
<name>none</name>
</owner>
<size>
<width>1024</width>
<height>681</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>shell</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>234</xmin>
<ymin>293</ymin>
<xmax>281</xmax>
<ymax>340</ymax>
</bndbox>
</object>
<object>
<name>shell</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>504</xmin>
<ymin>302</ymin>
<xmax>551</xmax>
<ymax>349</ymax>
</bndbox>
</object>
<object>
<name>shell</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>776</xmin>
<ymin>302</ymin>
<xmax>823</xmax>
<ymax>349</ymax>
</bndbox>
</object>
</annotation>
我感兴趣的信息在<object>
中。我想要得到<bndbox>
中的<name>
和所有东西。这些是数据集中对象的名称和边界框坐标。我不知道每个XML文件中的<bndbox>
都有<object>
条目,所以我想编写一个获取所有条目的逻辑。
到目前为止,我的逻辑所做的就是只获取和处理第一次出现的<object><bndbox></bndbox></object>
。如果XML文件中有任何其他边界框坐标,我的代码将直接跳过它。我不想这样。下面是我的代码:
for annotations_file in annotations_dir:
annotations = []
milliseconds = int(time() * 1000)
doc = ET.parse('/content/darknet/logorec/openlogo/Annotations/' + annotations_file) # Parsing the XML file
new_annotations_file_name = annotations_file.split('.')[0] # Getting the name of the XML file without the file extension
canvas = cv2.imread('/content/darknet/logorec/openlogo/JPEGImages/' + new_annotations_file_name + '.jpg') # Get the entire image
canvas_shape = canvas.shape # Get the dimensions of the image
root = doc.getroot() # Gets the root of the XML file
annotations_box = root[6][4] # Gets the bounding box coordinates from the XML file
class_name = root[6][0] # Name of the object within the bounding box
class_name = class_name.text # Getting the text value
for ant in annotations_box:
annotations.append(ant.text) # Appending every sindle bounding box coordinate to an empty list
''' These are my annotations calculations for the YOLO model'''
logo_shape_w = int(annotations[2]) - int(annotations[0])
logo_shape_h = int(annotations[3]) - int(annotations[1])
x1 = int(annotations[0]) # x1 = xmin
y1 = int(annotations[3]) # y1 = ymax
x2 = x1 + logo_shape_w
y2 = y1 + logo_shape_h
w = x2 - x1
h = y2 - y1
center_x = x1 + (w/2)
center_y = y1 + (h/2)
x = center_x / canvas_shape[0]
y = center_y / canvas_shape[1]
width = w / canvas_shape[0]
height = h / canvas_shape[1]
'''---------------------------------------------------------'''
发布于 2021-07-29 16:37:37
使用xpath解析XML可以遍历objList项。仅显示第一个项目
>>> from lxml import etree
>>> tree = etree.parse('test.xml')
>>> objList = tree.xpath('//object')
>>> bnd = objList[0].xpath('name | bndbox/*')
>>> for e in bnd:
... e.text
...
'shell'
'234'
'293'
'281'
'340'
迭代所有对象
>>> for obj in objList:
... bnd = obj.xpath('name | bndbox/*')
... for e in bnd:
... e.text
...
'shell'
'234'
'293'
'281'
'340'
'shell'
'504'
'302'
'551'
'349'
'shell'
'776'
'302'
'823'
https://stackoverflow.com/questions/68584021
复制相似问题