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

本文介绍了一个用于处理PascalVOC数据集的Python脚本,该脚本可以从原始图片中根据XML标注文件裁剪出目标物体并保存为新的图片文件,支持多种裁剪比例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np

ImgPath = '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:', image
	image_pre, ext = os.path.splitext(image)
	imgfile = ImgPath + image 
	xmlfile = AnnoPath + image_pre + '.xml'
	
	DomTree = xml.dom.minidom.parse(xmlfile)
	annotation = DomTree.documentElement

	filenamelist = annotation.getElementsByTagName('filename') #[<DOM Element: filename at 0x381f788>]
	filename = filenamelist[0].childNodes[0].data
	objectlist = annotation.getElementsByTagName('object')
	
	i = 1
	for objects in objectlist:
		# print objects
		
		namelist = objects.getElementsByTagName('name')
		# print 'namelist:',namelist
		objectname = namelist[0].childNodes[0].data
		print objectname


bndbox = 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 - x1
				h = y2 - y1

				img = Image.open(imgfile)
				width,height = img.size

				obj = 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 * shift
	
				for cropbox in cropboxes:
					# print 'cropbox:',cropbox
					minX = 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 += 1

			except Exception, e:
				print e




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值