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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章