Match for same pattern in same region passes then fails

Asked by Jeff Cavill

Has anyone tried successive, identical pattern searches with Region.find() and had the first invocation pass and the next fail?

I am trying to use the Sikuli Java API to write JUnit tests.
Each test builds a simple JFrame GUI which contains a custom web browser component as its only content. The content pane is sized so that the browser component is 1000x500. The HTML in the browser does nothing but paint the background red.

A 1000x500 red rectangle is stored in a .png file and used to create a Pattern.
The first time I seek this Pattern in the Region of the application, I get an almost perfect match.
Seeking the same Pattern on the same Region again, however, throws a FindFailed exception.

I am using 64-bit Java 7 update 55 on Windows 7.
I saw this behavior first with Sikuli 1.0.1 (sikuli-ide.jar) and then again with the latest 1.1.0-Beta3 source downloaded from GitHub.

Here is the relevant code:

@Test
public void basicPatternMatch() {
   Pattern p = new Pattern("C:/Users/jcavill/AppData/Local/Temp/jeff270671578108421267.png");
   try {
      Region r = app.getRegion(); // calls Region.create(x, y, w, h)
      printRegion(r);

      Match m = r.find(p);
      printMatch(m);

      m = r.find(p);
      printMatch(m);
   } catch (FindFailed e) {
      System.err.println(e.getMessage());
      junit.org.Assert.fail("Pattern match failed: " + e.getMessage());
   }
}

Here is the console output:
-----------
Region at (300,300) is 1016 x 538
Match found at (309,331) of size 1000 x 500 with score 0.9999250173568726

can not find P(C:/Users/jcavill/AppData/Local/Temp/jeff270671578108421267.png) S: 0.9 in R[300,300 1016x538]@S(0)
-----------

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Jeff Cavill
Solved:
Last query:
Last reply:
Revision history for this message
Jeff Cavill (jcavill) said :
#1

When Settings.MinSimilarity = 0.01, a second (poor) match was possible. Here was the console output:
-----------
Region at (300,300) is 1016 x 538
Match found at (309,331) of size 1000 x 500 with score 0.9998294711112976

Match found at (316,338) of size 1000 x 500 with score 0.06849050521850586
-----------

And when the Region being searched was the whole Screen:
-----------
Region at (0,0) is 1920 x 1080
Match found at (309,331) of size 1000 x 500 with score 0.9999250173568726

Match found at (918,541) of size 1000 x 500 with score 0.07191970944404602
-----------

Revision history for this message
Jeff Cavill (jcavill) said :
#2

Running against 1.1.0-Beta3 code, here is the top of the stack trace when FindFailed is thrown:

FindFailed: can not find P(C:/Users/jcavill/AppData/Local/Temp/jeff270671578108421267.png) S: 0.9 in R[300,300 1016x538]@S(0)
  Line 1994, in file Region.java
 at org.sikuli.script.Region.handleFindFailedShowDialog(Region.java:1994)
 at org.sikuli.script.Region.handleFindFailed(Region.java:1939)
 at org.sikuli.script.Region.wait(Region.java:2137)
 at org.sikuli.script.Region.find(Region.java:2010)
 at sikuli.test.TestResize.basicPatternMatch(TestResize.java:279)

Running against 1.0.1 (with sikuli-ide.jar):

FindFailed: can not find P(C:/Users/jcavill/AppData/Local/Temp/jeff270671578108421267.png) S: 0.9 on the screen.
  Line 1574, in file Region.java
 at org.sikuli.script.Region.handleFindFailed(Region.java:1574)
 at org.sikuli.script.Region.wait(Region.java:1682)
 at org.sikuli.script.Region.find(Region.java:1590)
 at sikuli.test.TestResize.basicPatternMatch(TestResize.java:279)

In all cases, the FindFailed exception is thrown 3 seconds after the find() is attempted.

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

I will check it when I have a test case for that the next days.

Revision history for this message
Jeff Cavill (jcavill) said :
#4

The problem only occurs with a COMBINATION of
1) creating the Pattern image from scratch rather than by using Sikuli to capture screenshots, and
2) trying to match that pattern against an application with a Chromium embedded browser (from Google) in the GUI

It is very odd that the first match should succeed but the next should not.
But though I don't know the root cause, we can close this issue since I have a work-around.