Cannot verify an object on screen

Asked by Ioannis

I want to identify an object from an image if exists inside another image, I read the documentation and tested several examples but none of them seems working.

Below is my code in java:

URL path1 = "Sikuli/test1.png";
URL path2 = "Sikuli/test2.png";

Finder finder = new Finder(path1);

finder.find(new Pattern(path2));

if (finder.hasNext()) {
System.out.println("found");
}
 }

Seems like I am missing something, or sikuli cannot work without a screen object maybe (?)
Any help will be appriciated, regards.

Question information

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

URL path1 = "Sikuli/test1.png";

... is not valid Java: a String cannot be converted to an URL

Finder() and Finder.find() accept valid path strings.
Valid means, that the image file can be loaded from the given path: absolute paths always work if valid.

Relative paths can only be evaluated against a valid ImagePath.

In your case it would give:
String path1 = "Sikuli/test1.png";
Finder finder = new Finder(path1);

[error] ImagePath: find: not in ImagePath: Sikuli/test1.png
[debug] ImagePath: BundlePath: --- not set ---
[error] java.lang.NullPointerException: Cannot invoke "java.awt.image.BufferedImage.getType()" because "bImg" is null

I do not know, where your folder "Sikuli" is in your file system, but something like this would help:
ImagePath.setBundlePath("/..../somewhere/Sikuli/");

and then:
String path1 = "test1.png";
String path2 = "test2.png";
Finder finder = new Finder(path1);
finder.find(path2);

if (finder.hasNext()) {
System.out.println("found");
}

would work.

I recommend to use SikuliX 2.0.5 and Java 8+ 64-Bit, because earlier versions are no longer supported in case of problems.

Revision history for this message
Ioannis (xmagicianx) said (last edit ):
#2

Thank you for your answer. I really have no issues with image paths, I know that because I have already save 'em as files to check this situation out, when debugging. I think my issue is with the different screen sizes. I take a screenshot from a screen and compare it to another image which is different resolution. I take my images files as follow, I am posting the full code now I have my laptop with me :)

public static boolean imageExist(String kind, String fileName, int secondsWait) {
        Settings.MinSimilarity = Double.parseDouble(SerenityProperties.read("sikuli.similarity"));

        //Object to be found
        URL path = SikuliRemote.class.getClassLoader().getResource("Sikuli/" + kind + "/" + fileName);
        String absolutePath = path.getPath();

        try {
            File screenshotFile = ((TakesScreenshot) Serenity.getDriver()).getScreenshotAs(OutputType.FILE);
            Finder finder = new Finder(screenshotFile.getPath());

            finder.find(absolutePath);
            if (finder.hasNext()) {
                System.out.println("Object found.");
                return true;
            }
        } catch (Exception e) {
            SerenityUtilities.logMessage("Cannot get a screenshot");
        }

        return false;
    }

I will do some more experiments and will come back.

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

ok, not really fair to give invalid/incomplete information. costs my time.

The image to search must fit into the image to search in.

Can you help with this problem?

Provide an answer of your own, or ask Ioannis for more information if necessary.

To post a message you must log in.