首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何从XML文件中自动获取相同类型的每个对象?

如何从XML文件中自动获取相同类型的每个对象?
EN

Stack Overflow用户
提问于 2021-07-29 16:07:29
回答 1查看 116关注 0票数 0

我正在尝试将XML文件解析为TXT文件。这是我的XML文件的样子:

代码语言:javascript
运行
AI代码解释
复制
<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文件中有任何其他边界框坐标,我的代码将直接跳过它。我不想这样。下面是我的代码:

代码语言:javascript
运行
AI代码解释
复制
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]
  '''---------------------------------------------------------'''
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-29 16:37:37

使用xpath解析XML可以遍历objList项。仅显示第一个项目

代码语言:javascript
运行
AI代码解释
复制
>>> 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'

迭代所有对象

代码语言:javascript
运行
AI代码解释
复制
>>> 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'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68584021

复制
相关文章
location.hash详解[通俗易懂]
一般情况下为URL后 “#” 及其后面一部分组成,如http://www.test.com/#/something,
全栈程序员站长
2022/09/14
4710
怎么自动刷新jwt?
如果用户一直在操作,当jwt颁发的token凭证到了过期时间需要有一个机制能自动延长过期时间。除非用户长时间没有操作,那是需要强制重新登录的。
蛋蛋编程手记
2022/07/27
3.3K0
自动刷新页面
script> <!--  var limit="0:15" if (document.images){ var parselimit=limit.split(":") parselimit=parselimit[0]*60+parselimit[1]*1 } function beginrefresh(){ if (!document.images) return if (parselimit==1) window.location.reload() else{  parselimit-=1 curmin
张善友
2018/01/19
2.5K0
前端自动刷新工具
Browsersync能让浏览器实时、快速响应您的文件更改(html、js、css、sass、less等)并自动刷新页面。更重要的是 Browsersync可以同时在PC、平板、手机等设备下进项调试。
李维亮
2021/07/09
1.1K0
javascript_时间自动刷新
=========================================================
Hongten
2018/09/13
1.8K0
javascript_时间自动刷新
Weex iOS端的自动刷新和调试
最近开始接触weex。目前weex尚不成熟,官方文档有些粗糙,初学者难免碰壁。这里分享两条我的初学经验,供参考。
全栈程序员站长
2022/07/20
1.2K0
Weex iOS端的自动刷新和调试
为什么你学不会递归?
可能很多人在大一的时候,就已经接触了递归了,不过,我敢保证很多人初学者刚开始接触递归的时候,是一脸懵逼的,我当初也是,给我的感觉就是,递归太神奇了!
Java3y
2019/11/18
5690
为什么C++不会衰老
Thoughts about C++ in the modern world. 一些关于当代C++的思考。 Debates about the present state of C++ in t
程序员的酒和故事
2018/03/12
9620
为什么C++不会衰老
Python 的切片为什么不会索引越界?
切片(slice)是 Python 中一种很有特色的特性,在正式开始之前,我们先来复习一下关于切片的知识吧。
Python猫
2021/12/20
1.6K0
Python 的切片为什么不会索引越界?
通过MySQL自动同步刷新Redis
在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终的存储引擎,Redis其实也可以作为一种键值对型的数据库,但在一些实际场景中,特别是关系型结构并不适合使用Redis直接作为数据库。这俩家伙简直可以用“男女搭配,干活不累”来形容,搭配起来使用才能事半功倍。本篇我们就这两者如何合理搭配以及他们之间数据如何进行同步展开。 一般地,Redis可以用来作为MySQL的缓存层。为什么MySQL最好有缓存层呢?想象一下这样的场景:在一个多人在线的游戏里,排行榜、好友关系、队列等直接关系数据的情景下,如果直接
逸鹏
2018/04/11
8K0
通过MySQL自动同步刷新Redis
C语言为什么不会过时?
评价任何一门编程语言,都是招人骂的。永远是这样。就像是春寒料峭的季节,街上穿棉袄和穿单衣的擦肩而过,双方一定是同时在心里出现了两个字:“傻逼!”这个在心理学上有个专业的名字:叫做“二逼”现象!
帅地
2020/07/03
1.6K0
为什么你学不会Appium
- 基于webdriver协议,看着眼熟不?没错就是和selenium webdriver同源的机制
苦叶子
2019/09/12
6220
学习分享——location.hash的用法「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。 【学习分享】location.hash的用法 location对象:设置或获取当前URL的信息 使用location对象可以设置或返回URL中的一些信息,一个
全栈程序员站长
2022/09/07
8610
为什么C语言不会过时?
评价任何一门编程语言,都是招人骂的。 永远是这样。就像是春寒料峭的季节, 街上穿棉袄和穿单衣的擦肩而过,双方一定是同时在心里出现了两个字:“傻逼!”这个在心理学上有个专业的名字:叫做“二逼”现象!
CSDN技术头条
2019/10/10
9780
为什么C语言不会过时?
为什么C语言不会过时?
来源:赵岩老师的个人网站:http://zhaoyan.website/blog/index.php/2017/07/15/future/
帅地
2019/02/26
1.2K0
你为什么学不会lambda
这个一篇对学习 Lambda 表达式进行解惑的文章,主要是总结了自己在学习 Lambda 表达式时候的一些理解不了的坑进行总结。长篇大论和详细语法,自行谷歌,这里只说一些理解起来不顺的概念。
潇洒
2023/10/20
1070
你为什么学不会lambda
这个一篇对学习 Lambda 表达式进行解惑的文章,主要是总结了自己在学习 Lambda 表达式时候的一些理解不了的坑进行总结。长篇大论和详细语法,自行谷歌,这里只说一些理解起来不顺的概念。
潇洒
2019/07/03
6340
你为什么学不会lambda
SwipeRefreshLayout实现上拉下拉刷新
1:在布局中添加SwipeRefreshLayout和Listview组件 [html] view plain copy <?xml version="1.0" encoding="utf-8
xiangzhihong
2018/02/02
1.1K0
点击加载更多

相似问题

在我的global.asax中,我可以输出当前触发的路由/动作吗?

21

在JavaFX中哪个动作按钮可以触发?

25

我可以在Selenuim中选择动作元素吗?

21

蓝牙可以触发应用程序中的某个动作吗?

20

Memcached是为我准备的吗?

20
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档