Skip to content

Instantly share code, notes, and snippets.

@jinyu121
Last active May 5, 2017 11:32
Show Gist options
  • Save jinyu121/2fb3264331241a3ec66039838ab686f6 to your computer and use it in GitHub Desktop.
Save jinyu121/2fb3264331241a3ec66039838ab686f6 to your computer and use it in GitHub Desktop.
VOC数据集验证
# -*- coding: utf-8 -*-
import os
import xml.etree.ElementTree as ET
from skimage import io, draw
xml_folder = "/home/haoyu/Annotations"
image_dir = "/home/haoyu/JPEGImages"
for parent, dirnames, filenames in os.walk(xml_folder):
for filename in filenames:
tree = ET.parse(os.path.join(xml_folder, filename))
root = tree.getroot()
img_filename = root.find(".//filename").text
img = io.imread(os.path.join(image_dir, img_filename))
print(img_filename)
for box in root.findall('.//bndbox'):
x_min = int(box.find('.//xmin').text)
y_min = int(box.find('.//ymin').text)
x_max = int(box.find('.//xmax').text)
y_max = int(box.find('.//ymax').text)
rr, cc = draw.polygon_perimeter(
[y_min, y_max, y_max, y_min],
[x_min, x_min, x_max, x_max]
)
draw.set_color(img, (rr, cc), (0, 0, 255))
io.imshow(img)
io.show()
# -*- coding: utf8 -*-
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from lxml import etree
import codecs
XML_EXT = '.xml'
class PascalVocWriter:
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
self.foldername = foldername
self.filename = filename
self.databaseSrc = databaseSrc
self.imgSize = imgSize
self.boxlist = []
self.localImgPath = localImgPath
self.verified = False
def prettify(self, elem):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf8')
root = etree.fromstring(rough_string)
return etree.tostring(root, pretty_print=True)
def genXML(self):
"""
Return XML root
"""
# Check conditions
if self.filename is None or \
self.foldername is None or \
self.imgSize is None:
return None
top = Element('annotation')
# top.set('verified', 'yes' if self.verified else 'no')
folder = SubElement(top, 'folder')
folder.text = self.foldername
filename = SubElement(top, 'filename')
filename.text = self.filename
localImgPath = SubElement(top, 'path')
localImgPath.text = self.localImgPath
source = SubElement(top, 'source')
database = SubElement(source, 'database')
database.text = self.databaseSrc
size_part = SubElement(top, 'size')
width = SubElement(size_part, 'width')
height = SubElement(size_part, 'height')
depth = SubElement(size_part, 'depth')
width.text = str(self.imgSize[1])
height.text = str(self.imgSize[0])
if len(self.imgSize) == 3:
depth.text = str(self.imgSize[2])
else:
depth.text = '1'
segmented = SubElement(top, 'segmented')
segmented.text = '0'
return top
def addBndBox(self, xmin, ymin, xmax, ymax, name):
bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax}
bndbox['name'] = name
self.boxlist.append(bndbox)
def appendObjects(self, top):
for each_object in self.boxlist:
object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name')
try:
name.text = unicode(each_object['name'])
except NameError:
# Py3: NameError: name 'unicode' is not defined
name.text = each_object['name']
pose = SubElement(object_item, 'pose')
pose.text = "Unspecified"
truncated = SubElement(object_item, 'truncated')
truncated.text = "0"
difficult = SubElement(object_item, 'difficult')
difficult.text = "0"
bndbox = SubElement(object_item, 'bndbox')
xmin = SubElement(bndbox, 'xmin')
xmin.text = str(each_object['xmin'])
ymin = SubElement(bndbox, 'ymin')
ymin.text = str(each_object['ymin'])
xmax = SubElement(bndbox, 'xmax')
xmax.text = str(each_object['xmax'])
ymax = SubElement(bndbox, 'ymax')
ymax.text = str(each_object['ymax'])
def save(self, targetFile=None):
root = self.genXML()
self.appendObjects(root)
out_file = None
if targetFile is None:
out_file = codecs.open(
self.filename + XML_EXT, 'w', encoding='utf-8')
else:
out_file = codecs.open(targetFile, 'w', encoding='utf-8')
prettifyResult = self.prettify(root)
out_file.write(prettifyResult.decode('utf8'))
out_file.close()
class PascalVocReader:
def __init__(self, filepath):
# shapes type:
# [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color]
self.shapes = []
self.filepath = filepath
self.verified = False
self.parseXML()
def getShapes(self):
return self.shapes
def addShape(self, label, bndbox):
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
self.shapes.append((label, points, None, None))
def parseXML(self):
assert self.filepath.endswith('.xml'), "Unsupport file format"
parser = etree.XMLParser(encoding='utf-8')
xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
filename = xmltree.find('filename').text
# try:
# verified = xmltree.attrib['verified']
# if verified == 'yes':
# self.verified = True
# except KeyError:
# self.verified = False
for object_iter in xmltree.findall('object'):
bndbox = object_iter.find("bndbox")
label = object_iter.find('name').text
self.addShape(label, bndbox)
return True
# tempParseReader = PascalVocReader('test.xml')
# print tempParseReader.getShapes()
"""
# Test
tmp = PascalVocWriter('temp','test', (10,20,3))
tmp.addBndBox(10,10,20,30,'chair')
tmp.addBndBox(1,1,600,600,'car')
tmp.save()
"""
# -*- coding: utf-8 -*-
import os
import xml.etree.ElementTree as ET
def get_filenames(file_dir):
filenames = os.listdir(file_dir)
filenames.sort()
return filenames
if __name__ == "__main__":
file_dir = "/home/haoyu/Annotations"
for fnm in get_filenames(file_dir):
tree = ET.parse(os.path.join(file_dir, fnm))
root = tree.getroot()
for bndbox in root.findall('./object/bndbox'):
xmin = int(bndbox.find('xmin').text)
xmax = int(bndbox.find('xmax').text)
ymin = int(bndbox.find('ymin').text)
ymax = int(bndbox.find('ymax').text)
try:
assert (xmin > 0)
assert (ymin > 0)
assert (xmin < xmax)
assert (ymin < ymax)
assert (xmax <= 640)
assert (ymax <= 480)
except:
print(xmin, xmax, ymin, ymax)
print(os.path.join(file_dir, fnm))
import os
from pascal_voc_io import PascalVocReader, PascalVocWriter
if '__main__' == __name__:
base_dir = './Annotations'
for parent, dirnames, filenames in os.walk(base_dir):
for filename in filenames:
fnm, fex = os.path.splitext(filename)
if '.xml' == fex:
reader = PascalVocReader(os.path.join(base_dir, filename))
writer = PascalVocWriter('VOC2007', fnm, [480, 640, 3],
localImgPath=os.path.join('JPEGImages', fnm + '.png'))
print(reader.shapes)
for label, pos, _, __ in reader.shapes:
# (xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)
# xmin, ymin, xmax, ymax
writer.addBndBox(pos[0][0], pos[0][1], pos[2][0], pos[2][1], label)
writer.save(targetFile=os.path.join("Anno2", filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment