← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #227960]: How to make sikuli detect motions within a specific region?

 

Question #227960 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/227960

    Status: Open => Answered

RaiMan proposed the following answer:
You overwrite the handler parameter event, which is set outside, so it
gets invalid.

this is a version using inline/foreground observation (starting the
observer, stop it in the handler, restart it in main):

from sikuli import *
def changed(event):
#    event = Sikuli.SikuliEvent
    print "Something changed in ", event.region
    print event.changes
#    print dir(event.changes) # changes has no methods
#    print help(event.changes) ???
    for ch in event.changes:
        ch.highlight()
# makes no sense, because changes are not updated while in handler
#    Sikuli.wait(2)
#    for ch in event.changes:
#        ch.highlight()
    event.region.stopObserver()
    
r = selectRegion("Select a region")
r.onChange(50, changed)
for i in range 10: # or some timed loop
    r.observe(FOREVER)
    wait(2)

This is a suitable version, if some processing is done in the
handler(the observation is paused, when in the handler!)

This version uses the background observation and needs some
communication between handler and main:

from sikuli import *
def changed(event):
    print "Something changed in ", event.region
    print event.changes
    # content of highlighted areas might have changed already!
    for ch in event.changes:
        ch.highlight()
    wait(2) # pauses observation
    Settings.isChanged = True
    
r = selectRegion("Select a region")
r.onChange(50, changed)
r.observe(FOREVER, True)
Settings.isChanged = False # "pseudo" global, some primitive handler communication
for i in range 10: 
    if r.isChanged:
         print "something changed"
         Settings.isChanged = False
    wait(2)
r.stopObserver()

BTW: wether this works to your satisfaction heavily depends on, how
often per second the content in the video area changes. It might be hard
to really get the changes you want. But give it a try.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.