python 处理pascal voc数据 读取xml文件

来源:互联网 发布:施工网络计划图软件 编辑:程序博客网 时间:2024/05/19 14:39

Pascal VOC数据的annotation是xml文件,要利用xml文件里的标注信息裁剪出数据~~


from __future__ import divisionimport osfrom PIL import Imageimport xml.dom.minidomimport numpy as npImgPath = 'C:/Users/liesmars/Desktop/VOC2012/JPEGImages/' AnnoPath = 'C:/Users/liesmars/Desktop/VOC2012/Annotations/'ProcessedPath = 'C:/Users/liesmars/Desktop/CropedVOC/'if not os.path.exists(ProcessedPath):os.makedirs(ProcessedPath)imagelist = os.listdir(ImgPath)for image in imagelist:print 'a new image:', imageimage_pre, ext = os.path.splitext(image)imgfile = ImgPath + image xmlfile = AnnoPath + image_pre + '.xml'DomTree = xml.dom.minidom.parse(xmlfile)annotation = DomTree.documentElementfilenamelist = annotation.getElementsByTagName('filename') #[<DOM Element: filename at 0x381f788>]filename = filenamelist[0].childNodes[0].dataobjectlist = annotation.getElementsByTagName('object')i = 1for objects in objectlist:# print objectsnamelist = objects.getElementsByTagName('name')# print 'namelist:',namelistobjectname = namelist[0].childNodes[0].dataprint objectnamebndbox = objects.getElementsByTagName('bndbox')cropboxes = []for box in bndbox:try:x1_list = box.getElementsByTagName('xmin')x1 = int(x1_list[0].childNodes[0].data)y1_list = box.getElementsByTagName('ymin')y1 = int(y1_list[0].childNodes[0].data)x2_list = box.getElementsByTagName('xmax')x2 = int(x2_list[0].childNodes[0].data)y2_list = box.getElementsByTagName('ymax')y2 = int(y2_list[0].childNodes[0].data)w = x2 - x1h = y2 - y1img = Image.open(imgfile)width,height = img.sizeobj = np.array([x1,y1,x2,y2])shift = np.array([[0.8,0.8,1.2,1.2],[0.9,0.9,1.1,1.1],[1,1,1,1],[0.8,0.8,1,1],[1,1,1.2,1.2],\[0.8,1,1,1.2],[1,0.8,1.2,1],[(x1+w*1/6)/x1,(y1+h*1/6)/y1,(x2+w*1/6)/x2,(y2+h*1/6)/y2],\[(x1-w*1/6)/x1,(y1-h*1/6)/y1,(x2-w*1/6)/x2,(y2-h*1/6)/y2]])XYmatrix = np.tile(obj,(9,1))  cropboxes = XYmatrix * shiftfor cropbox in cropboxes:# print 'cropbox:',cropboxminX = max(0,cropbox[0])minY = max(0,cropbox[1])maxX = min(cropbox[2],width)maxY = min(cropbox[3],height)cropbox = (minX,minY,maxX,maxY)cropedimg = img.crop(cropbox)cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg')i += 1except Exception, e:print e




0 0
原创粉丝点击