Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit 7accd93

Browse files
author
Severi Haverila
committed
separate file for img rec library
1 parent 088916e commit 7accd93

File tree

5 files changed

+96
-77
lines changed

5 files changed

+96
-77
lines changed

src/main/java/TestdroidImageRecognition.java

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import io.appium.java_client.TouchAction;
2+
import library.AkazeImageFinder;
3+
import library.ImageRecognition;
4+
25
import org.opencv.core.Point;
36
import org.openqa.selenium.By;
47
import org.openqa.selenium.Dimension;
@@ -37,83 +40,7 @@ public void setQueryImageFolder() {
3740
}
3841
}
3942

40-
/**
41-
* ======================================================================================
42-
* FINDING AN IMAGE IN ANOTHER IMAGE
43-
* ======================================================================================
44-
*/
45-
46-
47-
48-
//This method calls on the Akaze scripts to find the coordinates of a given image in another image.
49-
//The "image" parameter is the image that you are searching for
50-
//The "scene" parameter is the image in which we are looking for "image"
51-
// "tolerance" sets the required accuracy for the image recognition algorithm.
52-
public Point[] findImage(String image, String scene, double tolerance) throws Exception {
53-
Point[] imgRect = new Point[0];
54-
Point[] imgRectScaled;
55-
56-
57-
log("Searching for " + image);
58-
log("Searching in " + scene);
59-
try {
60-
imgRect = imageFinder.findImage(image, scene, tolerance);
61-
} catch (Exception e) {
62-
e.printStackTrace();
63-
}
64-
65-
if (imgRect != null) {
66-
Dimension size = getScreenSizeADB();
67-
68-
if (platformName.equalsIgnoreCase("iOS")) {
69-
//for retina devices we need to recalculate coordinates
70-
double sceneHeight = imageFinder.getSceneHeight();
71-
double sceneWidth = imageFinder.getSceneWidth();
72-
73-
int screenHeight = size.getHeight();
74-
int screenWidth = size.getWidth();
75-
76-
// Make sure screenshot size values are "landscape" for comparison
77-
if (sceneHeight > sceneWidth) {
78-
double temp = sceneHeight;
79-
sceneHeight = sceneWidth;
80-
sceneWidth = temp;
81-
}
82-
83-
// Make sure screen size values are "landscape" for comparison
84-
if (screenHeight > screenWidth) {
85-
int temp = screenHeight;
86-
screenHeight = screenWidth;
87-
screenWidth = temp;
88-
}
8943

90-
if ((screenHeight<sceneHeight) && (screenWidth<sceneWidth)) {
91-
if ((screenHeight<sceneHeight/2)&&(screenWidth<sceneWidth/2)) {
92-
imgRectScaled = new Point[]{new Point(imgRect[0].x / 3, imgRect[0].y / 3), new Point(imgRect[1].x / 3, imgRect[1].y / 3), new Point(imgRect[2].x / 3, imgRect[2].y / 3), new Point(imgRect[3].x / 3, imgRect[3].y / 3), new Point(imgRect[4].x / 3, imgRect[4].y / 3)};
93-
log("Device with Retina display rendered at x3 => coordinates have been recalculated");
94-
imgRect = imgRectScaled;
95-
}
96-
else {
97-
imgRectScaled = new Point[]{new Point(imgRect[0].x / 2, imgRect[0].y / 2), new Point(imgRect[1].x / 2, imgRect[1].y / 2), new Point(imgRect[2].x / 2, imgRect[2].y / 2), new Point(imgRect[3].x / 2, imgRect[3].y / 2), new Point(imgRect[4].x / 2, imgRect[4].y / 2)};
98-
log("Device with Retina display rendered at x2 => coordinates have been recalculated");
99-
imgRect = imgRectScaled;
100-
}
101-
}
102-
}
103-
104-
Point center = imgRect[4];
105-
106-
// Check that found center coordinate isn't out of screen bounds
107-
if ((center.x >= size.width) || (center.x < 0) || (center.y >= size.height) || (center.y < 0)) {
108-
log("Screen size is (width, height): " + size.getWidth() + ", " + size.getHeight());
109-
log("WARNING: Coordinates found do not match the screen --> image not found.");
110-
imgRect = null;
111-
} else {
112-
return imgRect;
113-
}
114-
}
115-
return null;
116-
}
11744

11845
/**
11946
* ======================================================================================
@@ -218,7 +145,7 @@ private ImageSearchDTO findImageLoop(String image, ImageRecognitionSettingsDTO s
218145
// queryImageFolder is "", unless set by setQueryImageFolder()
219146
String queryImageFile = "queryimages/" + queryimageFolder + image + "_screenshot";
220147
String screenshotFile = takeScreenshot(image + "_screenshot");
221-
Point[] imgRect = findImage(queryImageFile, screenshotFile, settings.getTolerance());
148+
Point[] imgRect = ImageRecognition.findImage(queryImageFile, screenshotFile, settings, platformName, getScreenSizeADB());
222149
if (imgRect!=null){
223150
long end_time = System.nanoTime();
224151
int difference = (int) ((end_time - start_time) / 1e6 / 1000);

src/main/java/AkazeImageFinder.java renamed to src/main/java/library/AkazeImageFinder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package library;
12
import org.apache.commons.io.IOUtils;
23
import org.json.JSONArray;
34
import org.json.JSONException;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package library;
2+
3+
import org.opencv.core.Point;
4+
import org.openqa.selenium.Dimension;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
9+
import dtos.ImageRecognitionSettingsDTO;
10+
11+
public class ImageRecognition {
12+
13+
private static Logger logger = LoggerFactory.getLogger(ImageRecognition.class);
14+
private static AkazeImageFinder imageFinder = new AkazeImageFinder();
15+
private static void log(String message) {
16+
logger.info(message);
17+
}
18+
19+
20+
21+
//This method calls on the Akaze scripts to find the coordinates of a given image in another image.
22+
//The "image" parameter is the image that you are searching for
23+
//The "scene" parameter is the image in which we are looking for "image"
24+
// "tolerance" sets the required accuracy for the image recognition algorithm.
25+
public static Point[] findImage(String image, String scene, ImageRecognitionSettingsDTO settings, String platformName, Dimension screenSize) throws Exception {
26+
Point[] imgRect = new Point[0];
27+
Point[] imgRectScaled;
28+
29+
log("Searching for " + image);
30+
log("Searching in " + scene);
31+
try {
32+
imgRect = imageFinder.findImage(image, scene, settings.getTolerance());
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
}
36+
37+
if (imgRect != null) {
38+
39+
if (platformName.equalsIgnoreCase("iOS")) {
40+
//for retina devices we need to recalculate coordinates
41+
double sceneHeight = imageFinder.getSceneHeight();
42+
double sceneWidth = imageFinder.getSceneWidth();
43+
44+
int screenHeight = screenSize.getHeight();
45+
int screenWidth = screenSize.getWidth();
46+
47+
// Make sure screenshot size values are "landscape" for comparison
48+
if (sceneHeight > sceneWidth) {
49+
double temp = sceneHeight;
50+
sceneHeight = sceneWidth;
51+
sceneWidth = temp;
52+
}
53+
54+
// Make sure screen size values are "landscape" for comparison
55+
if (screenHeight > screenWidth) {
56+
int temp = screenHeight;
57+
screenHeight = screenWidth;
58+
screenWidth = temp;
59+
}
60+
61+
if ((screenHeight<sceneHeight) && (screenWidth<sceneWidth)) {
62+
if ((screenHeight<sceneHeight/2)&&(screenWidth<sceneWidth/2)) {
63+
imgRectScaled = new Point[]{new Point(imgRect[0].x / 3, imgRect[0].y / 3), new Point(imgRect[1].x / 3, imgRect[1].y / 3), new Point(imgRect[2].x / 3, imgRect[2].y / 3), new Point(imgRect[3].x / 3, imgRect[3].y / 3), new Point(imgRect[4].x / 3, imgRect[4].y / 3)};
64+
log("Device with Retina display rendered at x3 => coordinates have been recalculated");
65+
imgRect = imgRectScaled;
66+
}
67+
else {
68+
imgRectScaled = new Point[]{new Point(imgRect[0].x / 2, imgRect[0].y / 2), new Point(imgRect[1].x / 2, imgRect[1].y / 2), new Point(imgRect[2].x / 2, imgRect[2].y / 2), new Point(imgRect[3].x / 2, imgRect[3].y / 2), new Point(imgRect[4].x / 2, imgRect[4].y / 2)};
69+
log("Device with Retina display rendered at x2 => coordinates have been recalculated");
70+
imgRect = imgRectScaled;
71+
}
72+
}
73+
}
74+
75+
Point center = imgRect[4];
76+
77+
// Check that found center coordinate isn't out of screen bounds
78+
if ((center.x >= screenSize.width) || (center.x < 0) || (center.y >= screenSize.height) || (center.y < 0)) {
79+
log("Screen size is (width, height): " + screenSize.getWidth() + ", " + screenSize.getHeight());
80+
log("WARNING: Coordinates found do not match the screen --> image not found.");
81+
imgRect = null;
82+
} else {
83+
return imgRect;
84+
}
85+
}
86+
return null;
87+
}
88+
}

src/test/java/AndroidSample.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.slf4j.LoggerFactory;
88

99
import io.appium.java_client.remote.HideKeyboardStrategy;
10+
import library.AkazeImageFinder;
1011

1112
import java.util.concurrent.TimeUnit;
1213

src/test/java/iOSSample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.openqa.selenium.By;
77
import org.slf4j.LoggerFactory;
88

9+
import library.AkazeImageFinder;
10+
911
/**
1012
* Testdroid Image Recognition Sample Test
1113
*

0 commit comments

Comments
 (0)