danceforthesky
danceforthesky

Reputation: 3

Using Sikuli Finder() to search screenshot with icon, providing cached like response when used in a loop

(Using Sikuli IDE -288 20/04/19 on Windows 10)

I am currently having issues with a portion of code that runs correctly the first time around, but the second time where the function is looped instead of overwriting information created in the first iteration, it is somehow using the old information.

A function called selectRewards() is called, and it takes a few screenshots of the reward region over a few seconds to gather a useable still of an animation, the file name is numerically incremented. Then the function creates a Finder using the screenshots starting with screenshot 1. The Finder, and image I want to check against is passed into a search() function where it should be using the passed finder and image to find matches. It checks for all defined images in screenshot1, screenshot2 etc. until matches are found. And the matches are selected on the screen using coordinates from the screenshot image.

This all works well in the first iteration of the selectRewards(), it cycles through the screenshots and finds the images on a stable screenshot, but when the function is called again the same exact "found" results are returned, and clicks are exactly the same even when the images don't exist in the screenshots (I've even deleted the screenshots at the end of the first loop to try and clear any incorrect information being sent to the finder.

I've tried to pull the section out to share in a cleaner way, and it still provides the same issue. Any help and advice would be deeply appreciated.

(Though currently having even stranger issues with the code, since having the main script open in a tab on the IDE and the new script in another - neither running - if I run the snippet script it will use the coordinates/image finds from a previous run of the scripts). Can there be some sort of memory issue or caching in windows? ALT+SHIFT+R to restart the IDE normally helps clear the issue.

Settings.MoveMouseDelay = 0.5
#Define Regions
rewardRegion = Region(536,429,832,207)
#Define Images
searchCoupons = Pattern("coupons.png").similar(0.85)
searchAdvanced = Pattern("2011.png").similar(0.85)
searchAdvancedFrag = Pattern("2012.png").similar(0.85)

matchesFound = False

def search(image,rewardGlimpse, descr = ""):    
    print ("##### searching for: (%s) %s" % (image, descr))
    rewardGlimpse.findAll(image) # find all matches (using passed finder variable & image variable)
    matches = [] # an empty list to store the matches
    while rewardGlimpse.hasNext(): # loop as long there is a first and more matches
        matches.append(rewardGlimpse.next())        # access next match and add to matches
        # now we have our matches saved in the list matches
    print("   Does FindAll have next? (should be false):" + str(rewardGlimpse.hasNext()))
    print("   Found matches:" + str(len(matches)))

    if len(matches) > 0:
        global matchesFound
        matchesFound = True
        obtainedReward = str(descr)
        print("   Match found should be true " + str(matchesFound) + ". Found: "+obtainedReward)

        # we want to use our matches
    for m in matches:
        #Find x & y location of rewards in screenshot
        matchx = m.x
        matchy = m.y
        #Append them to the reward region to line it up.
        newx = rewardRegion.getX()+matchx
        newy = rewardRegion.getY()+matchy
        rewardHover = Location(newx, newy)
        #click the found reward location
        click(rewardHover)
        wait(1)

def selectRewards():
    #---- Save Incremental Screenshots
    wait(1)
    capture(rewardRegion,"screenshot1.png")
    wait(0.5)
    capture(rewardRegion,"screenshot2.png")
    wait(0.5)
    capture(rewardRegion,"screenshot3.png")
    wait(0.5)
    capture(rewardRegion,"screenshot4.png")
    wait(0.5)
    capture(rewardRegion,"screenshot5.png")
    wait(0.5)
    capture(rewardRegion,"screenshot6.png")
    wait(0.5)
    #----- Test the screenshots
    snum = 1 #screenshot file number

    while True:
        global matchesFound
        if matchesFound == True:
            print("Rewards Found - breaking search loop")
            matchesFound = False
            break
        else:
            pass

        #Start with _screenshot1.png, increment snum.
        screenshotURL = "_screenshot"+str(snum)+".png"
        rewardGlimpse = Finder(screenshotURL) #Setup the Finder

        print("Currently searching in: " + str(screenshotURL))

        #Pass along the image to search, the screenshots Finder, and description.
        search(searchCoupons,rewardGlimpse, "Coupons")
        search(searchAdvanced,rewardGlimpse, "Advanced Recruit Proof")
        search(searchAdvancedFrag,rewardGlimpse, "Advanced Recruit Fragments")
        snum = snum + 1
        if snum>6:
            break

    while True: #All rewards available this round are collected     
        if exists("1558962266403.png"):
            click("1558962266403.png") 
            #confirm
            break
        else:
            pass
        print("No reward found at this point.")
        print("Matches Found at No Reward Debug: " +str(matchesFound))
        #Needed matches not found, selecting random reward.
        hover("1558979979033.png")
        click("1558980645042.png")
        #matchesFound = False #Toggle back to False

        #print("Matches found: Variable Value(Should be false)" + str(matchesFound))

def main():
    i = 0
    SSLoops = 2
    while i < 2:
        print("Loop #" + str(i+1) + "/"+ str(SSLoops))
        print("--------------")
        if i == 1: #remove this if statement for live
            click("1559251066942.png") #switches spoofed html pages to show diff rewards

        selectRewards()
        i = i + 1

if __name__ == '__main__':
    main()

Loop one of calling selectRewards() is correct, there were 3 images in the reward region that matched the things to search for. But the second loop is incorrect, only one of the matching images were there and wasn't in the same exact position. The script clicked in the 3 locations of the previous loop during the second time around.

Message log: ====

Loop #1/2
--------------

Currently searching in: _screenshot1.png
##### searching for: (P(coupons.png) S: 0.85) Coupons

Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Coupons

[log] CLICK on L[603,556]@S(0) (586 msec)

##### searching for: (P(2011.png) S: 0.85) Advanced Recruit Proof

Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Advanced Recruit Proof

[log] CLICK on L[653,556]@S(0) (867 msec)

##### searching for: (P(2012.png) S: 0.85) Advanced Recruit Fragments

Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Advanced Recruit Fragments

[log] CLICK on L[703,556]@S(0) (539 msec)

Rewards Found - breaking search loop

[log] CLICK on L[90,163]@S(0) (541 msec)

Loop #2/2

--------------

[log] CLICK on L[311,17]@S(0) (593 msec)

Currently searching in: _screenshot1.png
##### searching for: (P(coupons.png) S: 0.85) Coupons

Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Coupons

[log] CLICK on L[603,556]@S(0) (617 msec)

##### searching for: (P(2011.png) S: 0.85) Advanced Recruit Proof
Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Advanced Recruit Proof

[log] CLICK on L[653,556]@S(0) (535 msec)

##### searching for: (P(2012.png) S: 0.85) Advanced Recruit Fragments
Does FindAll have next? (should be false):False
Found matches:1
Match found should be true True. Found: Advanced Recruit Fragments

[log] CLICK on L[703,556]@S(0) (539 msec)

Rewards Found - breaking search loop

[log] CLICK on L[304,289]@S(0) (687 msec)

====

Upvotes: 0

Views: 947

Answers (1)

RaiMan
RaiMan

Reputation: 1143

RaiMan from SikuliX:

ok, the reason is the image caching internally based on filenames. When a new Finder is created with an image filename already in cache, the cached version is used.

So you either might switch off caching globally: Settings.setImageCache(0)

or add: Image.reset()

at the beginning of selectRewards()

or add the loop count to the filename of the captured images (take care: memory is constantly increased!)

BTW: when selecting another tab in the IDE, the images from the previously selected tab are uncached automatically.

Upvotes: 1

Related Questions