简体   繁体   English

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

[英]How to create an escape path for my micropython code?

I'm working on a project in micropython using an openMV camera and blob detection to determine the orientation of an object.我正在使用 openMV 相机和 blob 检测来确定对象的方向。 My problem is when the check is executed, I get an error “ArilY is not defined”, because the object isn't in the camera view yet (moving on conveyer).我的问题是在执行检查时,我收到一个错误“ArilY 未定义”,因为该对象尚未在相机视图中(在传送带上移动)。 How can I implement a path in my code to not execute the check and just print that there is no object instead, then begin the loop again and check for the object?如何在我的代码中实现一个路径以不执行检查而只打印没有对象,然后再次开始循环并检查对象? I have tried to implement a break with if else but can't seem to get the code right.我试图用 if else 实现中断,但似乎无法正确编写代码。

''' '''

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);




















    

       

Something you could do is preemptively define arilY and SeedY as None before the while loop, then enclose the check in a if(arilY and seedY):您可以做的是在 while 循环之前预先将 arilY 和 SeedY 定义为 None,然后将检查包含在if(arilY and seedY):

if you want to avoid using None, you could have an additional boolean that you set to true when arilY is detected, then enclose the check in a test for this boolean如果你想避免使用无,你可以有一个额外的布尔值,当检测到 arilY 时设置为 true,然后将检查包含在这个布尔值的测试中

But the bigger question here, is why your allocations are in the inner loop?但这里更大的问题是,为什么你的分配在内部循环中? You always redefine seedY and arilY for each iteration of the loop, which mean it will always be equal to seed.y of the last seed in the list, meaning all allocations prior to the last one were useless.你总是为循环的每次迭代重新定义 seedY 和 arilY,这意味着它总是等于列表中最后一个种子的 seed.y,这意味着在最后一个之前的所有分配都是无用的。

If you move the allocations outside the loop, there shouldn't be a problem.如果您将分配移到循环之外,则应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM