1

I am working on object detection with yolo (darkflow). I have a dataset and the labels for the dataset. But the problem is that, I require the annotations in XML format but I have it in .txt format. How do I convert txt to xml?

This is a sample txt file.

001.jpg 528 412 162 52  001

I wrote some code to convert txt to xml

import os
import xml.etree.cElementTree as ET
from lxml import etree
import cv2

def text_to_xml(file):

    filename = file.split('.')[0]
    extention = file.split('.')[1]

    image = cv2.imread(os.path.join(folder, file))
    height, width, depth = image.shape
    f = open(filename+'.txt', 'r')
    content = f.readlines()[0]
    content = content.split('\t')
    xmin, xmax, ymin, ymax = content[1], content[2], content[3], content[4]
    f.close()

    annotation = ET.Element('annotation')
    ET.SubElement(annotation, 'folder').text = folder
    ET.SubElement(annotation, 'filename').text = filename + '.jpg'
    ET.SubElement(annotation, 'segmented').text = '0'

    size = ET.SubElement(annotation, 'size')
    ET.SubElement(size, 'width').text = str(width)
    ET.SubElement(size, 'height').text = str(height)
    ET.SubElement(size, 'depth').text = str(depth)

    ob = ET.SubElement(annotation, 'object')
    ET.SubElement(ob, 'name').text = 'sign'
    ET.SubElement(ob, 'pose').text = 'Unspecified'
    ET.SubElement(ob, 'truncated').text = '0'
    ET.SubElement(ob, 'difficult').text = '0'

    bbox = ET.SubElement(ob, 'bndbox')
    ET.SubElement(bbox, 'xmin').text = str(xmin)
    ET.SubElement(bbox, 'ymin').text = str(ymin)
    ET.SubElement(bbox, 'xmax').text = str(xmax)
    ET.SubElement(bbox, 'ymax').text = str(ymax)

    xml_str = ET.tostring(annotation)
    root = etree.fromstring(xml_str)
    xml_str = etree.tostring(root, pretty_print=True)

    return xml_str

if __name__ == '__main__':
    folder = './images/'
    for file in os.listdir(folder):
        if file.split('.')[1] == 'jpg':
            text_to_xml(file)

But when I train the model with the converted xml files, the model does not detect anything. So I guess I messed up somewhere.

Sashaank
  • 880
  • 2
  • 20
  • 54

0 Answers0