Annotate images in pascal voc xml

22,035

Solution 1

This python code snippet will convert Sloth json to pascal voc xml.

  def make_anno():
    zind = 0
    for z in data:
        print zind
        filename = data[zind]["filename"]
        print filename
        head, tail = os.path.split(filename)
        basename, file_extension = os.path.splitext(tail)    
        f = open(basename + '.xml','w') 
        line = "<annotation>" + '\n'
        f.write(line)
        line = '\t\t<folder>' + "folder" + '</folder>' + '\n'
        f.write(line)
        line = '\t\t<filename>' + tail + '</filename>' + '\n'
        f.write(line)
        line = '\t\t<source>\n\t\t<database>Source</database>\n\t</source>\n'
        f.write(line)
        im=Image.open('/home/location/VOCdevkit/newdataset/img/' + tail)
        (width, height) = im.size
        line = '\t<size>\n\t\t<width>'+ str(width) + '</width>\n\t\t<height>' + str(height) + '</height>\n\t'
        line += '\t<depth>Unspecified</depth>\n\t</size>'
        f.write(line)
        line = '\n\t<segmented>Unspecified</segmented>'
        f.write(line)
        ind = 0
        for i in data[zind]["annotations"]:
            line = '\n\t<object>'
            line += '\n\t\t<name>Name</name>\n\t\t<pose>Unspecified</pose>'
            line += '\n\t\t<truncated>Unspecified</truncated>\n\t\t<difficult>Unspecified</difficult>'
            xmin = (data[zind]["annotations"][ind]["x"])
            line += '\n\t\t<bndbox>\n\t\t\t<xmin>' + str(xmin) + '</xmin>'
            ymin = (data[zind]["annotations"][ind]["y"])
            line += '\n\t\t\t<ymin>' + str(ymin) + '</ymin>'
            width = (data[zind]["annotations"][ind]["width"])
            height = (data[zind]["annotations"][ind]["height"])
            xmax = xmin + width
            ymax = ymin + height
            line += '\n\t\t\t<xmax>' + str(xmax) + '</xmax>'
            line += '\n\t\t\t<ymax>' + str(ymax) + '</ymax>'
            line += '\n\t\t</bndbox>'
            line += '\n\t</object>'     
            f.write(line)
            ind +=1
            f.close()
        zind +=1

Solution 2

Please refer to my github: https://github.com/tzutalin/ImageNet_Utils

How to annotate images : https://www.youtube.com/watch?v=p0nR2YsCY_U

Solution 3

It looks like there are no tools that output your desired format. You might want to use a tool that outputs xml in a different format, and transform that. Not ideal, but will probably work.

You could, for example, build an xslt to transform the xml output of your tools to the Pascal VOC xml standard.

Share:
22,035
gobob
Author by

gobob

Updated on July 13, 2022

Comments

  • gobob
    gobob almost 2 years

    I need a tool to annotate images with a rectangular bounding box. The output is going to be in pascal voc xml format. Annotations and images will be part of a training dataset used by a convolutional neural net to do object detection. I will annotate the images manually myself.

    I've considered the following tools but they don't support pascal-voc.

    Labelme, Sloth, Pilab, No name

    Is there a annotation tool that will save me time?

  • Pablo Gonzalez
    Pablo Gonzalez about 6 years
    Does pascal voc format accepts rotated rectangular annotations? I want to detect scratches but if I dont rotate the rectangle, I will get too much background when scratch is horizontal or vertical.