looking for several pictures and return value

Asked by Kurt Zalkowitz

Hi,

For some time I am trying to implement a function to check for pictures in a region and return a value. At first I used this:

def chkpic():
    if exists("img1".exact()):
       return 1
    elif exists("img2".exact()):
       return 2
    elif exists("img3".exact()):
       return 3

Did not work. Then I tried this and it did not work either:

def chkpic():
   if find ("img1".exact()):
      return 1
   elif find("img2".exact()):
     return 2
   elif find("img2".exact()):
     return 3

The function is triggered by another function were some seconds are waited for the images to appear and then this functions is called but it doesn't work.

best KZ

Question information

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

--1. exact() seems to have a bug - use similar(0.99) instead. But even that normally is not needed. But give it a try.

--2. for these situations, the first version is principally the choice, since in the second case, a not successful find() would stop the script with a FindFailed exception.

--3. If you make a case decision with exists(), you should use exists(image,0), so it does not wait 3 seconds, if the case is not true (image not there).

So this should work as expected:

def chkpic():
    if exists("img1", 0):
       return 1
    elif exists("img2", 0):
       return 2
    elif exists("img3", 0):
       return 3

To be more flexible and have a chance, to debug the situation:

def chkpic(debug = False):
    ret = 0
    if exists("img1", 0):
       ret = 1
    elif exists("img2", 0):
       ret = 2
    elif exists("img3", 0):
       ret = 3
    if debug and ret > 0:
        print ret, "---", getLastMatch()

So when using chkpic(), no message will be printed, but for debugging purposes you can use chkpic(True), which would print you a message, showing the match, which gives you a chance to check e.g. the similarity score.

To make this function even more generally usable:

def chkpic(images, debug = False):
    ret = 0
    for i in range(len(images)):
        if exists(images[i], 0):
            ret = i
            break
    if debug and ret > 0:
        print ret, "---", getLastMatch()

and the usage would be:

chkpic(("img1", "img2", "img3"))

or

chkpic(("img1", "img2", "img3"), True)

So you can use the function in different places with different lists of images.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

Be aware, that this solution returns when the first image is found. So the sequence of the images in the case decision is relevant.

Come back, if you need a solution, to get ALL occurrences.

Revision history for this message
Kurt Zalkowitz (34g18lf3dr10mafs) said :
#3

Thx. I used similar and it works now.