Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quadrilateral bounding box to LabelImg xml generation
import numpy as np
from pathlib import Path
import xml.etree.cElementTree as ET
from PIL import Image
def create_labimg_xml(image_path, annotation_list):
image_path = Path(image_path)
img = np.array('RGB'))
annotation = ET.Element('annotation')
ET.SubElement(annotation, 'folder').text = str(
ET.SubElement(annotation, 'filename').text = str(
ET.SubElement(annotation, 'path').text = str(image_path)
source = ET.SubElement(annotation, 'source')
ET.SubElement(source, 'database').text = 'Unknown'
size = ET.SubElement(annotation, 'size')
ET.SubElement(size, 'width').text = str (img.shape[1])
ET.SubElement(size, 'height').text = str(img.shape[0])
ET.SubElement(size, 'depth').text = str(img.shape[2])
ET.SubElement(annotation, 'segmented').text = '0'
for annot in annotation_list:
tmp_annot = annot.split(',')
cords, label = tmp_annot[0:-2], tmp_annot[-1]
xmin, ymin, xmax, ymax = cords[0], cords[1], cords[4], cords[5]
object = ET.SubElement(annotation, 'object')
ET.SubElement(object, 'name').text = label
ET.SubElement(object, 'pose').text = 'Unspecified'
ET.SubElement(object, 'truncated').text = '0'
ET.SubElement(object, 'difficult').text = '0'
bndbox = ET.SubElement(object, 'bndbox')
ET.SubElement(bndbox, 'xmin').text = str(xmin)
ET.SubElement(bndbox, 'ymin').text = str(ymin)
ET.SubElement(bndbox, 'xmax').text = str(xmax)
ET.SubElement(bndbox, 'ymax').text = str(ymax)
tree = ET.ElementTree(annotation)
xml_file_name = image_path.parent / ('.')[0]+'.xml')
# ----------------------------------------------------------------------------------
anotation_list = ['291,473,385,481,383,504,289,496,Hello',
create_labimg_xml('data/demo.jpg', anotation_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.