labelimg作为图像领域目标检测、图像语义分割等领域常见的标注工具,输出的数据格式是Pascal VOC,实际工作中,我们经常需要把其他标注格式为txt的文件转为VOC的xml文件格式,下面的代码提供txt转VOC格式的xml文件的功能。

from xml.dom.minidom import Document
import os
import os.path
from PIL import Imageann_path = "/Users/label_result.txt"
img_path = "/Users/"
xml_path = "/Users/"if not os.path.exists(xml_path):os.mkdir(xml_path)def writeXml(tmp, imgname,imgpath, w, h, label_list, wxml, attributes_list):doc = Document()#ownerannotation = doc.createElement('annotation')doc.appendChild(annotation)#ownerfolder = doc.createElement('folder')annotation.appendChild(folder)folder_txt = doc.createTextNode("R_dataset")folder.appendChild(folder_txt)filename = doc.createElement('filename')annotation.appendChild(filename)filename_txt = doc.createTextNode(imgname)filename.appendChild(filename_txt)path = doc.createElement('path')annotation.appendChild(path)path_txt = doc.createTextNode(imgpath)path.appendChild(path_txt)#ones#source = doc.createElement('source')annotation.appendChild(source)database = doc.createElement('database')source.appendChild(database)database_txt = doc.createTextNode("The R Database")database.appendChild(database_txt)#onee##twos#size = doc.createElement('size')annotation.appendChild(size)width = doc.createElement('width')size.appendChild(width)width_txt = doc.createTextNode(str(w))width.appendChild(width_txt)height = doc.createElement('height')size.appendChild(height)height_txt = doc.createTextNode(str(h))height.appendChild(height_txt)depth = doc.createElement('depth')size.appendChild(depth)depth_txt = doc.createTextNode("3")depth.appendChild(depth_txt)#twoe#segmented = doc.createElement('segmented')annotation.appendChild(segmented)segmented_txt = doc.createTextNode("0")segmented.appendChild(segmented_txt)# attributesattributes = doc.createElement("attributes")annotation.appendChild(attributes)for i in range(0,len(attributes_list)):#threes#attr_new = doc.createElement(attributes_list[i])attributes.appendChild(attr_new)attr_new_txt = doc.createTextNode(label_list[i])attr_new.appendChild(attr_new_txt)#threee#with open(wxml, "w") as f:f.write(doc.toprettyxml(indent = '\t', encoding='utf-8')) f.close()# tempfile = tmp + "test.xml"# with open(tempfile, "w") as f:#     f.write(doc.toprettyxml(indent = '\t', encoding='utf-8'))# rewrite = open(tempfile, "r")# lines = rewrite.read().split('\n')# newlines = lines[1:len(lines)-1]# fw = open(wxml, "w")# for i in range(0, len(newlines)):#     fw.write(newlines[i] + '\n')# fw.close()# rewrite.close()# # os.remove(tempfile)returnattributes_list = ['Female','AgeLess16','Age17-30','Age31-45','BodyFat','BodyNormal','BodyThin'
]# temp = "/Users/data/temp"
# if not os.path.exists(temp):
#    os.mkdir(temp)f = open(ann_path, 'r')
txt_list = f.readlines()
f.close()
im_name_list = []for line in txt_list:line = line.strip()line_split = line.split(' ')img_name = line_split[0].split('/')[-1]im_name_list.append(img_name)label_list = []for j in range(1, len(line_split)):label_list.append(line_split[j])fileimgpath = os.path.join(img_path, img_name)im=Image.open(fileimgpath)  width= int(im.size[0])height= int(im.size[1])# print label_listsavename = os.path.join(xml_path , img_name.split('.')[0] + '.xml')writeXml(temp, img_name,fileimgpath, width, height, label_list, savename,attributes_list)
# os.rmdir(temp)

将txt文件转为xml文件以适配图像标注工具labelimg或Pascal VOC XML 格式相关推荐

  1. Python将txt文件转为json文件

    python将txt文件转为json文件 txt文件中内容: [*]www.xiaoyang.1 [*]www.xiaoyang.12 [*]www.xiaoyang.135 [*]www.xiaoy ...

  2. 批量将txt文件转为excel文件

    python萌新,记录一下,自己做项目时遇到需批量将txt文件转为excel文件. 代码如下: `# coding:utf-8 import pandas as pd import os def ge ...

  3. txt文件转为mat文件

    ** MATLAB .txt文件转为.mat文件** 在近红外光谱matlab数据处理中,我们获得的数据并不都是.mat文件,也有可能是.csv或.txt文件,如何将这些文件转为.mat 文件呢? 以 ...

  4. 深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提取图像信息

    @Author:Runsen 图像标注主要用于创建数据集进行图片的标注.本篇博客将推荐一款非常实用的图片标注工具LabelImg,重点介绍其安装使用过程.如果想简单点,请直接下载打包版(下载地址见结尾 ...

  5. python转csv_python脚本如何将Excel文件转为csv文件(代码)

    本篇文章给大家带来的内容是关于python脚本如何将Excel文件转为csv文件(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助.#!/usr/bin/env python __ ...

  6. 【windwos bat】批量把windows下的wav文件转为raw文件

    借助工具sox,可以批量将wav文件转为raw文件. @echo off set work_path=recordings set raw_path=raw SET mypath=%~dp0mkdir ...

  7. GDCM:png文件转为dcm文件的测试程序

    GDCM:png文件转为dcm文件的测试程序 GDCM:png文件转为dcm文件的测试程序 GDCM:png文件转为dcm文件的测试程序 #include "gdcmImageReader. ...

  8. python csv转excel_将Excel文件转为csv文件的python脚本

    将Excel文件转为csv文件的python脚本 #!/usr/bin/env python __author__ = "lrtao2010" ''' Excel文件转csv文件脚 ...

  9. matlab p文件转码 matlab pcode文件 将matlab中的p文件转为m文件工具

    matlab p文件转码 matlab pcode文件 将matlab中的p文件转为m文件工具 源码可见,解密P ID:22600679158222577美丽小飞侠大队

最新文章

  1. Nginx教程系列四:实现反向代理配置
  2. Liunx 重新mount
  3. 服务器重新部署踩坑记
  4. jQuery常用技巧大放送
  5. 移动办公、企业-移动办公:移动过程中的办公触手可及-by小雨
  6. 谷歌浏览器不兼容小于12px的字体,在做网页的时候怎么解决
  7. 生日python十种日期格式_Python可视化-二十四节气与生日间隔天数统计
  8. 数据库索引系列四:索引算法Hash与BTree的区别
  9. 前端小白案例-爱新鲜抽屉式特效制作
  10. 假期最后一天,出差赶到天津
  11. matlab2017b 破解
  12. java编程新手自学手册_Java Web编程新手自学手册
  13. 什么是边缘计算网关?(边缘计算网关产品的特点?)
  14. excel2010设置列宽为像素_如何以厘米为单位精确设置Excel表格的行高列宽?
  15. 计算机毕设(附源码)JAVA-SSM基于远程协作的汽车故障诊断系统
  16. 计算机符号的名字,空格符号怎么打 游戏名字空格怎么打
  17. 【iOS】Plist-XML-JSON数据解析
  18. 最成功的失败学——《创游记:游戏团队创业成长之路》
  19. DHCP+DHCP中继
  20. 【5】数据结构与算法--- 算法 进阶

热门文章

  1. 【Scratch案例实操】Scratch画正方形 scratch编程案例教学 scratch创意编程 少儿编程教案
  2. Rancher 2.1平台搭建及使用
  3. ubuntu 怎样查看隐藏文件
  4. windows-start-iexplorer
  5. python初级6元组字典集合
  6. 感性理解Word2Vec - 它的神奇以及原理
  7. mui退出登录但不退出软件继续登录,底部选项卡不能使用问题
  8. P2579或JZOJ 2288. 【ZJOI2005】沼泽鳄鱼
  9. 国产龙芯双核64位系统迅为2K开发板应用到工控轨道交通电力能源等领域
  10. 图像分割处理不均衡数据集