提取VOC数据person类的全身标签(坐标)

import xml.dom.minidom # 第一种解析
import os

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

import xml.etree.ElementTree as ET # 第二种解析
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')

# tree = ET.parse('D:/University/Data/VOCdevkit/VOC2012/Annotations_ssd/2007_000272.xml')
tree = ET.parse(ann_filepath + '\\' + file)
root1 = tree.getroot()

for obj in root1.findall('object'):
    for shell in obj.findall('part'):       # 删除part
        obj.remove(shell)
    for bndx in obj.findall('bndbox'):
        xmin = int(bndx.find('xmin').text)
        ymin = int(bndx.find('ymin').text)
        xmax = int(bndx.find('xmax').text)
        ymax = int(bndx.find('ymax').text)

        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)