Python 如何去规范化坐标?
我正在为计算机视觉应用程序注释数据集。我有xml文件形式的标准化坐标(xmin、ymin、xmax、ymax) 完整的xml如下所示:Python 如何去规范化坐标?,python,tensorflow,machine-learning,computer-vision,object-detection,Python,Tensorflow,Machine Learning,Computer Vision,Object Detection,我正在为计算机视觉应用程序注释数据集。我有xml文件形式的标准化坐标(xmin、ymin、xmax、ymax) 完整的xml如下所示: <annotation> <folder>image</folder> <filename>100_icdar13.png</filename> <path>/Users/image/100_icdar13.png</path> <sourc
<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>
我如何做到这一点,我可以使用什么算法来实现这一点?您可以使用
ElementTree
解析XML并提取坐标:
将xml.etree.ElementTree作为ET导入
从xml.etree.ElementTree导入元素
xml_raw=''
...
文本
未指明
0
0
192
157
530
223
...
...
'''
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root:Element=ET.fromstring(xml\u原始)
对于root.findall('object')中的obj:
bndbox:Element=obj.find('bndbox')
name=obj.find('name')。文本
xmin,xmax,ymin,ymax=[int(bndbox.find(x).text)表示['xmin','xmax','ymin','ymax']
坐标=[(x,y)表示[xmin,xmax]中的x,表示[ymin,ymax]中的y]
打印(名称、坐标)
哪些产出:
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)]
答案是:
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)
输出:
[(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
当我运行脚本时,可能会出现此错误的重复:文件“denormalise.py”,第30行,在name=obj.find('name')。text AttributeError:'NoneType'对象没有属性'text',在
x1,y1,x2,y2,x3,y3,x4,y4,text
中的text
字段从哪里获取?我假设它来自annotation>object>name
。您可能必须修改obj.find('name')。text根据您的假设是正确的,您创建的代码是否必须有“”…“”或者我应该用xml_raw=open(“demofile.xml”,“r”)替换它吗?你可以使用和open('raw_data.xml')作为f:root=ET.parse(f)
而不是当我使用xml_raw=open(“demofile.xml”,“r”)时,我得到了错误。TypeError:需要类似字节的对象,而不是“\u io.TextIOWrapper”
[(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