#!/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()