1

I’m working on a project in micropython using an openMV camera and blob detection to determine the orientation of an object. 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). 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.

'''

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




















    

       

1 Answers1

1

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

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

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.

If you move the allocations outside the loop, there shouldn't be a problem.

jeekiii
  • 289
  • 2
  • 10
  • Can you explain what you mean by allocations? Do you mean all of the for statements in the while loop? Sorry I am still having problems. I am just learning Python....Every single way I try to do it, I get arilY isn't defined. So I am missing something – Hannah Reigi Jun 28 '22 at 02:26
  • I mean the line seedY = seed.y(). Let's look at your code again, you have these three for loops. Inside of them, you start by drawing the rectangles, and that's fine, you presumably want to do that for every element of the loop. But then you do this "arilY = aril.y()". The problem with that line is that you are redefining arilY every single time and not keeping the previous values. So actually only the last element in the list will have it's value saved in arilY, all others will be overwritten by the next iteration. I presume that's not what you wanted? – jeekiii Jun 28 '22 at 10:28
  • But really if you want the simplest answer you can absolutely just add seedY = None and arilY = None at the start, and then enclose your func_orientation with if(seedY and arilY): – jeekiii Jun 28 '22 at 10:53
  • I updated my code in my question...no error now, however the check doesn't execute when the object is in frame? Or I am guessing that is the problem because I get no output.. – Hannah Reigi Jun 28 '22 at 14:09
  • I mean you have a different problem then. Yeah, check the size of your seed array and so on, but I think that's a different issue not fit for this threat. – jeekiii Jun 28 '22 at 22:15