python讀取xml文件中的坐標點


用labelImg工具制作好xml文件后,需要讀取其中img路徑和坐標點,生成一個label.txt

<annotation>
    <folder>big</folder>
    <filename>img_7.jpg</filename>
    <path>E:/FDDB/2003/01/02/big/img_7.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>367</width>
        <height>450</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>face</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>116</xmin>
            <ymin>21</ymin>
            <xmax>220</xmax>
            <ymax>167</ymax>
        </bndbox>
    </object>
</annotation>

python腳本,python2.7,在img的路徑中最好不要加上中文字符,否則提取或者存入txt的時候,會比較麻煩

# coding=utf-8
# 讀xml文件中的一個rect
import xml.etree.ElementTree as ET
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

xml_path='./img_7.xml'
tree = ET.parse(xml_path)
rect={}
line=""
root = tree.getroot()
for name in root.iter('path'):
    rect['path'] = name.text
for ob in root.iter('object'):
    for bndbox in ob.iter('bndbox'):
        # for l in bndbox:
        #     print(l.text)
        for xmin in bndbox.iter('xmin'):
            rect['xmin'] = xmin.text
        for ymin in bndbox.iter('ymin'):
            rect['ymin'] = ymin.text
        for xmax in bndbox.iter('xmax'):
            rect['xmax'] = xmax.text
        for ymax in bndbox.iter('ymax'):
            rect['ymax'] = ymax.text
line = rect['path'] + "\t"+ rect['xmin']+ "\t"+rect['ymin']+"\t"+rect['xmax']+"\t"+rect['ymax']

f1 = open('img_7.txt', 'w')
f1.write(line)

即可將xml中的img路徑和,xmin,ymin,xman,ymax數據寫入到一個txt文檔中。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM