from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np
ImgPath = r'D:\code\data\JPEGImages_newname'
AnnoPath = r'D:\code\data\Annotations_newname'
ProcessedPath = r'D:\code\data\output'
prefix_str = '''<annotation>
<folder>VOC2007</folder>
<filename>{}.jpeg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>Ring Cell Dataset</annotation>
<image>flickr</image>
</source>
<size>
<width>2003</width>
<height>2010</height>
<depth>3</depth>
</size>
<segmented>0</segmented>'''
suffix = '</annotation>'
new_head = ''' <object>
<name>head</name>
<bndbox>
<xmin>{}</xmin>
<ymin>{}</ymin>
<xmax>{}</xmax>
<ymax>{}</ymax>
</bndbox>
<difficult>0</difficult>
</object>'''
imagelist = os.listdir(ImgPath)
for image in imagelist:
image_pre, ext = os.path.splitext(image)#将图片的名称拆成两部分,名称和图片格式
imgfile = ImgPath + '\\' + image
xmlfile = AnnoPath + '\\' + image_pre + '.xml'
DomTree = xml.dom.minidom.parse(xmlfile) # 打开xml文档
annotation = DomTree.documentElement # 得到xml文档对象
filenamelist = annotation.getElementsByTagName('filename') # [<DOM Element: filename at 0x381f788>]
filename = filenamelist[0].childNodes[0].data # 获取XML节点值
namelist
目标检测数据增强:同时切割图片与xml标注
最新推荐文章于 2023-01-28 17:14:07 发布