how to denormalise coordinates?

2019-07-29 23:56发布

I am annotating a dataset for computer vision application. I have normalised coordinates(xmin,ymin,xmax,ymax) in form of xml file

The complete xml looks like this:

<annotation>
    <folder>image</folder>
    <filename>100_icdar13.png</filename>
    <path>/Users/image/100_icdar13.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>816</width>
        <height>608</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>192</xmin>
            <ymin>157</ymin>
            <xmax>530</xmax>
            <ymax>223</ymax>
        </bndbox>
    </object>
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>561</xmin>
            <ymin>159</ymin>
            <xmax>645</xmax>
            <ymax>219</ymax>
        </bndbox>
    </object>
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>74</xmin>
            <ymin>247</ymin>
            <xmax>465</xmax>
            <ymax>311</ymax>
        </bndbox>
    </object>
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>493</xmin>
            <ymin>255</ymin>
            <xmax>625</xmax>
            <ymax>305</ymax>
        </bndbox>
    </object>
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>85</xmin>
            <ymin>339</ymin>
            <xmax>496</xmax>
            <ymax>400</ymax>
        </bndbox>
    </object>
</annotation>

I want to denormalise this dataset and export all the boxes it in following format

x1, y1, x2, y2, x3, y3, x4, y4, text

How do I do this, what algorithm can I use to achieve this?

2条回答
趁早两清
2楼-- · 2019-07-30 00:39

You can use ElementTree to parse XML and extract coordinates:

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element

xml_raw = '''
<annotation>
    ...
    <object>
        <name>text</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>192</xmin>
            <ymin>157</ymin>
            <xmax>530</xmax>
            <ymax>223</ymax>
        </bndbox>
    </object>
    <object>
        ...
    </object>
    ...
</annotation>
'''
if __name__ == '__main__':
    root: Element = ET.fromstring(xml_raw)
    for obj in root.findall('object'):
        bndbox: Element = obj.find('bndbox')

        name = obj.find('name').text
        xmin, xmax, ymin, ymax = [int(bndbox.find(x).text) for x in ['xmin', 'xmax', 'ymin', 'ymax']]
        coords = [(x, y) for x in [xmin, xmax] for y in [ymin, ymax]]
        print(name, coords)

which outputs:

text [(192, 157), (192, 223), (530, 157), (530, 223)]
text [(561, 159), (561, 219), (645, 159), (645, 219)]
text [(74, 247), (74, 311), (465, 247), (465, 311)]
text [(493, 255), (493, 305), (625, 255), (625, 305)]
text [(85, 339), (85, 400), (496, 339), (496, 400)]
查看更多
老娘就宠你
3楼-- · 2019-07-30 00:51

This is the answer :

import xml.etree.ElementTree as ET
import os
import glob
import shutil

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element


with open('100_icdar13.xml') as f: root = ET.parse(f)
for obj in root.findall('object'):
    bndbox: Element = obj.find('bndbox')
    name = obj.find('name').text
    xmin, xmax, ymin, ymax = [int(bndbox.find(x).text) for x in ['xmin', 'xmax', 'ymin', 'ymax']]
    coords = [(x, y) for x in [xmin, xmax] for y in [ymin, ymax]]
    print(coords, name)

output:

[(201, 162), (201, 229), (207, 162), (207, 229)] text
[(208, 162), (208, 229), (223, 162), (223, 229)] text
[(224, 162), (224, 229), (239, 162), (239, 229)] text
[(493, 255), (493, 305), (625, 255), (625, 305)] text
[(85, 339), (85, 400), (496, 339), (496, 400)] text
查看更多
登录 后发表回答