脚本:批量生成负样本空标签.xml标签文件

该代码片段展示了如何利用Python的os和xml.dom.minidom库来遍历指定目录中的图像文件,创建XML注释文件。每个文件的注释包括folder,filename,path,source,size等信息,用于图像识别或机器学习的数据标注。
摘要由CSDN通过智能技术生成
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()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值