← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #255516]: how to detect when screen changes - java

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
All image related features of SikuliX currently are based on OpenCV's
function matchTemplate, that simply uses some statistical functions, to
find out which area of one (at least equal sized but usually larger)
(base) image has the most equal pixels as the given (search) image. This
means, that from top left at each pixel, the area with the same size as
the search image is compared pixel for pixel, to evaluate the so called
similarity that ranges from 0 (no pixel is equal) to 1 (all pixels are
equal).

the basic find op makes a shot of the given region as base image and
runs a matchTemplate using the search image. The area showing the
highest similarity is returned as the match region.

There is an implicit standard minimum similarity of 0.7, leading to a
FindFailed exception, if the evaluated highest similarity is below this
threshold. There are some different ways to define this wanted minimum
similarity up to exact (meaning the evaluated similarity must be >0.99).

... and we have exists() that does not throw FindFailed, but returns
false (null) and we have the observe() feature, that might even run in
background and check different appear, vanish or change (which actually
internally uses different OpenCV features) events (look at the docs to
understand the features).

Especially for observe it depends on the SikuliX version you are using,
how this works on the Java level (not documented).

One more thing: the scan rate: since we are looking for an image in a screenshot, the search operation has to be repeated during the given waiting time with a refreshed screenshot from time to time.
As a good average standard value this scan rate is defined as 3 per second, but can be changed globally or per region if needed (lower or higher). But how many repeats are actually done per second depends on the individual search time for one repeat step.
So if the search time is larger than 0,333 secs (searching in large regions or even big screens), then we do not have our 3 per second and the machine is constantly in the number crunching mode.

In your case, this is the easiest solution (Python code):

observedRegion = Region(500, 500, 10, 10)
regionContent = Pattern(capture(observedRegion)).exact() # to detect differences of some pixels
changed = True
maxTime = 10
endTime = time.time() + maxTime * 1000 # when should it stop
while observedRegion.exists(regionContent, 0): # runs one check lasting some milli seconds
    if time.time() > endTime:
        changed = False
        break
    wait(0.1) # this influences the scan rate

If you need that in Java code: come back later, I want to go to bed now
;-)

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