findAll function is not returning any results though match preview indicates it should

Asked by Michel Aubinais

I am currently writing a script where the object is to find a certain image on a page. Once the image is found, the page resets and it needs to find this image again. Because the image is quite small, sikuli will sometimes interpret other graphical elements as being the image. As such, I need to find a way to prevent my mouse from clicking the same place over and over.

In order to avoid this occuring, this is how my script was built (the region is declared earlier):

x = Large.find(Found) #finding the first searchable in level
y = x #storing position of searchable for later comparison
click(x)
sleep(1)
while Large.exists(Found): #searching for hit boxes
        x = Large.find(Found) #current new position
        print(x,y)
        if x == y:
          with Large.findAll(Found) as m:
              while m.hasNext():
                      x = m.next()
                      y = x #resetting the value of y
                      click(x)
                      print(x,y)
        else:
            x = Large.find(Found)
            Large.find(Found)
            y = x #resetting y value to new x position
            click(x)
            sleep(1)
else:
    wait("arsLu5LL_.png",10)
    click("_JJ11i.png")

The issue that I am encountering is that the findAll() in line 14 won't always find items even when the matching preview indicates that some should be found. It also generally does not find the proper amount of matches. The following error appears in the output:

[error] Error message: Traceback (most recent call last):
 File "C:\Users\mpoulin\AppData\Local\Temp\sikuli-tmp6720749050519535256.py", line 15, in
 if x == y:
 Line 30, in file C:\Users\mpoulin\AppData\Local\Temp\sikuli-tmp6720749050519535256.py

 at org.sikuli.script.Region.handleFindFailed(Region.java:420)
at org.sikuli.script.Region.findAll(Region.java:459)
at sun.reflect.GeneratedMethodAccessor57.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

org.sikuli.script.FindFailed: FindFailed: can not find 1339174675949-1.png on the screen.
 Line 30, in file C:\Users\mpoulin\AppData\Local\Temp\sikuli-tmp6720749050519535256.py

I'm not sure if the error is in the coding or if it's an issue with the pattern recognition.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

--- the reported error

there is something strange with the line numbering:
--- you say: the findAll() in line 14
--- error says: line 15, in if x == y:

but then the findAll() should be in line 16 !?

and then an error is reported with line 30 (FindFailed with 1339174675949-1.png) - so which image is it in this case?

If it is the image that variable Found references, then I would change the code not using with, since I guess this puts the error to the last line.

          m = Large.findAll(Found)
          while m.hasNext():
                  x = m.next()
                  y = x #resetting the value of y
                  click(x)
                  print(x,y)

--- avoiding false positives on find
you should use:
x = Large.find(Pattern(Found).similar(0.99)) #finding the first searchable in level

to make sure, the exact pattern is found.

this normally helps to make sure not to click on false positives.

BTW: One can use Region.getLastMatch(), to not search for something, that was already found before.
your code:

while Large.exists(Found): #searching for hit boxes
        x = Large.getLastMatch() #current new position

But with the Pattern().similar(0.99) (DO NOT use 1.0 !) you might solve your problem anyway.

Can you help with this problem?

Provide an answer of your own, or ask Michel Aubinais for more information if necessary.

To post a message you must log in.