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

阅读: 评论:0

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

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

txt_to_xml程序如下:

# -*- coding: utf-8 -*-
import os,shutil
import cv2
 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 adlines():line = line.strip('n')list = line.split(" ")# print(list)clas.append(list)node_root = Element('annotation')node_folder = SubElement(node_root, 'folder') = '1'node_filename = SubElement(node_root, 'filename')#图像名称 = img_namenode_size = SubElement(node_root, 'size')node_width = SubElement(node_size, 'width') = str(imw)node_height = SubElement(node_size, 'height') = str(imh)node_depth = SubElement(node_size, 'depth') = '3'for i in range(len(clas)):node_object = SubElement(node_root, 'object')node_name = SubElement(node_object, 'name') = str(clas[i][0])node_pose=SubElement(node_object, 'pose')="Unspecified"node_truncated=SubElement(node_object, 'truncated')="truncated"node_difficult = SubElement(node_object, 'difficult') = '0'node_bndbox = SubElement(node_object, 'bndbox')node_xmin = SubElement(node_bndbox, 'xmin') = str(clas[i][1])node_ymin = SubElement(node_bndbox, 'ymin') = str(clas[i][2])node_xmax = SubElement(node_bndbox, 'xmax') = str(int(clas[i][1])+int(clas[i][3]))node_ymax = SubElement(node_bndbox, 'ymax') = 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:UsersAnDesktopExclusively-Dark-Image-Dataset-masterGroundtruthimgtest"#标注文件夹所在位置txt_path=r"C:UsersAnDesktopExclusively-Dark-Image-Dataset-masterGroundtruthanntest"#txt转化成xml格式后存放的文件夹xml_path=r"C:UsersAnDesktopExclusively-Dark-Image-Dataset-masterxml"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打开的结果:

本文发布于:2024-03-09 10:41:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1710226447135525.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:格式   转换为   目标   文件   txt
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23