LoginSignup
16
15

More than 5 years have passed since last update.

Pascal VOC形式のxmlファイルをPythonで読む

Last updated at Posted at 2018-12-09

Pascal VOC形式のxmlファイルをPythonで読む

はじめに


ツールを利用してアノテーションをおこなったはいいが、さぁ物体検知を試そうという段階になって
想定しているフォーマットが微妙に違い加工が必要なケースは結構あるかと思います。

ということで、Pascal VOC形式のxmlファイルを読んで必要な情報を取り出すようにしました。

xmlファイル


以下のようなものです。アノテーション作業はlabelImgを利用しました。
ペットボトル2本と缶の位置を記録しています。

IMG_1542.xml
<annotation>
    <folder>181208</folder>
    <filename>IMG_1542.JPG</filename>
    <path>/181208/IMG_1542.JPG</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>4032</width>
        <height>3024</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>petbottle</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>733</xmin>
            <ymin>507</ymin>
            <xmax>1383</xmax>
            <ymax>2232</ymax>
        </bndbox>
    </object>
    <object>
        <name>can</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>1608</xmin>
            <ymin>1412</ymin>
            <xmax>2028</xmax>
            <ymax>2292</ymax>
        </bndbox>
    </object>
    <object>
        <name>petbottle</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>2548</xmin>
            <ymin>547</ymin>
            <xmax>3143</xmax>
            <ymax>2467</ymax>
        </bndbox>
    </object>
</annotation>

Pythonで情報の取得


以下の処理では、all_listに画像ファイル名と、クラスと位置の情報を格納しています。

# coding:utf-8

import xml.etree.ElementTree as ET

FILE = 'IMG_1542.xml'

file = open(FILE)
tree = ET.parse(file)
root = tree.getroot()

all_list = []

img_file = root.find('filename').text  # 画像ファイル名を取得

for obj in root.iter('object'):
    cls = obj.find('name').text
    xmlbox = obj.find('bndbox')
    b = [int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text)]

    all_list.append([img_file] + b + [cls])

all_listは以下のようになります。

[['IMG_1542.JPG', 733, 507, 1383, 2232, 'petbottle'],
 ['IMG_1542.JPG', 1608, 1412, 2028, 2292, 'can'],
 ['IMG_1542.JPG', 2548, 547, 3143, 2467, 'petbottle']]
16
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
15