分享

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

 雪柳花明 2017-08-02

VOC的数据处理 

 工程如下:

xml文件如下:




 
   
 

结果如下:
 
 

# -*- coding: utf-8 -*-
from __future__ import division
import os
from PIL import Image

import xml.dom.minidom #xml.dom.minidom解析模块解析xml文件
import numpy as np

#文件路径
ImgPath = './JPEGImages/' #图片路径
AnnoPath = './Annotations/' #xml文件路径
savepath = './CropedVOC/' #裁剪之后,图片存放路径

#如果路径不存在,则创建路径目录
if not os.path.exists(savepath):
os.makedirs(savepath)

imagelist = os.listdir(ImgPath)#返回该路径下的文件和文件夹列表

for image in imagelist:
print 'a new image:', image #000001.jpg
image_pre, ext = os.path.splitext(image)#将文件名和文件后缀给分开
#image_pre 为文件名000001 ext为文件后缀jpg

imgfile = ImgPath + image #./JPEGImages/000001.jpg
xmlfile = AnnoPath + image_pre + '.xml' #获取图片对应的xml文件
#./Annotations/000001.xml

DomTree = xml.dom.minidom.parse(xmlfile)#打开xml文档
annotation = DomTree.documentElement #得到xml对象

#通过标签对filename之间,嵌入的数据 看右边 # <filename>000001.jpg</filename>
filenamelist = annotation.getElementsByTagName('filename') # <filename>000001.jpg</filename>
#该返回值类型为nodelist,是一个list由,标签filename之间所有的node节点组成
filename = filenamelist[0].childNodes[0].data
#获取filename标签对之间的值

# 通过标签object获取标签属性值
objectlist = annotation.getElementsByTagName('object')
#objectlist 是一个NodeList列表

i = 1
for objects in objectlist:
# print objects

## 通过name标签对之间的数据
namelist = objects.getElementsByTagName('name')
objectname = namelist[0].childNodes[0].data
print objectname

# 通过bndbox标签对之间的数据 NodeList列表
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 是9个[0., 0., 1., 1.]组成 shape=[9,4]
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))#将obj[,,,]复制,9份,构成一个二维数组
#最终XYmatrix shape=[9,4]

cropboxes = XYmatrix * shift #shape=[9,4]

for cropbox in cropboxes:
# print 'cropbox:',cropbox
minX = (int)(max(0, cropbox[0]))
minY = (int)(max(0, cropbox[1]))
maxX = (int)(min(cropbox[2], width))
maxY = (int)(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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多