import os
import xml.dom.minidom
i=0
img_path = 'E:\yolov3-9.5.0\\filt_in_picture\QQ20230504165738'
xml_path = 'E:\yolov3-9.5.0\get_kongbiaoqian'
for img_file in os.listdir(img_path):
img_name = os.path.splitext(img_file)[0]
print(img_name)
print(i)
i=i+1
# create an empty dom document object
doc = xml.dom.minidom.Document()
# creat a root node which name is annotation
annotation = doc.createElement('annotation')
# add the root node to the dom document object
doc.appendChild(annotation)
# add the folder subnode
folder = doc.createElement('folder')
folder_text = doc.createTextNode('VOC2012')
folder.appendChild(folder_text)
annotation.appendChild(folder)
# add the filename subnode
filename = doc.createElement('filename')
filename_text = doc.createTextNode(img_file)
filename.appendChild(filename_text)
annotation.appendChild(filename)
# add the path subnode
path = doc.createElement('path')
path_text = doc.createTextNode(img_path + img_file)
path.appendChild(path_text)
annotation.appendChild(path)
# add the source subnode
source = doc.createElement('source')
database = doc.createElement('database')
database_text = doc.createTextNode('Unknown')
source.appendChild(database)
database.appendChild(database_text)
annotation.appendChild(source)
# add the size subnode
size = doc.createElement('size')
width = doc.createElement('width')
width_text = doc.createTextNode('1920')
height = doc.createElement('height')
height_text = doc.createTextNode('1080')
depth = doc.createElement('depth')
depth_text = doc.createTextNode('3')
size.appendChild(width)
width.appendChild(width_text)
size.appendChild(height)
height.appendChild(height_text)
size.appendChild(depth)
depth.appendChild(depth_text)
annotation.appendChild(size)
# add the segmented subnode
segmented = doc.createElement('segmented')
segmented_text = doc.createTextNode('0')
segmented.appendChild(segmented_text)
annotation.appendChild(segmented)
# write into the xml text file
# with open(xml_path + '%s.xml' %img_name,'w+') as fp:
# open('%s.xml' % img_name,'w+')
fp = open('%s.xml' %img_name,'w+')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8')
fp.close()
脚本:批量生成负样本空标签.xml标签文件
该代码片段展示了如何利用Python的os和xml.dom.minidom库来遍历指定目录中的图像文件,创建XML注释文件。每个文件的注释包括folder,filename,path,source,size等信息,用于图像识别或机器学习的数据标注。
摘要由CSDN通过智能技术生成