Vaibhav
Vaibhav

Reputation: 71

I have pascal voc dataset with .xml annotation. I want to convert it to dakrnet .txt format. How can i do it?

I am working on a video analytics project. I have a written a code using xml.etree.ElementTree for parsing .xml doc but its somehow not giving accurate results.

I have a written a code using xml.etree.ElementTree in python for parsing .xml doc, extracting values of xmin, xmax, ymin, ymax, width and height of image. After normalisation, appending it in desired format in the text file with same name as that of image. Formula i used for normalisation is:

enter code here
            x = (xmin + xmax)/2. * 1./width
            y = (ymin + ymax)/2. * 1./height
            w = (xmax - xmin) * 1./width
            h = (ymax - ymin) * 1./height

I expect result to be in darknet annotation format as .

Upvotes: 1

Views: 2172

Answers (1)

user9270170
user9270170

Reputation:

You can try my code to see if it can meet your needs.

import os
import glob
import csv
import xml.etree.ElementTree as ET

os.chdir(r'C:\Users\Administrator\Desktop\test')
path = r'C:\Users\Administrator\Desktop\test'

def xml_to_txt(path):
    txt_list = []
    for xml_file in glob.glob(path + '/*.xml'):
        everyrow_xml_list = []
        tree = ET.parse(xml_file)
        root = tree.getroot()
        everyrow_xml_list.append(path + '/' + root.find('filename').text)
        for member in root.findall('object'):
            xmin = str(int(member[4][0].text))
            ymin = str(int(member[4][1].text))
            xmax = str(int(member[4][2].text))
            ymax = str(int(member[4][3].text))
            if xmin=="0":
                xmin="1"
            if ymin=="0":
                ymin="1"
            if xmax=="0":
                xmax="1"
            if ymax=="0":
                ymax="1"
            value = xmin+','+ymin+','+xmax+','+ymax+','+member[0].text
            everyrow_xml_list.append(value)
        txt_list.append(everyrow_xml_list)#image_path x_min,y_min,x_max,y_max,class_id  x_min,y_min,x_max,y_max,class_id ……
    return txt_list



def main():
    image_path = path
    xml2txt_list = xml_to_txt(image_path)
    with open(r'D:/aaa.txt', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f,delimiter=' ')
        writer.writerows(xml2txt_list)
    print('Successfully converted xml to txt.')

if __name__ == '__main__':
    main()

Upvotes: 1

Related Questions