xml标签文件分析

#!/usr/bin/env python
# @File : voc_window_file.py
import os, sys
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt 


reload(sys)
sys.setdefaultencoding('utf-8')
 
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
 
    tree = ET.parse(filename)
    objects = []
 
    for pic in tree.findall('size'):
        pic_struct = {}
        pic_struct['width'] = int(pic.find('width').text)
        pic_struct['height'] = int(pic.find('height').text)
        pic_struct['depth'] = int(pic.find('depth').text)
        objects.append(pic_struct)
 
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        # obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(float(bbox.find('xmin').text)),
                              int(float(bbox.find('ymin').text)),
                              int(float(bbox.find('xmax').text)),
                              int(float(bbox.find('ymax').text))]
        objects.append(obj_struct)
    return objects
 
def main():
    array=[]
    vocdevkit = '/mnt/cascade-rcnn-master/data/VOCdevkit'
    VOCopts_classes=['Pedestrian','Cyclist', 'Person_sitting']
    years = ['2018']
    split = ['train']
    fileName = 'window_file_voc2018_train.txt'
    #fid = open(fileName, 'wt')
 
    # just running
    print ("VOC years: %s" %(years[0]))
    txt_ = os.path.join(vocdevkit, 'VOC' + years[0], 'ImageSets', 'Main' , split[0] + '.txt')
    f = open(txt_); idList = f.readlines() ; f.close()
    print len(idList)
 
    idCount = 0
 
    for u in range(len(idList)):
        nm = idList[u].strip() + '.jpg'
        xm = idList[u].strip() + '.xml'
        imgFullName = os.path.join('VOC' + years[0], 'JPEGImages', nm)
        # imgPath = os.path.join(vocdevkit, 'VOC' + years[0], 'JPEGImages', nm)
        xmlPath = os.path.join(vocdevkit, 'VOC' + years[0], 'Annotations', xm)
        nObjs = parse_rec(xmlPath)
 
        chw1 = nObjs[0]['depth']
        chw2 = nObjs[0]['height']
        chw3 = nObjs[0]['width']
        #fid.write('# %d\n' % u)
        #fid.write('%s\n' % imgFullName)
        #fid.write('%d\n%d\n%d\n' % (chw1, chw2, chw3))
        #fid.write('%d\n' % (len(nObjs) - 1))
        for j in range(1, len(nObjs)):
 
            clsIds = nObjs[j]['name']
            xmin = nObjs[j]['bbox'][0]
            ymin = nObjs[j]['bbox'][1]
            xmax = nObjs[j]['bbox'][2]
            ymax = nObjs[j]['bbox'][3]
            if (xmax <= xmin) | (ymax <= ymin):
                continue
        print clsIds
            clsId = VOCopts_classes.index(clsIds) + 1
            t_lines = str(clsId) + ' 0' + ' 0' + ' ' + str(xmin) + ' ' + str(ymin) + ' ' + str(xmax) + ' ' + str(ymax)
            objwidth=xmax-xmin
            objheight=ymax-ymin
            ratio=float(objwidth)/objheight
            #proportion=round(a/b,5)           
            print('ratio is :%f \n' %ratio)
            array.append(ratio)
            #fid.write('%s\n' % t_lines)
            ## print t_lines

        #fid.write('%d\n' % 0)
        idCount += 1
 
    n, bins, patches = plt.hist(array, bins=20, normed=0,edgecolor='None',facecolor='red')
    plt.xlabel('ratio')
    plt.ylabel('num')
    plt.savefig("aspectratio20.png")  
    #fid.close()
 
if __name__ == "__main__":
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值