Any way to use Sikuli Java API to compare two screenshot file captured by other tools like webdriver?

Asked by test smart

I have been using Sikuli in several projects before, now the new project are using WebDriver as auto test tool and have a need to compare two screenshot inside browser to see if have major changes after some operation actions. If there have any way to use Sikuli's Java API to compare two saved screenshot and give their similarity score? Thanks.

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

Use the class Finder.

Transcript the example in the docs:
http://sikuli.org/docx/finder.html#finder

using the Java class definition:
http://sikuli.org/doc/java-x/org/sikuli/script/Finder.html

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#2

Hi RaiMan,

I have similar requirement where i need to compare one screenshot which is stored in my machine and other screenshot is taking it on the fly on the ios simulator.

I tried doing with finder but sikuli is not throwing any exception though screenshots are not matching :(

Can you please tell me is there any other way to compare two screenshot using sikuli.

Thank You,
Hari.

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

Have a look at the Finder class:
http://sikuli.org/docx/finder.html#Finder

A Finder returns a match iterator, which is empty, if not found. Finder does not throw exceptions.

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#4

f = Finder("/Users/hari/Desktop/Screen shot 2011-09-02 at 11.18.42 AM.png")
img= <took image screenshot from sikuli># the image you are searching - Actual

f.find(img) # find all matches

while f.hasNext(): # loop as long there is a first and more matches
        print "found: ", f.next()

I have used this code to compare two screenshots, one screenshot I have saved on my desktop and other i tok from sikuli.

Though the screenshots are not matching..the script is passing.

Can you please help me in this regard.

Thanks,
Hari.

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

@Hari

??? What is printed with the statement:
print "found: ", f.next()

Might be a problem of similarity.

Try: f.find(img, 0.99) # find all matches

So I would do it this way (since it is a 2 image compare):

f = Finder("/Users/hari/Desktop/Screen shot 2011-09-02 at 11.18.42 AM.png")
img= <took image screenshot from sikuli># the image you are searching - Actual

f.find(img, 0.99)

if f.hasNext():
        print "matching: ", f.next()
else:
        print "not matching"

Revision history for this message
Hari Kiran Vankayala (harikiran1985) said :
#6

Thanks for your help RaiMan...it worked..
Your comments really helped beginner like me :)

Revision history for this message
Shivakumar (shivaselenium) said :
#7

Why dont we use the compareTo method in the Match to compare two images....
bcoz compareTo will compare the object
region.find(new Pattern("C:\\b.png")).compareTo(region1.find(new Pattern("C:\\a.png"))

Revision history for this message
Shivakumar (shivaselenium) said :
#8

What is the difference between find(PSC ptn) and findAll(PSC ptn) both are returning the PSC. Why we require findAll, that find also does the same.

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

@ Shivarkumar

---- comment #7
Your solution does not solve the problem:
He wants to compare 2 images (one from an image file and one as a screenshot taken in the script).

---- comment #8

find() only returns the best match.

findAll() returns an Iterator of Matches of all matches found in the specified region.

--- find(PSC ptn) and findAll(PSC ptn) both are returning the PSC.
That is wrong:
<PSC> is a template def for the parameter to the function.

- find returns a Match object
- findAll returns Iterator <Match>

Revision history for this message
shawn kirsch (shawn-kirsch) said :
#10

I'm having some issues with this as well. It seems that the match is inconsistent. How is this normally handled? Since this seems like a large part of automated testing, being able to verify the results versus expected results, is this how you are doing it?

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

@ shawn
--- It seems that the match is inconsistent
what do you mean by that?
example?
more info on errors/messages?

Revision history for this message
shawn kirsch (shawn-kirsch) said :
#12

it means that totally different images one being very close to the original and one being very differnt are matching at the same percent. I found another thread that said the same thing with your post to the opencv site. I'm going at it in a different direction and it seems to work.

def verifyOutput():
    if exists(Pattern("expected_result-1.png").exact()):
        print "PASS: Expected result found."
        #save out photo to log library
    else:
        print "FAIL: Expected result not found."
        #save out photo to log library

This works very well so far. I just need to save images out and I'll be set.

Revision history for this message
shawn kirsch (shawn-kirsch) said :
#13

It just seems like this should be in the docs, because if you have 400 test cases, you aren't going to run them yourself, you will mass run them all and you need to be able to verify the result somehow.

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

@ shawn

--- comment #12

--- I'm going at it in a different direction and it seems to work.
This is exactly is how one is supposed to use Sikuli, if the image to check is to be compared with an expected_result image

--- it means that totally different images one being very close to the original and one being very differnt are matching at the same percent
definitely NO: if the matched region on the screen is the same image as the expected_result image, the score is always 1.0. If you allow Sikuli to search the whole screen for an expected_result image, that currently is not visible, then you either get a FindFailed or Sikuli finds an image that is similar with a score greater than the given by similar (standard 0.7) and might lead to weird results with scripts, that do not check the matches about correctness.
If the image is visible, but has slight differences (usually in the background and at edges caused by different renderings), then you might get a match, but with a lower score. But the lower the match score, the higher the risk to get false positives.

Therefor the usual Sikuli image processing has to be called search and not compare, as long as the search region is larger than the image.
If you restrict the search region exactly to the size of the image, then you can call it a compare (which is what was requested with this question at the beginning).

--- comment #13
What are you missing in the docs?
Might be, I did not understand your comment.

Can you help with this problem?

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

To post a message you must log in.