pascal-voc-writer icon indicating copy to clipboard operation
pascal-voc-writer copied to clipboard

writer.addObject is not working

Open shreesurya opened this issue 4 years ago • 1 comments

import os
import numpy as np
from pascal_voc_writer import Writer

bboxList = [(77, 238, 230, 458),(99, 298, 0, 48),(1, 2, 3, 4)]
objectLabelList = ['left','right','front'] 
imageDirPathBuffer = '__Path__/name.jpg'

anno_filename = 'annotations.csv'
w, h = 1920, 1080
def convert2xml(bboxList, objectLabelList, imageDirPathBuffer):
    writer = Writer(imageDirPathBuffer,w,h)
    annotation_file = open(anno_filename, 'a')
    for idx, item in enumerate(bboxList):
        x1, y1, x2, y2 = bboxList[idx]
        writer.addObject(str(objectLabelList[idx]), x1, y1, x2, y2)
        annotation_file.write('__Path__' + '/' + 'name.jpg'+ ',' +
                                           ','.join(map(str, bboxList[idx])) + ',' + (objectLabelList[idx])
                                            + '\n')
        annotation_file.close()
        base = os.path.basename(imageDirPathBuffer)
        baseName = os.path.splitext(base)[0]
        save_dir = 'annotations/annotations_voc/'
        save_path = save_dir + baseName + '.xml'
        if(not os.path.exists(save_dir)):
            os.mkdir(save_dir)
        writer.save(save_path)
        writer = None
        print(bboxList)
        print(objectLabelList)
        print(x1)


convert2xml(bboxList, objectLabelList, imageDirPathBuffer)

It generates xml with only first coordinates i.e. (77, 238, 230, 458) where I want to write xml with all the coordinates.

shreesurya avatar Dec 02 '20 13:12 shreesurya

Pretty old, but others who see this, the issue looks like the file is being written within the for-loop, and then the writer is set to None before going on to the next annotation.

Jordan-Pierce avatar Jan 17 '23 17:01 Jordan-Pierce