Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created December 24, 2018 08:52
Show Gist options
  • Save makotoshimazu/78c2c2a826e4acd3b942e619cf452bff to your computer and use it in GitHub Desktop.
Save makotoshimazu/78c2c2a826e4acd3b942e619cf452bff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8 :
#
# transform.py
#
# Author: Makoto Shimazu <[email protected]>
# URL: https://amiq11.tumblr.com
# License: 2-Clause BSD License
# Created: 2018-12-24
#
#
# Copyright (c) 2018, Makoto Shimazu
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import xml.etree.ElementTree as ET
class PascalVOCGenerator():
def __init__(self):
self._root = ET.Element('annotations')
self._tree = ET.ElementTree(self._root)
def addImage(self, fileName, crackLabels):
"""Adds annotation tag for this file.
crackLabels: array of cracks (sample: {name: 2, bb: (123 /*xmin*/, 264 /*ymin*/, 219/*xmax*/, 553 /*ymax*/)})
difficult, truncated and pose are omitted from the output.
"""
node = ET.SubElement(self._root, 'annotation')
ET.SubElement(node, 'filename').text = fileName
for crackLabel in crackLabels:
obj = ET.SubElement(node, 'object')
ET.SubElement(obj, 'name').text = str(crackLabel['name'])
bndbox = ET.SubElement(obj, 'bndbox')
ET.SubElement(bndbox, 'xmin').text = str(crackLabel['bb'][0])
ET.SubElement(bndbox, 'ymin').text = str(crackLabel['bb'][1])
ET.SubElement(bndbox, 'xmax').text = str(crackLabel['bb'][2])
ET.SubElement(bndbox, 'ymax').text = str(crackLabel['bb'][3])
def save(self, filePath):
self._tree.write(filePath)
def test():
pascalGenerator = PascalVOCGenerator()
pascalGenerator.addImage('0000.jpg', [{'name': 2, 'bb': (123, 456, 789, 555)}, {'name': 3, 'bb': (000, 111, 222, 333)}])
pascalGenerator.save('testtest.xml')
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment