





# -*- 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/' #裁剪之后,圖片存放路徑
#如果路徑不存在,則創(chuàng )建路徑目錄
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)#將文件名和文件后綴給分開(kāi)
#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)#打開(kāi)xml文檔
annotation = DomTree.documentElement #得到xml對象
#通過(guò)標簽對filename之間,嵌入的數據 看右邊 # <filename>000001.jpg</filename>
filenamelist = annotation.getElementsByTagName('filename') # <filename>000001.jpg</filename>
#該返回值類(lèi)型為nodelist,是一個(gè)list由,標簽filename之間所有的node節點(diǎn)組成
filename = filenamelist[0].childNodes[0].data
#獲取filename標簽對之間的值
# 通過(guò)標簽object獲取標簽屬性值
objectlist = annotation.getElementsByTagName('object')
#objectlist 是一個(gè)NodeList列表
i = 1
for objects in objectlist:
# print objects
## 通過(guò)name標簽對之間的數據
namelist = objects.getElementsByTagName('name')
objectname = namelist[0].childNodes[0].data
print objectname
# 通過(guò)bndbox標簽對之間的數據 NodeList列表
bndbox = objects.getElementsByTagName('bndbox')
cropboxes = []
for box in bndbox:
try:
# 通過(guò) 標簽對 之間的數據
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)#打開(kāi)文件
width, height = img.size #文件的尺寸
obj = np.array([x1, y1, x2, y2])
#shift 是9個(gè)[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份,構成一個(gè)二維數組
#最終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)#原圖片進(jìn)行裁剪
#將裁剪后的圖片進(jìn)行保存
cropedimg.save(savepath + image_pre + '_' + str(i) + '.jpg')
i += 1
except Exception, e:
print e
聯(lián)系客服