• Python-目标检测-将xml文件转换成.txt文件


    代码说明:labels文件夹是工程下的一个文件夹,里面存放的是一些xml文件。
    然后我们将这些xml文件中的内容取出来,放在路径path1的文件名下。这样也就完成了xml文件到txt文件的转化。
    该代码用到了两个包,pathlib以及xml.etree.cElementTree。文档的后面会对这两个包的一些基本使用做一些说明。
    实现效果:
    首先看一下xml文件:
    '''

    <annotation>
    <folder>VOC2012</folder>
    <filename>2007_000027.jpg</filename>
    <source>
    	<database>The VOC2007 Database</database>
    	<annotation>PASCAL VOC2007</annotation>
    	<image>flickr</image>
    </source>
    <size>
    	<width>486</width>
    	<height>500</height>
    	<depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
    	<name>person</name>
    	<pose>Unspecified</pose>
    	<truncated>0</truncated>
    	<difficult>0</difficult>
    	<bndbox>
    		<xmin>174</xmin>
    		<ymin>101</ymin>
    		<xmax>349</xmax>
    		<ymax>351</ymax>
    	</bndbox>
    	<part>
    		<name>head</name>
    		<bndbox>
    			<xmin>169</xmin>
    			<ymin>104</ymin>
    			<xmax>209</xmax>
    			<ymax>146</ymax>
    		</bndbox>
    	</part>
    	<part>
    		<name>hand</name>
    		<bndbox>
    			<xmin>278</xmin>
    			<ymin>210</ymin>
    			<xmax>297</xmax>
    			<ymax>233</ymax>
    		</bndbox>
    	</part>
    	<part>
    		<name>foot</name>
    		<bndbox>
    			<xmin>273</xmin>
    			<ymin>333</ymin>
    			<xmax>297</xmax>
    			<ymax>354</ymax>
    		</bndbox>
    	</part>
    	<part>
    		<name>foot</name>
    		<bndbox>
    			<xmin>319</xmin>
    			<ymin>307</ymin>
    			<xmax>340</xmax>
    			<ymax>326</ymax>
    		</bndbox>
    	</part>
    </object>
    
    转换后的txt: '''
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000027.jpg 174,101,349,351,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000032.jpg 104,78,375,183,0 133,88,197,123,0 195,180,213,229,1 26,189,44,238,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000033.jpg 9,107,499,263,0 421,200,482,226,0 325,188,411,223,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000039.jpg 156,89,344,279,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000042.jpg 263,32,500,295,0 1,36,235,299,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000061.jpg 274,11,437,279,0 184,214,281,252,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000063.jpg 123,115,379,275,0 75,1,428,375,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000068.jpg 27,45,266,375,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000121.jpg 251,28,475,267,0 22,28,251,273,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000123.jpg 1,26,358,340,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000129.jpg 70,202,255,500,0 251,242,334,500,0 1,144,67,436,0 1,1,66,363,1     74,1,272,462,1 252,19,334,487,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000170.jpg 87,100,109,165,0 41,114,73,181,0 324,148,352,206,0 426,157,443,195,0     3,91,43,206,1 4,28,461,372,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000175.jpg 25,34,419,271,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000187.jpg 1,95,240,336,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000241.jpg 356,183,500,280,0 60,109,142,213,0 246,134,348,217,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000243.jpg 181,127,274,193,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000250.jpg 1,170,474,375,0 97,124,150,297,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000256.jpg 8,96,491,232,0
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000272.jpg 25,71,304,500,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000323.jpg 277,3,500,375,1 12,3,305,375,1
    F:Object detectionYunYang1994-tensorflow-yolov3-master2007_000332.jpg 54,50,285,262,0
    

    代码:
    '''

    from pathlib import Path
    import os
    
    try:
      import xml.etree.cElementTree as ET
    except ImportError:
      import xml.etree.ElementTree as ET
    import sys
    path = Path('F:/Object detection/YunYang1994-tensorflow-yolov3-master/labels')
    file_list=[]
    for files in path.rglob('*.xml'):
        file_list.append(files)#存进file_list
    
    for  file in file_list:#循环文件
        tree = ET.parse(file)  #打开xml文档
        root = tree.getroot()  #获得root节点
        print("*"*10)
        filename = root.find('filename').text
        filename = filename[:-4]
        path1 = "voc3.txt"
        with open(path1, "a") as f:  # "a"用于追加内容
    
        f.write(os.path.abspath(filename + '.jpg'))#写入路径
        for object in root.findall('object'):#找到root节点下的所有object节点,写入坐标信息
          name = object.find('name').text   #子节点下节点name的值
          bndbox = object.find('bndbox')    #子节点下属性bndbox的值
          #坐标值
          xmin = bndbox.find('xmin').text
          ymin = bndbox.find('ymin').text
          xmax = bndbox.find('xmax').text
          ymax = bndbox.find('ymax').text
    
          if name==("person"):#
              class_id=1
          else:
              class_id=0
          with open(path1,"a") as f:#"a"用于追加内容
            f.write(' '+str(xmin)+','+str(ymin)+','+str(xmax)+','+str(ymax)+','+str(class_id))
        with open(path1, "a") as f:
          f.write('
    ')
        f.close()
    学习让我快乐,工作让我快乐。学习和工作都是为了更好的生活!
  • 相关阅读:
    Remmina:一个 Linux 下功能丰富的远程桌面共享工具(转载)
    Linux安装多功能词典GoldenDict
    Ubuntu18.04 更改GRUB引导菜单背景图片和默认启动项
    ubuntu 18.04网卡命名规则改回传统的ethx
    Linux Putty 复制粘贴
    Ubuntu 18.04图形化软件包管理器
    Ubuntu 18.04 安装 Broadcom Limited BCM43142无线网卡驱动
    Linux下通过rdesktop连接Windows远程桌面
    Debian9.5 系统配置NFS配置说明
    CentOS6.9下NFS配置说明
  • 原文地址:https://www.cnblogs.com/xyuanzi/p/14525494.html
Copyright © 2020-2023  润新知