Python 如何通过xml获取形状的坐标?

Python 如何通过xml获取形状的坐标?,python,Python,读取鼠标坐标值成功。但是我需要通过xml读取存储的坐标值 值是使用ElementTree检索的。 但是一旦你把它放在一个数组中,坐标的形状是x,y,中间的逗号就阻止了整数转换。它是一个字符串,两端都是撇号,所以不能转换。 请告诉我 2. 0,0 1280,0 1280,720 0,720 625,564 625,0 1280,0 1280,631 将xml.etree.ElementTree作为ET导入 tree=ET.parse('./MapFile/c00101.map') root=t

读取鼠标坐标值成功。但是我需要通过xml读取存储的坐标值

值是使用ElementTree检索的。 但是一旦你把它放在一个数组中,坐标的形状是x,y,中间的逗号就阻止了整数转换。它是一个字符串,两端都是撇号,所以不能转换。 请告诉我


2.
0,0
1280,0
1280,720
0,720
625,564
625,0
1280,0
1280,631
将xml.etree.ElementTree作为ET导入
tree=ET.parse('./MapFile/c00101.map')
root=tree.getroot()
DetectPoint=root.getchildren()[1]
LoiteringPoint=root.getchildren()[2]
IntrusionPoint=root.getchildren()[2]
Ipointvalue=[]
Lpointvalue=[]
Dpointvalue=[]
如果DetectPoint.tag==“DetectArea”:
对于root.findall中的DPoint(“DetectArea/Point”):
Dpointvalue.append(DPoint.text)
如果LoiteringPoint.tag==‘Loitering’:
对于root.findall中的LPoint(“游荡/点”):
Lpointvalue.append(LPoint.text)
elif INTERSIONPOINT.tag==“入侵”:
对于root.findall(“入侵/点”)中的IPoint:
Ipointvalue.append(IPoint.text)
ip=len(Ipointvalue)
lp=len(Lpointvalue)
dp=len(Dpointvalue)
对于范围内的i(dp):
Dpointvalue[i]
打印(Dpointvalue[i])
对于范围内的i(lp):
Lpointvalue[i]
打印(Lpointvalue[i])
对于范围内的i(ip):
Ipointvalue[i]
打印(Ipointvalue[i])
'
'
'
def onMouseCallback(self、event、x、y、flags、idx):
如果self.view\u state==“入侵追加”或self.view\u state==“游荡追加”或self.view\u state==“计数追加”:
如果event==cv2.event\u LBUTTONUP和flags==cv2.event\u FLAG\u LBUTTON:
works[idx].area_tmp.append([x,y])
#打印(工作[idx]。区域\u tmp)
#打印(Dpointvalue)
创建多段线的步骤
我想要的坐标值是x和y,但我想征求意见,因为它被识别为这样的“x,y”。

定义一个“namedtuple”命名点。此对象有两个int属性x和y。有一个帮助器方法可以帮助您将数据(x和y作为字符串)转换为点对象

见下文

from collections import namedtuple

Point = namedtuple('Point','x y')

def make_point(point_str):
    parts = point_str.split(',')
    return Point(int(parts[0]),int(parts[1]))


point_str = '3,4'
point = make_point(point_str)
print(point)
print(point.x)
print(point.y)
输出

Point(x=3, y=4)
3
4
现在,您的代码可能如下所示:

...
Lpointvalue.append(make_point(LPoint.text))
...

一步一步地做。隔离标记内的值。因为它们在第一次提取时是字符串,所以使用逗号拆分它们。这将为您收集字符串列表。将结果列表的每个元素转换为整数。根据需要进行后期处理。问题已解决。谢谢你的建议。我很容易理解。