用于查找对象并在找到对象后停止的控制流



我有一个相机装备,在那里我初始化舞台,然后需要移动相机,找到可以检测到物体的范围。我无法预测物体的位置。如果相机没有检测到物体,我会增加相机的级数,然后再看。当我找到对象开始可检测的位置时,我会将当前相机位置附加到列表中。我在整个范围内重复这个。我想做的是,一旦一个物体不再在视野中,即一旦它停止被发现,就停止不必要的寻找尝试。我想到了一个列表,它可能读起来像:y_list=[1000150200250300,…500],我不知道如何检查列表的长度是否在for循环的几次迭代中停止了增长。我想用另一个列表来显示何时检测到对象,但不知道如何实现

y_list_flags=[0,0,0,0,0,1,1,1,10,1,1,1,1,0]

代码

y_list = []
len_y = len(y_list)
for i in list(range(1,70):
my_obj = obj_present()#returns True if object detected, False otherwise
if my_obj:
y_list.append(current_cam_position)
move_cam_stage()
elif my_obj not True:
move_cam_stage()

所需输出

y_list = [100,150,200,250,300, 350,400,450,500,550,600] # list stops growing when object not found and test has stopped

y_list = [100,150,200,250,300, 350,400,450,500,550,600] # list stops growing when object not found and test has stopped a few attempts after drop is no longer found

move_cam_stageobj_present是伪函数。

当再次找不到对象时,循环中断和列表停止增长。

代码:

def move_cam_stage():
print("move cam stage")
def obj_present(i):
y_list_flags = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
return y_list_flags[i] == 1
y_list = []
for i in range(1,70):
my_obj = obj_present(i)#returns True if object detected, False otherwise
if my_obj:
y_list.append(50 + 50*i)
move_cam_stage()
else:
if len(y_list)>1:
break
print(y_list)

结果:

move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
move cam stage
[450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950]

最新更新