如何为我的micropython代码创建一个转义路径?



我正在micropython中使用openMV相机和blob检测来确定对象的方向。我的问题是,当执行检查时,我得到一个错误"ary未定义",因为对象尚未在相机视图中(在输送机上移动)。我如何在代码中实现一个路径,不执行检查,只是打印没有对象,然后再次开始循环并检查对象?我曾试图实现一个中断,如果否则,但似乎不能得到正确的代码。

"

import sensor, image, time, math
from pyb import UART
sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
#sensor.set_auto_gain(False) # must be turned off for color tracking
#sensor.set_auto_whitebal(False) # must be turned off for color tracking
threshold_seed = (7,24,-8,4,-3,9)
threshold_aril = (33,76,-14,6,17,69)
threshold_raphe = (36,45,28,43,17,34)
thresholds = [threshold_seed,threshold_aril,threshold_raphe]
clock = time.clock()                # Create a clock object to track the FPS.
uart = UART(3, 9600)
arilY = None
seedY = None

def func_pass():
result = "Pass"
print(result)
print("%dn"%aril.cx(), end='')
uart.write(result)
uart.write("%dn"%aril.cx())
#these two functions print info to serial monitor and send
def func_fail():
result = "Fail"
print(result)
print("%dn"%aril.cx(), end='')
uart.write(result)
uart.write("%dn"%aril.cx())

def func_orientation(seedY, arilY):
if (seedY and arilY):
check = 0
check = (seedY - arilY)
if
func_pass()
else:
func_fail()

while(True):                        #draw 3 blobs for each fruit
clock.tick()
img = sensor.snapshot()
for seed in img.find_blobs([threshold_seed], pixels_threshold=200, area_threshold=200, merge=True):
img.draw_rectangle(seed[0:4])
img.draw_cross(seed.cx(), seed.cy())
img.draw_string(seed.x()+2,seed.y()+2,"seed")
seedY = seed.cy()
for aril in img.find_blobs([threshold_aril],pixels_threshold=300,area_threshold=300, merge=True):
img.draw_rectangle(aril[0:4])
img.draw_cross(aril.cx(),aril.cy())
img.draw_string(aril.x()+2,aril.y()+2,"aril")
arilY = aril.cy()
for raphe in img.find_blobs([threshold_raphe],pixels_threshold=300,area_threshold=300, merge=True):
img.draw_rectangle(raphe[0:4])
img.draw_cross(raphe.cx(),raphe.cy())
img.draw_string(raphe.x()+2,raphe.y()+2,"raphe")
rapheY = raphe.cy()

func_orientation(seedY, arilY);












您可以在while循环之前先将ary和SeedY定义为None,然后将检查包含在if(arilY and seedY):

中如果您想避免使用None,您可以有一个额外的布尔值,当检测到ary时将其设置为true,然后将检查包含在该布尔值

的测试中。但是这里更大的问题是,为什么你的分配在内循环中?每次循环迭代都要重新定义seedY和ary,这意味着它总是等于seed。列表中最后一个种子的Y,这意味着在最后一个种子之前的所有分配都是无用的。

如果您将分配移出循环,应该不会有问题。

最新更新