簡體   English   中英

我們如何通過xml得到形狀的坐標?

[英]How do we get the coordinates of the shape through xml?

讀取鼠標坐標值成功。 但是我需要通過xml讀取存儲的坐標值。

使用 ElementTree 檢索值。 但是一旦你把它放在一個數組中,坐標的形狀就是 x,y,所以中間的逗號會阻止 integer 轉換。 而且它是一個字符串,所以兩端都是撇號,所以不能轉換。 請給我提意見。

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

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

定義一個名為 Point 的“namedtuple”。 這個 object 有兩個 int 屬性 x 和 y。 有一個輔助方法可以幫助您將您擁有的數據(x 和 y 作為字符串)轉換為點 object

見下文

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)

output

Point(x=3, y=4)
3
4

現在您的代碼可能如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM