目标检测:把标注文件txt格式转换为xml格式

编程入门 行业动态 更新时间:2024-10-25 10:21:16

目标检测:把标注文件txt<a href=https://www.elefans.com/category/jswz/34/1771175.html style=格式转换为xml格式"/>

目标检测:把标注文件txt格式转换为xml格式

txt_to_xml程序如下:

# -*- coding: utf-8 -*-
import os,shutil
import cv2
from lxml.etree import Element, SubElement, tostring
def txt_xml(img_path,img_name,txt_path,img_txt,xml_path,img_xml):#读取txt的信息clas=[]img=cv2.imread(os.path.join(img_path,img_name))imh, imw = img.shape[0:2]txt_img=os.path.join(txt_path,img_txt)with open(txt_img,"r") as f:next(f)for line in f.readlines():line = line.strip('\n')list = line.split(" ")# print(list)clas.append(list)node_root = Element('annotation')node_folder = SubElement(node_root, 'folder')node_folder.text = '1'node_filename = SubElement(node_root, 'filename')#图像名称node_filename.text = img_namenode_size = SubElement(node_root, 'size')node_width = SubElement(node_size, 'width')node_width.text = str(imw)node_height = SubElement(node_size, 'height')node_height.text = str(imh)node_depth = SubElement(node_size, 'depth')node_depth.text = '3'for i in range(len(clas)):node_object = SubElement(node_root, 'object')node_name = SubElement(node_object, 'name')node_name.text = str(clas[i][0])node_pose=SubElement(node_object, 'pose')node_pose.text="Unspecified"node_truncated=SubElement(node_object, 'truncated')node_truncated.text="truncated"node_difficult = SubElement(node_object, 'difficult')node_difficult.text = '0'node_bndbox = SubElement(node_object, 'bndbox')node_xmin = SubElement(node_bndbox, 'xmin')node_xmin.text = str(clas[i][1])node_ymin = SubElement(node_bndbox, 'ymin')node_ymin.text = str(clas[i][2])node_xmax = SubElement(node_bndbox, 'xmax')node_xmax.text = str(int(clas[i][1])+int(clas[i][3]))node_ymax = SubElement(node_bndbox, 'ymax')node_ymax.text = str(int(clas[i][2])+int(clas[i][4]))xml = tostring(node_root, pretty_print=True)  # 格式化显示,该换行的换行img_newxml = os.path.join(xml_path, img_xml)file_object = open(img_newxml, 'wb')file_object.write(xml)file_object.close()if __name__ == "__main__":#图像文件夹所在位置img_path = r"C:\Users\An\Desktop\Exclusively-Dark-Image-Dataset-master\Groundtruth\img\test"#标注文件夹所在位置txt_path=r"C:\Users\An\Desktop\Exclusively-Dark-Image-Dataset-master\Groundtruth\ann\test"#txt转化成xml格式后存放的文件夹xml_path=r"C:\Users\An\Desktop\Exclusively-Dark-Image-Dataset-master\xml"for img_name in os.listdir(img_path):print(img_name)img_xml=img_name.split(".")[0]+".xml"img_txt=img_name+".txt"txt_xml(img_path, img_name, txt_path, img_txt,xml_path, img_xml)

1.图像和标注文件对应关系:

2.标注文件的格式如下:

3.生成的xml文件格式如下:


4.图像用labelImg打开的结果:

更多推荐

目标检测:把标注文件txt格式转换为xml格式

本文发布于:2024-03-09 10:41:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1724753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   转换为   目标   文件   txt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!