check the existence of more than one image simultaneously?

Asked by mc123

Is there any way for Sikuli to scan for the occurence of multiple images?

while not find("imagename1.jpg") or ("imagename2.jpg"):
   sleep(10)
openApp("c:\\alert.bat")

In my example above I need something that checks for both images and then play a sound if either appears. I tried OR, but that doesn't work. What do I need to do?

Question information

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

if you use find, internally always 3 sec are spended to wait for the image to come up, if its not there. Since with sleep you use a waiting timer yourself, its better to use wait() instead and reduce the timeout to minimum. So wait tries to find only once and stops with None (evals to False), if not found.

this should do what you want:

while not ( wait(<img1>,1) or wait(<img2>,1) ):
   sleep(10)
openApp("c:\\alert.bat")

if any image is found (in a logical evals to true), the while is not executed any longer

Revision history for this message
mc123 (mcast12) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
alcia burke (alcianicole) said :
#3

Hi,

How do I use if-else statement for checking the existence of ( x or y or z) image. I am doing this because I am iterating through a csv file and the image changes with each line.

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

Based on version 0.10:

To use find() or wait() does not make sense, since the first not found would interrupt your script (since 0.9.9 a not found will throw exception FindFailed by default)

To make checks like this there is the function exists(), that does the same as wait() and find(), but returns True and False respectively without raising an exception FindFailed. The match itself can be recalled by using getLastMatch(). So this is the function to use in situations where you only want to know if something is there or not, to decide, how your workflow should proceed.

On top, exists() accepts a timing parameter like wait, that can be used in a special way: setting it to 0, means, that internally only one try is made to find the image (makes sense, since you think it is there or not). So its very speedy.

To even boost up performance, you could restrict the search to a smaller region, you think should contain your images. To adjust the minimum similarity using the preview in the IDE may make sense, to assure that Sikuli finds, what you are looking for.

To first test the situation, I always do something like this, to get as much information as possible in the IDE:
print "m1: "; m1=exists(image1, 0); if m1: print getLastMatch()
print "m2: "; m2=exists(image2, 0); if m2: print getLastMatch()
print "m3: "; m3=exists(image3, 0); if m3: print getLastMatch()

This would give you in the message area:
if found: m1: Match[246,37 52x49] score=1,00, target=center
not found: m2:

if m1 or m2 or m3:
   pass # here goes your coding if one or more images are found
else:
   pass # here goes your coding if none of the images is there

If everything works well, you could decide to streamline your code;
if exists(image1, 0) or exists(image2, 0) or exists(image3, 0):

But with this optimized version, you will not have a simple chance, to evaluate, which of your alternatives matched.

Generally: when using the "or" construct, the first successful find will end the evaluation. So the sequence of the exists() may matter.

If you want to act different for each match you could write:
m1=exists(image1, 0)
m2=exists(image2, 0)
m3=exists(image3, 0)

if m1:
   pass # code if m1 matched
elif m2:
   pass # code if m2 matched
elif m3:
   pass # code if m3 matched
else:
   pass # here goes your coding if none of the images is there

If you want to try one of the cool new features of version 0.10, look at
5.5. Observing Visual Events in a Region

This allows, to wait for different events, that can be handled totally independent from each other.

Have fun.