Region() example help

Asked by rlkee888

Hi Sikuli Community

I have been at it for sometime trying to use the region() function in Sikuli and have referred to many example, unfortunately not complete but sort of get the idea.

Anyway what I am trying to do is test a program that has a scroll button, it is instructed to click a button until an image appears in a scroll box. The code below works but scans very slow, I have tried to insert examples from others into it but sadly can't get it to work...

===================================================================================================
#Test if the department sale button is there
if not exists("department.png"):
    popup("Oops...!!! Check that you are on the sales menu")
    exit(1)
else:
    hover("department.png")
    click()
#This is where I get stuck
def Region_with():
   reg = Region(131,154,512,415) #This is the region I would like Sikuli to scan
   reg.highlight(5) #This confirms it is looking at the area
#And this where I don't know how to tell "while not exists" to only scan in the area above
while not exists ("dept_list.png"):
    click("scroll_btn.png")
    click(getLastMatch())
else:
    if exists("dept_list.png"):
        click("ok_btn.png")
        type("1000" + Key.ENTER)
wait("total_scrn.png")

===================================================================================================

What an awesome program, love how python is used and its teaching me more everyday.

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
RaiMan (raimund-hocke) said :
#1

To tell Sikuli, to only search in a specific region, you have to qualify the actions with the region using the dot-chaining or use with: to restrict all actions in the given block.

#Test if the department sale button is there
if not exists("department.png"):
    popup("Oops...!!! Check that you are on the sales menu")
    exit(1)
else:
    hover("department.png")
    click()

#This is where I get stuck

reg = Region(131,154,512,415) #This is the region I would like Sikuli to scan
reg.highlight(5) #This confirms it is looking at the area

#And this where I don't know how to tell "while not exists" to only scan in the area above
while not reg.exists ("dept_list.png"):
    reg.click("scroll_btn.png")
    click(reg.getLastMatch()) # this will again click on "scroll_btn.png" - wanted?
# else: # for a while is only additional noise and 1 more indent
# only makes sense, when break is used in the loop, since this would skip the else
# so if the loop ends, you are here anyway

if reg.exists("dept_list.png"):
    reg.click("ok_btn.png")
    type("1000" + Key.ENTER)
else:
    print "not found: dept_list"
    # what should we do here?
wait("total_scrn.png")

If this is too complicated and/or tedious there are 2 other options:

--- 1:
with reg:
# all following lines with one indent (like with while)
# instead of whole screen all actions will only be done in reg

---2:
setROI(reg)
this will "shrink" the screen's active search area to reg
advantage: no indent needed
disadvantage: you must reset it, if no longer needed - and in a longer code snippet it might not be obvious, that you are working on a restricted screen area
to reset it:
setROI(SCREEN)

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

One more thing:

region definitions like:
reg = Region(131,154,512,415)

only make sense, if the region will be there in all cases and for testing.

One should try to define regions relative to some fixed visuals , that are found on the screen, as soon as possible in script development.

example on this page:
--- I want to restrict to the region in the top right (the framed boxes)
top = find(<image of the box: ask a question ...>)
reg = top.below()

this will always restrict to the boxes area, no matter where on the screen it is currently.

Revision history for this message
rlkee888 (rlkee888) said :
#3

Hi @RaiMan,

I am going through all that you have sent me and will report back asap.

Thank you so much for your help.
Robin

Revision history for this message
rlkee888 (rlkee888) said :
#4

Hi @RaiMan,

I updated it to use the region, however based on the logs and the time it takes to find the image it seems as if it is still scanning the whole screen. Please see here...

#Test the department sale button
if not exists("department.png"):
    popup("Oops...!!! Check that you are on the sales menu")
    exit(1)
else:
    hover("department.png")
    click()
#Define a region for Sikuli to scan for
reg = Region(131,154,512,415)
while not reg.exists("dept_list.png"):
    reg.click("scroll_btn.png")
    click(reg.getLastMatch()) # I used this to speed up the scan and it works
if reg.exists("dept_list.png"):
    reg.click("ok_btn.png")
    wait("1378803496852.png")
    type("1000" + Key.ENTER)
else:
    print"not found:test department"

#this is the log file
[log] CLICK on L(843,126)@S(0)[0,0 1596x821] # this log shows it clicked image "department.png"

[log] CLICK on L(614,419)@S(0)[0,0 1596x821] # this log shows it clicked image "scroll_btn.png"

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(614,419)@S(0)[0,0 1596x821]

[log] CLICK on L(485,534)@S(0)[0,0 1596x821]

[log] TYPE "1000#ENTER."

Is it correct to assume that the area in the log file [0,0 1596x821] is the area Sikuli is still scanning the full screen and not the area in the region that was declared?

This whole exercise is to see if it scan and click faster.

Thank you,
Robin

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

--- [log] CLICK on L(614,419)@S(0)[0,0 1596x821]
means: Sikuli clicked at the point (614,419), which is on screen 0 ( S(0) ), which has a size of 1596x821 and its top left corner at (0,0), which in turn means it is the primary screen if more than one is available.

I guess, that the scroll button is at a fixed location, so this would be the fastest:

button = reg.find("scroll_btn.png")
while not reg.exists("dept_list.png", 0): # see comment
    click(button)
    click(button)
    wait(0.5) # adjust this as needed

comment: exists(image, 0) only searches once and comes back immediately. exists(image) will wait 3 seconds before giving up

You might test, wether it works without the wait().

Why do you click the button twice in one round?

Revision history for this message
rlkee888 (rlkee888) said :
#6

Thanks RaiMan, that solved my question.

Revision history for this message
rlkee888 (rlkee888) said :
#7

Hi @RaiMan,

Just adding the , 0) to it already made it faster, I will add the rest to the code and give feedback.

I have a list of departments that make increase or decrease based on the server's instructions, so the computer I am testing is to see that the scroll is going through all the departments until a specific department appears. Adding the click() then getLastMatch() was only used to speed up the clicking of the scroll button, now that the ,0 ) works I can remove the second click.

I tried to find out more on what a function like region() contains or what it supports and I cannot seem to find anything on the Sikuli guides like in the example you gave above on top = find(<image of the box: ask a question ...>) reg = top.below(), is there a way to see what is available like when you open python IDE in Linux or Windows and type >>> help () top.below()

Thank you for all the help...

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

-- autocomplete support and function help is on the list for 1.1.0

we have the docs at http://doc.sikuli.org
which are still on level on level RC3 though.

additionally there are the JavaDocs which are up to date and have information on new features e.g. for Region, that are not available in RC3 (https://dl.dropboxusercontent.com/u/42895525/SikuliX/SikuliX-API-JavaDocs/index.html)
Looking there is ok, since all Screen, Region, Pattern, Match and Location functions are available on the Jython/Python script level too.