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

问题描述

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

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

调用了一个函数selectRewards(),它需要在几秒钟内对奖励区域进行一些屏幕截图,以收集一个可用的动画静止图像,文件名按数字递增。然后该函数使用从屏幕截图 1 开始的屏幕截图创建一个 Finder。我要检查的 Finder 和图像被传递到一个search()函数中,它应该使用传递的 finder 和图像来查找匹配项。它检查 screenshot1、 screenshot2 等中所有定义的图像,直到找到匹配项。并使用屏幕截图中的坐标在屏幕上选择匹配项。

这一切都在 的第一次迭代中运行良好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)

====

标签: sikulisikuli-idesikuli-x

解决方案


来自 SikuliX 的 RaiMan:

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

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

或添加:Image.reset()

在 selectRewards() 的开头

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

顺便说一句:在 IDE 中选择另一个选项卡时,先前选择的选项卡中的图像会自动取消缓存。


推荐阅读