【Caffe】生成数据之修改label

目录

【Caffe】生成数据之修改label

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

  • 修改图片 cv2.resize()
  • 修改xml标注框
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)

上一篇:python – 编译时Caffe没有看到hdf5.h


下一篇:通过Dockerfile创建caffe-gpu环境