python 实现对应图片名合并二个的xml文件

python 实现对应图片名合并二个的xml文件

例如:A.xml

<annotation>
	<folder>JPEGImages</folder>
	<filename>dog.jpg</filename>
	<path>/home/ycc/darknet-master/data/JPEGImages/dog.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>768</width>
		<height>576</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>dog</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>125</xmin>
			<ymin>215</ymin>
			<xmax>308</xmax>
			<ymax>545</ymax>
		</bndbox>
	</object>
</annotation>

B.xml

<annotation>
	<folder>JPEGImages</folder>
	<filename>dog.jpg</filename>
	<path>/home/ycc/darknet-master/data/JPEGImages/dog.jpg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>768</width>
		<height>576</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>bicycle</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>115</xmin>
			<ymin>135</ymin>
			<xmax>568</xmax>
			<ymax>427</ymax>
		</bndbox>
	</object>
	<object>
		<name>car</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>466</xmin>
			<ymin>70</ymin>
			<xmax>688</xmax>
			<ymax>177</ymax>
		</bndbox>
	</object>
</annotation>

批量图片合并:将想要合并的xml分别置于Annotations和Annotations,out用于保存合并的xml。

from xml.etree.ElementTree import ElementTree, Element, parse

import xml.etree.ElementTree as ET

import os

import shutil

hole_path = './Annotations'

arm_path = './Annotations1'

out_path = './out'

# 格式化

def __indent(elem, level=0):

    i = "\n" + level*"\t"

    if len(elem):

        if not elem.text or not elem.text.strip():

            elem.text = i + "\t"

        if not elem.tail or not elem.tail.strip():

            elem.tail = i

        for elem in elem:

            __indent(elem, level+1)

            if not elem.tail or not elem.tail.strip():

                elem.tail = i

    else:

        if level and (not elem.tail or not elem.tail.strip()):

            elem.tail = i

for hole_xml in os.listdir(hole_path):

    # 将同名xml合并

    if os.path.exists(os.path.join(arm_path,hole_xml)):

        print('fusing',hole_xml)

        tree_hole = parse(os.path.join(hole_path,hole_xml))

        root_hole = tree_hole.getroot() # annotation

        new_hole = tree_hole

        tree_arm = parse(os.path.join(arm_path,hole_xml))

        root_arm = tree_arm.getroot() # annotation

        object = (tree_arm.findall('object'))

        for i in range(len(object)):

            root_hole.append(object[i])

            __indent(root_hole)

            new_hole.write(os.path.join(out_path,hole_xml))

            # 不同名xml复制

    else:

        print('copying',hole_xml)

        shutil.copy(os.path.join(hole_path,hole_xml), out_path)

# 将不同名xml复制

for arm_xml in os.listdir(arm_path):

    if not os.path.exists(os.path.join(out_path,arm_xml)):

        print('copying')

        shutil.copy(os.path.join(arm_path, arm_xml), out_path)

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学术菜鸟小晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值