如何高效使用python生成labelimg空白xml文件

  • 训练模型需要用到空白的xml,生成负样本的xml文件
# -*- coding:utf-8 -*-
from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString
import json
import os
import cv2
import codecs
import numpy as np
from PIL import  Image

def save_xml(image_name, width, height,channel=3):
    save_dir = '保存xml的文件夹路径'

    node_root = Element('annotation')     

    node_folder = SubElement(node_root, 'folder')
    node_folder.text = 'VOC2007'

    node_filename = SubElement(node_root, 'filename')
    node_filename.text = image_name

    node_source = SubElement(node_root,'source')
    node_database = SubElement(node_source,'database')
    node_database.text = 'Unknown'

    node_size = SubElement(node_root, 'size')
    node_width = SubElement(node_size, 'width')
    node_width.text = '%s' % width

    node_height = SubElement(node_size, 'height')
    node_height.text = '%s' % height

    node_depth = SubElement(node_size, 'depth')
    node_depth.text = '%s' % channel

    # for x, y, w, h in list(bbox):
    x, y, w, h = 0, 0, width, height
    left, top, right, bottom = x, y, x + w, y + h
    node_object = SubElement(node_root, 'object')
    node_name = SubElement(node_object, 'name')
    node_name.text = 'hxq_gjtps'
    node_bndbox = SubElement(node_object, 'bndbox')
    node_xmin = SubElement(node_bndbox, 'xmin')
    node_xmin.text = '%s' % left
    node_ymin = SubElement(node_bndbox, 'ymin')
    node_ymin.text = '%s' % top
    node_xmax = SubElement(node_bndbox, 'xmax')
    node_xmax.text = '%s' % right
    node_ymax = SubElement(node_bndbox, 'ymax')
    node_ymax.text = '%s' % bottom

    node_object = SubElement(node_root, 'object')
    node_name = SubElement(node_object, 'name')
    node_name.text = 'hxq_gjbs'
    node_bndbox = SubElement(node_object, 'bndbox')
    node_xmin = SubElement(node_bndbox, 'xmin')
    node_xmin.text = '%s' % left
    node_ymin = SubElement(node_bndbox, 'ymin')
    node_ymin.text = '%s' % top
    node_xmax = SubElement(node_bndbox, 'xmax')
    node_xmax.text = '%s' % right
    node_ymax = SubElement(node_bndbox, 'ymax')
    node_ymax.text = '%s' % bottom

    xml = tostring(node_root, pretty_print=True)
    dom = parseString(xml)

    save_xml = os.path.join(save_dir, image_name.replace('jpg', 'xml'))
    with open(save_xml, 'wb') as f:
        f.write(xml)

    return



if __name__ == '__main__':

    root_path = '图片路径'

    imagelist = os.listdir(os.path.join(root_path))

    for image_name in imagelist:
        if '.DS_Store' in image_name:    #跳过苹果电脑生成的.DS_Store文件
            continue
        image = root_path + image_name   #图片的路径
        img = Image.open(image)
        img_size = img.size
        w = img.width
        h = img.height
        print('image',image,w,h)

        save_xml(image_name,w,h)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值