[1.1.0+] HowTo use Region.observe()

Asked by Alex Lunyov

usage is correct - problem fixed with 1.1.1 nightly 2016-11-29
-----------------------------------------------------------------

I don't understand, why this code hangs up (I use latest 1.1.1 nightly version).

def myhandler(event):
    print "Seen"
    event.stopObserver()

img = "a.png"
reg = Region(417,895,209,185)

reg.onAppear(img, myhandler)
reg.observeInBackground(1)
reg.stopObserver()

# img is on the screen, so myhandler should immediately run and print "Seen". But I see the following:

--------------------------
Exception in thread "Thread-11" Traceback (most recent call last):
File "C:\SikuliX\SikuliX 1.1.1 Nov 07\sikulix.jar\Lib\sikuli\Region.py", line 63, in observe
...
File "C:\SikuliX\SikuliX 1.1.1 Nov 07\sikulix.jar\Lib\sikuli\Region.py", line 63, in observe
...
...
RuntimeError: maximum recursion depth exceeded (Java StackOverflowError)
--------------------------

Question information

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

That is a bit tricky, but for the Python level observeInBackground() is not useable (special method for the Java level).

So instead you have to use:
reg.observe(background=True)

... see docs.

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

... and carefully read through
http://sikulix-2014.readthedocs.io/en/latest/region.html#observing-visual-events-in-a-region

... and watch the changes with 1.1.0

Revision history for this message
Alex Lunyov (lunyov-av) said :
#3

When I use this:

onAppear(img, myhandler)
observe(10)

- it works fine. It observed the whole screen, see pic and write message "Seen".

And when I try to use this:

reg.onAppear(img, myhandler)
reg.observe(10, background=True)

- I see a "snowfall" of messages:
----------------------------------
Exception in thread "Thread-11" Traceback (most recent call last):
File "C:\SikuliX\SikuliX 1.1.1 08-11\sikulix.jar\Lib\sikuli\Region.py", line 63, in observe
...672 same lines
RuntimeError: maximum recursion depth exceeded (Java StackOverflowError)
---------------------------------

Not critical for me at my work but it could be interesting to solve. Thanks.

Revision history for this message
Alex Lunyov (lunyov-av) said :
#4

The same problem, hundreds of lines "File "C:\SikuliX\SikuliX 1.1.1 08-11\sikulix.jar\Lib\sikuli\Region.py", line 63, in observe", I have on Win7-32 and Win10-64.

When I change "10, background=True" → "10, background=False" I see only the error "RuntimeError: maximum recursion depth exceeded (Java StackOverflowError)" in red colour.

May be I missed something in syntaxis of observers, but I tried to read the docs carefully.

Revision history for this message
Alex Lunyov (lunyov-av) said :
#5

OK, I looked more closely the docs, tried the example and found out that adding the line

reg = JRegion(reg)

solved my problem at last. I don't know Java, this line was written on the off-chance. :)

That's curious that these lines produce different kinds of Region (Java and Python?):

reg = Region(x,y,w,h) # this doesn't work in observer
reg = selectRegion("select a region to observe") # this works in observer

But print(reg) gives in both cases "R[x,y wxh]@S(0) E:Y, T:3,0" where one can't see the difference between Java and Python level Region (hope this is right terminology).

My problem seems to be solved but I need some time to check.

Revision history for this message
spyros-liakos (spy-arts) said :
#6

Finally got this work out!!

regblueline = Region(602,315,100,150)
regblueline = JRegion(regblueline)
clicktoavoiderrordone = Region(1280,95,2,2)
count = False
popupcoc = "error1.png"
popupcoca = "error2.png"

def handler(event):
    global count
    count = True
    wait(2)
    print("#Observe Error")
    clicktoavoiderrordone.click()
    print("con")
    event.repeat(2)
    return count

regblueline.onAppear(Pattern("error1.png").exact(),handler)
regblueline.onAppear(Pattern("error2.png").exact(),handler)
regblueline.observeInBackground(FOREVER)

Got NO errors, observeinbackgronnd works fine...and the handler returns with success..!!

So the trick to observe in background always is this on 1.1.1

Thank you!!!

Revision history for this message
Alex Lunyov (lunyov-av) said :
#7

@spyros-liakos, ;) we do all the best to help each other here.

Revision history for this message
RaiMan (raimund-hocke) said :
#8
Revision history for this message
Launchpad Janitor (janitor) said :
#9

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

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

problem fixed with 1.1.1 nightly 2016-11-29