Home > Blockchain >  Python: Unable to move between loops correctly
Python: Unable to move between loops correctly

Time:02-10

I'm writing an image detection bot and as per the advice from a few members, I refactored some code and separated this section into its own function objectDetection I'm trying to do two things with the array image_list

  1. Iterate through the array and if any image is detected I want to immediately print "Avoided" to the terminal and restart the objectDetection function from the beginning of the array.

I've tried to do this with the flag variable found_anything = True and break it hasn't worked.

  1. Iterate through the full array of images and if nothing at all is found, execute functionTwo()

I tried to do this with the break at the end of my for i in image_list: loop, it also hasn't worked.

So far I've tried....

Reading through these three pages: Break out of two loops and two if statements - Python break out of two loops without disturbing the if statements after it in python Accessing the index in 'for' loops? and I tried both the variable flags and break statements but I can't get either to work.

I also had a look at this: How to break out of multiple loops? but what is the difference between return and break in python? made me think in my case the top answer wouldn't be appropriate as I don't think I'm returning data outside of my function.

Is there a way I can adapt this code to achieve my two uses cases above? Am I missing something obvious? I've been thought all the loop tutorials I can find and I'm still struggling, is this simply a difficult way I'm trying to design things? Or am I simply not getting it and should go back over some tutorials.

def objectDetection(image_list, threshold, haystack, a):
    for x in range (0, a):

        found_anything = False
        while found_anything is False:

            for i in image_list:
                needle_img = i[0]
                result = cv.matchTemplate(haystack, needle_img, cv.TM_CCORR_NORMED)
    
                # Get the best match position
                min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)

                # Define top left and bottom right and threshold
                (H, W) = i[0].shape[:2] 
                top_left = max_loc
                bottom_right = (top_left[0]   W, top_left[1]   H)

                if max_val >= threshold:
                    cv.rectangle(haystack, top_left, bottom_right, 255, 2)
                    print("Avoided")
                    found_anything = True
                    break
            break                              
            functionTwo()


while True:
    
    # Listener to check if on the correct screen
    if py.locateCenterOnScreen('!.jpg') != None:
        objectDetection(ships_to_avoid, 0.92, window, 10)
        break

CodePudding user response:

I'd refactor the whole thing but something like this answers the main question: Refactor your functions such that you can use 'Return' instead of 'Break'

def objectDetection(image_list, threshold, haystack, a):
    for i in image_list:
 
        max_val, top_left = find_needle_in_haystack(haystack, i[0])

        if max_val >= threshold:
            draw_rectangle(i,top_left,haystack)
            print("Avoided")
            return True

    return False


while True:
    if py.locateCenterOnScreen('!.jpg') != None:
        if not objectDetection(ships_to_avoid, 0.92, window, 10):
            function_two()
  • Related