do an action if the image on a region doesn't change

Asked by Greg83140

Hello,

i have a problem to do an automatic test on the sceen.
Here is my problem :

I would like to see if after an action, the image on a specific region has changed. I now how to do that with :
if region.exists(image,0) :

The problem in this case is that I don't know the image, but I know the region....
I know how to set an image with Sikuli IDE but I would like to set an image in my code for doing the comparaison

Thank's by advance for your answer

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Greg83140
Solved:
Last query:
Last reply:
Revision history for this message
j (j-the-k) said :
#1

you can use the onChange listener to do this, if you don't know how your image looks like:

def handler(event):
    print "the region changed:"+`event`

region.onChange(handler, 10)
region.observe(5)

This will observe the region for 5 seconds and execute the script that is defined in the handler() method if the region changes during that time

Revision history for this message
Greg83140 (ggrergg) said :
#2

Thanks for your answer...But the fact is that I would do an action only if the region not change.
Is exists an onNotChange method ??

Could you give me an exemple please?

Revision history for this message
Greg83140 (ggrergg) said :
#3

Nobody ??

Revision history for this message
Greg83140 (ggrergg) said :
#4

I tried this code :

reg = myRegion

def pp(event):
    global toto
    toto = "change"
    print"change"

def fff():
    toto = "notChange"
    print "do my action"
    reg.onChange(pp)
    reg.observe(1)
    wait(4)
    if toto == "notChange":
        print"notChanged"

fff()

When my region isn't change, it works:
result :
do my action
notChanged

but when my region change, it doesn't work:
result:
do my action
change
notChanged

why does the code goes to "notChanged"

Thanks by advance for your answer

Revision history for this message
Mark Weisler (mark-weisler-saratoga-ca) said :
#5

The following working code might help you...

# script: RegionAreaTest6, 01 June 2013
# Related to [Question #229825]: do an action if the image on a region doesn't change
# This Sikuli script monitors a screen area to detect change.

bigChange=False # a global variable set if a region changes

def changed(event): # this function runs if change is observed
        print "something changed in ", event.region
        global bigChange # set to true if this function runs
        bigChange=True
        print bigChange
        for ch in event.changes:
                ch.highlight(2) # highlight all changes
        sleep(1)
        for ch in event.changes:
                ch.highlight() # turn off the highlights
        event.region.stopObserver()

# now start watching a Chicago Transit Authority map of buses moving down streets
# http://ctabustracker.com/bustime/map/displaymap.jsp

print "Starting..."
myRegion=Region(200,250,500,500)
# adjust size of region examined, make area as small as reasonably possible to limit searching, analysis

#myRegion=Region(420,270,200,200) # an alternative region that is smaller
myRegion.highlight(3) # for illustrative purposes, not functionally necessary
wait(1)
myRegion.highlight(0) #remove highlight
wait(1)
print bigChange # value of the variable before starting the monitoring

myRegion.onChange(30, changed) # install 'changed' as the function to run if changes are discovered
myRegion.observe(30,background=True) # start observing for 30 seconds before proceeding
# Monitoring is now running.
# Next start a loop to check for changes in the monitored area...

myLimit=4
i = 0 # index for our loop
mycontinue = True
print "Starting mycontinue loop..."
while mycontinue:
    print bigChange
    if bigChange:
        print "Something just changed."
    else:
        print "No change yet."
        # Do some desired action here...
    i +=1 # increment index
    if i >=myLimit:
        mycontinue=False
    wait(5)

print "Leaving mycontinue loop..."

myRegion.stopObserver() #stops the observer

myRegion=Region(100,300,500,500)
myRegion.highlight(3)
wait(1)
print bigChange
exit(0)

Revision history for this message
Greg83140 (ggrergg) said :
#6

Thank you, I tested your code it works well !
Have a nice sunday

Revision history for this message
Steve (evildeliverance) said :
#7

Very old question but it was a top search result for this question.

This can be solved in a much simpler way with:

def CheckForChange(checkRegion,sleepTime):
    cap = capture(checkRegion)
    sleep(sleepTime)
    if checkRegion.exists(cap,0):
        print("Still exists, no change")
        return False
    else:
        print("No longer exists, reg changed")
        return True