使用Sikuli Finder()搜索带有图标的屏幕截图,在循环中使用时提供类似缓存的响应

时间:2019-05-31 22:04:51

标签: sikuli sikuli-ide sikuli-x

(在Windows 10上使用Sikuli IDE -288 20/04/19)

我目前遇到的问题是部分代码在第一次运行时正确运行,但是第二次循环执行该函数而不是覆盖第一次迭代中创建的信息,而是使用了旧信息。

调用了一个名为selectRewards()的函数,该函数需要花费几秒钟的时间来截取奖励区域的一些屏幕快照,以收集动画的可用静止图像,并且文件名会按数字递增。然后,该函数使用从屏幕快照1开始的屏幕快照创建Finder。将要检查的Finder和图像传递到search()函数中,在其中应使用传递的Finder和图像查找匹配项。它检查屏幕快照1,屏幕快照2等中所有已定义的图像,直到找到匹配项。然后使用屏幕快照中的坐标在屏幕上选择匹配项。

这一切在selectRewards()的第一次迭代中都能很好地工作,它在屏幕截图中循环并在稳定的屏幕截图中找到图像,但是当再次调用该函数时,将返回相同的精确“找到”结果,甚至在屏幕截图中不存在图像的情况下,点击也完全相同(我甚至在第一个循环结束时就删除了屏幕截图,以尝试清除发送给查找程序的任何不正确信息。

我试图以一种更简洁的方式来分享该部分,但仍然存在相同的问题。任何帮助和建议将不胜感激。

(尽管当前代码甚至有更奇怪的问题,因为在IDE的选项卡中打开了主脚本,而在另一个选项卡中打开了新脚本-均未运行-如果我运行摘要脚本,它将使用坐标/图像查找来自先前的脚本运行)。 Windows中是否可能存在某种内存问题或缓存? ALT + SHIFT + R重新启动IDE通常可以帮助解决此问题。

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

调用selectRewards()的循环之一是正确的,奖励区域中有3张图像与要搜索的内容匹配。但是第二个循环是不正确的,只有一个匹配的图像在那里并且不在相同的精确位置。脚本在第二次单击前一个循环的3个位置。

消息日志:     ====

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)

====

1 个答案:

答案 0 :(得分:0)

SikuliX的RaaiMan:

好的,原因是图像基于文件名在内部进行缓存。 当使用缓存中已有图像文件名创建新的Finder时,将使用缓存的版本。

因此,您可能会全局关闭缓存: Settings.setImageCache(0)

或添加: Image.reset()

selectRewards()开头

或将循环计数添加到捕获图像的文件名中(注意:内存不断增加!)

BTW:在IDE中选择另一个选项卡时,先前选择的选项卡中的图像将自动取消缓存。