我正在使用 yolo (darkflow) 进行对象检测。我有一个数据集和数据集的标签。但问题是,我需要 XML 格式的注释,但我有 .txt 格式的注释。如何将txt转换为xml?
这是一个示例 txt 文件。
001.jpg 528 412 162 52 001
我写了一些代码来将txt转换为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)
但是当我用转换后的 xml 文件训练模型时,模型没有检测到任何东西。所以我想我在某个地方搞砸了。