我正在使用openMV摄像机和blob检测来确定对象的方向,在micropython中进行一个项目。我的问题是,当执行检查时,我会得到一个错误“ArilY未定义”,因为对象还没有在摄像机视图中(在传送器上移动)。如何实现代码中的路径,使其不执行检查,只需打印没有对象,然后再次开始循环并检查对象?我已经尝试实现了一个中断,如果其他,但似乎不能正确的代码。
“”“
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("%d\n"%aril.cx(), end='')
uart.write(result)
uart.write("%d\n"%aril.cx())
#these two functions print info to serial monitor and send
def func_fail():
result = "Fail"
print(result)
print("%d\n"%aril.cx(), end='')
uart.write(result)
uart.write("%d\n"%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);发布于 2022-06-27 23:03:11
您可以做的事情是在while循环之前先将arilY和SeedY定义为None,然后将检查包含在if(arilY and seedY):中。
如果要避免使用None,则可以在检测到arilY时设置为true的额外布尔值,然后将检查包含在此布尔值的测试中。
但更大的问题是,为什么您的分配处于内部循环中?您总是为循环的每一次迭代重新定义seedY和arilY,这意味着它总是等于列表中最后一个种子的种子.y,这意味着在最后一个循环之前的所有分配都是无用的。
如果将分配移到循环之外,就不会出现问题。
https://stackoverflow.com/questions/72778937
复制相似问题