用于传入 UDP 提要的循环
For Loop for incoming UDP feed
我想知道是否有人可以帮我用伪代码解决这个问题。
问题:
我有一个来自本地主机、端口 3333 的实时提要。正在使用传入的 x 和 y 位置、跟踪的对象编号、运动和加速度信息更新提要数据。
要获得方向,我需要知道物体之前的位置,然后将其与当前位置进行比较。
问题:
在当前传入的 Feed 与来自同一 Feed 的先前位置之间设置比较的简单方法是什么?是否设置了一个函数并通过 For 循环访问它来存储位置?
代码:
import tuio, math
print "tuio was imported"
tracking = tuio.Tracking()
print "tuio.Tracking variable assigned"
print "loaded profiles:", tracking.profiles.keys()
print "list functions to access tracked objects:", tracking.get_helpers()
print "Waiting for incoming data", str(tracking.host) + ":" + str(tracking.port)
try:
while 1:
tracking.update()
for cur in tracking.cursors():
#print cur.xpos, "this is xpos"
#print cur.ypos, "this is ypos"
y2, x2 = cur.ypos, cur.xpos
#print y2, x2, cur.sessionid
[For Loop, call a function to store the previous locations?]
except KeyboardInterrupt:
tracking.stop()
一旦我弄清楚如何循环播放直播,方向部分就很容易了。
如果我对问题的理解正确,你为什么不为旧位置创建一个元组列表并使用 in
检查新位置是否在列表中。如果需要,您可以使用 for 循环来比较特定值。
例如(使用变量):
prev_vars = []
while True:
tracking.update()
x,y = cur.xpos,cur.ypos
if (x,y) in prev_vars:
pass # use this if you only want to see if the object has been there
for pos in prev_vars:
#Use this if you want to compare each old pos to the new
do_something_with_each_old_x(pos[0])
do_something_with_each_old_y(pos[1])
prev_vars.append((x,y))#add the new pos to the list
希望对您有所帮助。
tracking.objects() 中的 tuio obj 是否具有运动矢量(.xmot 和 .ymot)的属性?
如果没有,我想你需要这样的东西:
# dictionary to hold previous data about tuio objects
# will be keyed by tuio object sessionid
previous = {}
try:
while 1:
tracking.update()
for obj in tracking.objects():
prev = previous.get(obj.sessionid, None)
if prev:
# there was previous data, do your processing here
# for example:
dx = obj.xpos - prev.xpos
dy = obj.ypos - prev.xpos
# save current data
previous[curr.sessionid] = obj
请注意,您需要定期从字典中清除旧数据(例如,在某个时间间隔内未看到的对象)
我想出了一个不同的方法来做到这一点。上述答案的问题在于 prev.xpos 和 prev.ypos 的前一个变量是它们被写入内存中的相同位置。相反,我将值分开了。它似乎工作得更好。仍然感谢您的帮助。真的很有帮助!
import tuio, math
print "tuio was imported"
tracking = tuio.Tracking()
print "tuio.Tracking variable assigned"
print "loaded profiles:", tracking.profiles.keys()
print "list functions to access tracked objects:", tracking.get_helpers()
print "Waiting for incoming data", str(tracking.host) + ":" + str(tracking.port)
previous = None
px = 0
cx = 0
py = 0
cy = 0
try:
while 1:
tracking.update()
#print tracking.update(), "this is tracking update"
for cur in tracking.cursors():
#print the location of object, current and previous
#print id(cur), id(previous)
cx = cur.xpos
cy = cur.ypos
if previous:
if cx != px or cy != py:
#uncomment to find space
#print "!"*50
dx = cx - px
dy = cy - py
degs = math.atan2(dx, dy)/math.pi*180
print degs, "degs"
previous = cur
px = cur.xpos
py = cur.ypos
except KeyboardInterrupt:
tracking.stop()
我想知道是否有人可以帮我用伪代码解决这个问题。
问题:
我有一个来自本地主机、端口 3333 的实时提要。正在使用传入的 x 和 y 位置、跟踪的对象编号、运动和加速度信息更新提要数据。
要获得方向,我需要知道物体之前的位置,然后将其与当前位置进行比较。
问题:
在当前传入的 Feed 与来自同一 Feed 的先前位置之间设置比较的简单方法是什么?是否设置了一个函数并通过 For 循环访问它来存储位置?
代码:
import tuio, math
print "tuio was imported"
tracking = tuio.Tracking()
print "tuio.Tracking variable assigned"
print "loaded profiles:", tracking.profiles.keys()
print "list functions to access tracked objects:", tracking.get_helpers()
print "Waiting for incoming data", str(tracking.host) + ":" + str(tracking.port)
try:
while 1:
tracking.update()
for cur in tracking.cursors():
#print cur.xpos, "this is xpos"
#print cur.ypos, "this is ypos"
y2, x2 = cur.ypos, cur.xpos
#print y2, x2, cur.sessionid
[For Loop, call a function to store the previous locations?]
except KeyboardInterrupt:
tracking.stop()
一旦我弄清楚如何循环播放直播,方向部分就很容易了。
如果我对问题的理解正确,你为什么不为旧位置创建一个元组列表并使用 in
检查新位置是否在列表中。如果需要,您可以使用 for 循环来比较特定值。
例如(使用变量):
prev_vars = []
while True:
tracking.update()
x,y = cur.xpos,cur.ypos
if (x,y) in prev_vars:
pass # use this if you only want to see if the object has been there
for pos in prev_vars:
#Use this if you want to compare each old pos to the new
do_something_with_each_old_x(pos[0])
do_something_with_each_old_y(pos[1])
prev_vars.append((x,y))#add the new pos to the list
希望对您有所帮助。
tracking.objects() 中的 tuio obj 是否具有运动矢量(.xmot 和 .ymot)的属性?
如果没有,我想你需要这样的东西:
# dictionary to hold previous data about tuio objects
# will be keyed by tuio object sessionid
previous = {}
try:
while 1:
tracking.update()
for obj in tracking.objects():
prev = previous.get(obj.sessionid, None)
if prev:
# there was previous data, do your processing here
# for example:
dx = obj.xpos - prev.xpos
dy = obj.ypos - prev.xpos
# save current data
previous[curr.sessionid] = obj
请注意,您需要定期从字典中清除旧数据(例如,在某个时间间隔内未看到的对象)
我想出了一个不同的方法来做到这一点。上述答案的问题在于 prev.xpos 和 prev.ypos 的前一个变量是它们被写入内存中的相同位置。相反,我将值分开了。它似乎工作得更好。仍然感谢您的帮助。真的很有帮助!
import tuio, math
print "tuio was imported"
tracking = tuio.Tracking()
print "tuio.Tracking variable assigned"
print "loaded profiles:", tracking.profiles.keys()
print "list functions to access tracked objects:", tracking.get_helpers()
print "Waiting for incoming data", str(tracking.host) + ":" + str(tracking.port)
previous = None
px = 0
cx = 0
py = 0
cy = 0
try:
while 1:
tracking.update()
#print tracking.update(), "this is tracking update"
for cur in tracking.cursors():
#print the location of object, current and previous
#print id(cur), id(previous)
cx = cur.xpos
cy = cur.ypos
if previous:
if cx != px or cy != py:
#uncomment to find space
#print "!"*50
dx = cx - px
dy = cy - py
degs = math.atan2(dx, dy)/math.pi*180
print degs, "degs"
previous = cur
px = cur.xpos
py = cur.ypos
except KeyboardInterrupt:
tracking.stop()