python提取voc数据集person类头部框(坐标)

打开xml文件,存到TXT文件里面(处理person类的head标签)

import xml.dom.minidom
import os

if name == ‘main’:
ann_filepath = ‘D:/University/Data/VOCdevkit/VOC2012/Annotations_ssd/’
ann_savepath = ‘D:/University/Data/VOCdevkit/VOC2012/Annotations_result/’
if not os.path.exists(ann_savepath): # 创建文件夹
os.mkdir(ann_savepath)

for file in os.listdir(ann_filepath):
    fp = open(ann_filepath + '\\' + file)       # 读取原文件
    newfile = file.replace('.xml','.txt')
    ann_savefile = ann_savepath + newfile       # 保存文件
    fp_w = open(ann_savefile, 'w')

    dom1 = xml.dom.minidom.parse(ann_filepath + '\\' + file)        #打开xml文件
    root = dom1.documentElement              # 得到文档元素对象
    print(root.nodeName)
    parts=root.getElementsByTagName("part")
    for part in parts:
        abc= part.getElementsByTagName("name")[0]
        name=abc.childNodes[0].data
        if(name=='head'):
            bdox = part.getElementsByTagName("bndbox")[0]
            xmin=bdox.getElementsByTagName("xmin")[0]
            # print(xmin.firstChild.data)
            ymin = bdox.getElementsByTagName("ymin")[0]
            xmax = bdox.getElementsByTagName("xmax")[0]
            ymax = bdox.getElementsByTagName("ymax")[0]

            fp_w.write(xmin.firstChild.data)
            fp_w.write(',')
            fp_w.write(ymin.firstChild.data)
            fp_w.write('\n')
            fp_w.write(xmax.firstChild.data)
            fp_w.write(',')
            fp_w.write(ymax.firstChild.data)
            fp_w.close()

你可能感兴趣的:(数据处理,xml,python)