gpt4 book ai didi

python - 如何对坐标进行非规范化?

转载 作者:行者123 更新时间:2023-11-30 09:04:09 24 4
gpt4 key购买 nike

我正在注释计算机视觉应用程序的数据集。我以 xml 文件的形式标准化了坐标(xmin,ymin,xmax,ymax)

完整的 xml 如下所示:

<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>

我想对该数据集进行非规范化并以以下格式导出所有框

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

我该如何做到这一点,我可以使用什么算法来实现这一目标?

最佳答案

您可以使用ElementTree来解析XML并提取坐标:

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)

输出:

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)]

关于python - 如何对坐标进行非规范化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com