[HowTo] handling findFailed

Asked by Javier Gonzales Rodriguez on 2020-06-14

Hello there ...

am using Sikuli for pretty basics task, am not a developer so will be great if any of you give me a hand to solve my issue.

So let me explain what am trying to do ...

I have 2 images, if any of them are at the region, then do something "except crashing :( "

I found how to handle it with "exists()" but the problem is that the two images are very very simular with other images so, sometimes it does well, sometimes not, the best is using .. find() but it stop the entire script if does not find anything.

my code is like this...

myRegion = Region(1186,447,173,63)

if myRegion.find(image1.png) or myRegion.find(image2.png):
      print("Found")
else:
     print("not found")

continue with the script .....

I tried to handle it with this...
try:
      myRegion = Region(1186,447,173,63)

      if myRegion.find(image1.png) or myRegion.find(image2.png):
           print("Found")
      else:
          print("not found")
except findFailed:
      pass

But it cotinues crashing....

I know that Sikuli is well documented but i couldnt understand how fix it, so ...

could someone please give me an example how to handle findFailed without stop my script?
I know that i can use all these but i dont know how... "setFindFailedResponse(SKIP), setThrowException(False), a function to handle it, etc."

Note: am using python, Sikuli IDE on Linux Mint 19.03

Thanks, regards!

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
masuo
Solved:
2020-07-16
Last query:
2020-07-16
Last reply:
2020-06-14

Hello Mauso,

Thanks for your answer but it didnt work,

findAny, findAll, findBest ... i tested all of them and they choose similar images but not the correct one.

I definetly need to use find()

masuo (masuo-ohara) said : #3

Try similar() and exact().
https://sikulix-2014.readthedocs.io/en/latest/pattern.html?highlight=similar#Pattern.similar

[sample code]
r = Region(0,0,500,500)
anylist = r.findAny(Pattern("1592122231908.png").exact(),Pattern("1592122242862.png").exact())
if len(anylist):
    print "found"
    for m in anylist:
        if m.getIndex() == 0:
            m.highlight(1,"yellow")
        else:
            m.highlight(1,"blue")
else:
    print "not found"

Thanks Masuo, it is working now!

I just did this...

rightRegion = Region(1186,447,173,63)

if rightRegion.findBest(Pattern("1592064085453.png").similar(0.98)) or rightRegion.findBest(Pattern("1592064049381.png").similar(0.98)):
    print "on screen"
else:
    print "is not there"

Thanks again!

Will be great if someone gives an example to handle find().
Am going to keep this open for a little more just in case.

Thanks masuo, that solved my question.

RaiMan (raimund-hocke) said : #7

The solution masuo suggested in comment #3 is the best one.
It gives the chance to evaluate the matches with little effort.

To use findBest() only makes sense, if you have more than one image and want to find the one with the highest score (e.g. a button, that might have different states).

If you want to work without handling FindFailed, you have to use exists().

So in your case, supposing the resulting matches do not matter:

if rightRegion.exists(Pattern("1592064085453.png").similar(0.98)) or rightRegion.exists(Pattern("1592064049381.png").similar(0.98)):
    print "on screen"
else:
    print "is not there"

if the matches matter:

match1 = rightRegion.exists(Pattern("1592064085453.png").similar(0.98))
match2 = rightRegion.exists(Pattern("1592064049381.png").similar(0.98))
if match1 and match2:
    print "on screen both"
elif match1:
    print "on screen match1"
elif match2:
    print "on screen match2"
else:
    print "is not there"

be aware:
all methods that are mentioned in this discussion do the same internally: search for an image with the highest score.
They differ only in parameters, results and how they handle FindFailed.

So if you have very similar images, than you have to use Pattern().similar() to distinguish.
... but the base always is to use images, that concentrate on the distinguishing key factors with as little background as possible.