其他分享
首页 > 其他分享> > 【Caffe】生成数据之修改label

【Caffe】生成数据之修改label

作者:互联网

目录

【Caffe】生成数据之修改label

当标注图片尺寸不一致时需要批量修改

import  xml.dom.minidom as xd
import os
import numpy as np
def changesize512(xml_path,xml_name):
    # 将label 尺寸更改为512
    tmp_name = xml_name
    xml_name = os.path.join(xml_path,xml_name)
    dom = xd.parse(xml_name)
    root=dom.documentElement 
    # print(xml_name)
    width = root.getElementsByTagName('width')
    height = root.getElementsByTagName('height')
    xmin = root.getElementsByTagName('xmin')
    ymin = root.getElementsByTagName('ymin')
    xmax = root.getElementsByTagName('xmax')
    ymax = root.getElementsByTagName('ymax')
    if width[0].firstChild.data == '1280' and height[0].firstChild.data=='1024':
        width[0].firstChild.data = 512
        height[0].firstChild.data = 512
        for i in range(len(xmin)):
            xmin[i].firstChild.data = int(int(xmin[i].firstChild.data)/1280*512)
            ymin[i].firstChild.data = int(int(ymin[i].firstChild.data)/1024*512)
            xmax[i].firstChild.data = int(int(xmax[i].firstChild.data)/1280*512)
            ymax[i].firstChild.data = int(int(ymax[i].firstChild.data)/1024*512)
    with open(tmp_name,'w') as fh:
        dom.writexml(fh)


if __name__=='__main__':
    pathDir = os.listdir('***/label')
    num = 0
    for allDir in pathDir:
        num += 1
        xml_path = '***label'
        xml_name = allDir
        changesize512(xml_path,xml_name)
        # break
    print(num)

标签:xml,name,int,root,firstChild,生成,Caffe,label,data
来源: https://www.cnblogs.com/wangxiaobei2019/p/14323756.html