首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们如何通过xml获得形状的坐标?

我们如何通过xml获得形状的坐标?
EN

Stack Overflow用户
提问于 2019-10-01 16:53:13
回答 1查看 272关注 0票数 0

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

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

代码语言:javascript
复制
<?xml version='1.0' encoding='utf-8'?>
<DA>
    <DetectionAreas>2</DetectionAreas>
    <DetectArea>
        <Point>0,0</Point>
        <Point>1280,0</Point>
        <Point>1280,720</Point>
        <Point>0,720</Point>
    </DetectArea>
    <Loitering>
        <Point>625,564</Point>
        <Point>625,0</Point>
        <Point>1280,0</Point>
        <Point>1280,631</Point>
    </Loitering>
</DA>
代码语言:javascript
复制
import xml.etree.ElementTree as ET

tree = ET.parse('./MapFile/C001101.map')
root = tree.getroot()
DetectPoint = root.getchildren()[1]
LoiteringPoint = root.getchildren()[2] 
IntrusionPoint = root.getchildren()[2]
Ipointvalue = []
Lpointvalue = []
Dpointvalue = []


if DetectPoint.tag == 'DetectArea' :
    for DPoint in root.findall("DetectArea/Point") :
        Dpointvalue.append(DPoint.text)
if LoiteringPoint.tag == 'Loitering' :
    for LPoint in root.findall("Loitering/Point") :        
        Lpointvalue.append(LPoint.text)
elif IntrusionPoint.tag == 'Intrusion' :
    for IPoint in root.findall("Intrusion/Point") :
        Ipointvalue.append(IPoint.text) 

ip = len(Ipointvalue)
lp = len(Lpointvalue)
dp = len(Dpointvalue)

for i in range(dp): 
    Dpointvalue[i]
    print(Dpointvalue[i])
for i in range(lp):
    Lpointvalue[i]
    print(Lpointvalue[i])
for i in range(ip):
    Ipointvalue[i]
    print(Ipointvalue[i])   

'
'
'
    def onMouseCallback(self, event, x, y, flags, idx):
        if self.view_state == 'intrusion append' or self.view_state == 'loitering append' or self.view_state == 'counting append':
            if event == cv2.EVENT_LBUTTONUP and flags == cv2.EVENT_FLAG_LBUTTON:
                works[idx].area_tmp.append([x, y])
                #print(works[idx].area_tmp)
                #print(Dpointvalue)

要创建折线,我想要的坐标值是x和y,但我想请教一下,因为它被识别为'x,y‘。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-01 17:02:28

定义一个名为Point的'namedtuple‘。此对象具有两个int属性x和y。有一个辅助方法可帮助您将所拥有的数据(x和y作为字符串)转换为Point对象

见下文

代码语言:javascript
复制
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)

输出

代码语言:javascript
复制
Point(x=3, y=4)
3
4

现在,您的代码可能如下所示:

代码语言:javascript
复制
...
Lpointvalue.append(make_point(LPoint.text))
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58181255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档