search multiple images at same time

Asked by alex

I have a script where i search images one after the other because I can't figure out how to search them all at once. I have a feeling the observe function might be my answer but don't understand it well enough.

I know how to set ROI and I have. The reason they are being changed in the script so many times is because if it finds a match and clicks the region needs to change.

The problem is that I need to speed this up significantly. Right now it is searching for 1 image at a time. There are about 10 images (this is not the whole script) and I would like to add more.

Also I found out that the coordinates for setROI cannot be decimals. Is there a way to round those numbers up or down? (its not giving me an error now because x and y are set to 1)

    setROI(90/x,95/y,700/x,400/y)
    for pick in range(1):
        type(Key.ALT)
        if exists(Pattern("1342385078677.png").similar(0.99)):
            click(getLastMatch())
            setROI(440/x,200/y,975/x,535/y)
            wait(1)
        if exists(Pattern("1342388811042.png").similar(0.99)):
            click(getLastMatch())
            setROI(440/x,200/y,975/x,535/y)
            wait(1)
        if exists(Pattern("1342388633408.png").similar(0.99)):
            click(getLastMatch())
            setROI(440/x,200/y,975/x,535/y)
            wait(1)
        type(Key.ALT)
        if exists(Pattern("J.png").similar(0.99)):
            click(getLastMatch())
            setROI(440/x,200/y,975/x,535/y)
            wait(1)
        if exists(Pattern("1342388876296.png").similar(0.99)):
            click(getLastMatch())
            setROI(440/x,200/y,975/x,535/y)
            wait(1)

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

--- cannot be decimals. Is there a way to round those numbers up or down?
these kind of questions can easily be solved using Google: "Python round up down" comes back with over 3 Million entries.
integer = int(x/y) # cuts the decimals
integer = int(round(x/y)) # up/down at 0.5

--- The problem is that I need to speed this up significantly.
Your problem is, that every if, that does NOT find, costs you 3 seconds (the standard waiting time).
Since each find operation with smaller regions should cost less than 0.5 seconds, the first step to speed up is using
if exists(some_image, 0):
which comes back after one search, no matter if found or not, hence each if will max take 0.5 seconds (the smaller the search region, the faster).
You can speed this up further, if the searches can be grouped into logical groups, that exclude each other (if from one group one is found, other groups need not be searched, ...)
group1 = false
group2 = false
# searching group 1
if exists(group1_img1, 0):
    group1 = true
    # more code
if exists(group1_img2, 0):
    group1 = true
    # more code
# .... more if's
if not group1:
    if exists(group2_img1, 0):
        group2 = true
        # more code
........

--- using observe
observe might further help, since it can be detached to subtasks with background=True, which would parallelize the searches.
But it is rather complex to coordinate the observes and react coordinated when something is found (e.g. using the mouse to click in your case).
More complexity is added, if, as in your case, the search region changes or other aspects of the search.

And you have to switch to use regions instead of using the setROI (which I generally do not recommend, because you might forget to reset/change the ROI again and these are hard to find script bugs).

region1 = Region(x,y,w,h)
if region1.exists(some_image, 0):
     region1.morphTo(Region(newX, newY, newW, newH)
     ....

--- BTW: type(Key.ALT)
should do nothing, because the modifier keys cannot be type()'d.
use instead:
keyDown(Key.Alt); keyUp()

Revision history for this message
alex (alexander-appugliese) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
alex (alexander-appugliese) said :
#3

Thanks so much I used the following

--- integer = int(x/y) # cuts the decimals

--- if exists(some_image, 0):

--- i still want to look into observe for something later

for some reason I tested it and using region takes longer for the program to search than just setROI so i stuck with setROI but am being careful

--- not sure why but type(Key.ALT) works for me, at least for what I'm using sikuli for. typing alt just at the desktop does nothing but it does something with the program that I use sikuli to pilot.

since this I have moved on and will be posting another problem regarding global functions and importing