AndrewCarterUK / pascal-voc-writer

A python library for generating annotations in the PASCAL VOC format.
MIT License
220 stars 57 forks source link

writer.addObject is not working #7

Open shreesurya opened 3 years ago

shreesurya commented 3 years ago
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.

Jordan-Pierce commented 1 year ago

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.