sikuli find same image in screen

Asked by eduardobedoya

I have these "empty checker boxes" images on screen that I would like to count them from top to bottom and hover the mouse over the last one (the one at the bottom) using Sikuli. I don't know if it is really necesary to count them to do so.

I have search on the web and have tried this code to count the images:

    count = 0
    try:
        matches = list(findAll(the empty checker box image))

        count = len(matches)
    except FindFailed:
        pass #nothing to do
    print "found:", count

I also have try this code to hover the mouse over the images, but it hover it randomly:

    for x in findAll(the empty checker box image):
       hover(x)

Thanks Advanced.

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:

This question was reopened

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

   count = 0
    try:
        matches = list(findAll(the empty checker box image))
        count = len(matches)
    except FindFailed:
        pass #nothing to do
    print "found:", count

    if count > 0:
        sortedMatches = sorted(matches, key=lambda m:m.y) # sorts top to bottom
        hover(sortedMatches[-1]) # take the last one

Revision history for this message
eduardobedoya (gonnabdh) said :
#2

Thanks man it worked, but is a little slow (counting 14 equal images) is there any other way to make it faster?
THanks Advanced.

Revision history for this message
eduardobedoya (gonnabdh) said :
#3

Thanks RaiMan, that solved my question.

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

faster?

only by making the region to search in as small as possible and use:
matches = list(reg.findAll(the empty checker box image))

where reg is the region evaluated somehow.

Revision history for this message
eduardobedoya (gonnabdh) said :
#9

Thanks man,
Now I realized that I need to perform a little extra task before performing the script above

I would like to left click on a single image (e.g. start image), hold it down (the left click)
then drag the mouse until the last "empty checker boxes" finally release the left click

is that possible??

THanks advanced RaiMan, it would be all.

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

hover(startImage)
mouseDown(Button.LEFT)
hover(lastImage)
mouseUp()

depending on what you are doing, intermediate short waits (wait(0.3) or wait(0.5)) might be needed, if it does not work.

BTW: all in the docs:
http://sikulix-2014.readthedocs.org/en/latest/index.html

Revision history for this message
eduardobedoya (gonnabdh) said :
#11

Thanks RaiMan it worked fine.

just some issue
I said if the script could run faster because when running the script it is possible to watch the mouse moving toward the defined image, I mean the mouse does not move inmediatly, it move kinda fast but not inmediatly. Another macro recorders, move the mouse inmediatly so you can hardly see where is it moving. I mean the operation of looking the images and finding the last one is performed very fast, but then I got to wait to the mouse to move toward the last image, when it already knows where to go, it just does not go inmediatly.

Is any way to adjust that? so the mouse move inmediatly to its target once its computed?

PD: just in case, is any way to restrict the search area to a given window? like google chrome,
thanks advanced,
thanks for all, this is the best forum I ever found!

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

I guess, you should at least once read through the docs:
- yes, mouse move delay can be set to 0
- yes, you can restrict the search region in various ways

... and SikuliX is not a macro recorder ;-)

Revision history for this message
eduardobedoya (gonnabdh) said :
#13

Hi RaiMan,

I read the manual and I set
Settings.DelayAfterDrag = 0
Settings.DelayBeforeDrop = 0
Settings.MoveMouseDelay = 0
So now the delay of mouse is gone

I have read much about restrict search area
using reg.findAll but I quite don't understand how to get it to work
It seem when I use reg.findAll I'm suposed to put some code at the top of the script,
but I just dont know what, I have try typing...
    aChrome = App("Chrome") # to be adapted for Windows
    aChrome.focus()
    SCR1 = aChrome.window().getScreen()
or
    setROI()
but both give me error

I tried to read other questions like this one
https://answers.launchpad.net/sikuli/+question/173996
and try to apply to my case, but I still can not get it to work

How could I perform this search image task inside chrome (perhaps even if is hided?)

Thanks Advanced RaiMan, sorry if I couldn't find my own answer, I did solve the mouse delay. THank Again

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

These are the steps, to get the region of the frontmost Chrome window.

1. open Chrome and make it the frontmost application
2. open the needed webpage and wait until it is ready

For doing step 1 and 2 in a Sikuli script, there are various approaches.
The easiest of course is to do it manually and start the script, when the page is ready.

reg = App.focusedWindow() # gives you the window region of the frontmost application

matches = list(findAll(some_image))

now you have 0 or more matches in matches, that you can evaluate.

Revision history for this message
eduardobedoya (gonnabdh) said :
#15

Thanks RaiMan
Sorry for long delay, regex have been taking me quite distant from sikuli these days
What if I just have two opened windows
Chrome and Skype both maximized, chrome is already showing the webpage where I need to perform the image recognition, but Chrome usually get covered by the Skype window (loss focus), So.
is there a way to automatically tell sikuli to look for those image in my script only inside chrome window (cuz it has loss focus)
in other words is there a way to tell sikuli to gain focus on chrome window if when I run the script Skype is the focused window?
how could I achieve that?
Thanks for your support, hope I am not abusing,
anyway I will check it up with documentation ASAP
please give me a hint or link?
Thanks again Raiman.

Revision history for this message
eduardobedoya (gonnabdh) said :
#16

Thanks RaiMan, that solved my question.

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

App.focus()

see docs

Revision history for this message
sami baig (gabbarmic) said :
#18

It worked, thanks to everyone.