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

Commit 0e0badd

Browse files
author
Lasse Häll
committed
Update to Opencv 3.2.0, remove unused dependencies
1 parent 8758e8d commit 0e0badd

File tree

4 files changed

+42
-89
lines changed

4 files changed

+42
-89
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
dependency-reduced-pom.xml
22
example/lib/image_recognition_library_test.jar
33
target/
4-
testdroid.properties
54
run-tests.sh
65
*.zip
76
application.ipa
@@ -10,4 +9,5 @@ application.apk
109
*.project
1110
*.settings/
1211
.DS_Store
13-
12+
*.idea
13+
*.iml

library/pom.xml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.testdroid</groupId>
66
<artifactId>mobile-opencv-image-recognition-library</artifactId>
7-
<version>0.2</version>
7+
<version>0.3</version>
88
<packaging>jar</packaging>
99
<name>OpenCV Mobile Image Recognition Library</name>
1010
<description>Library for image recognition that can be used in mobile testing with for example Appium</description>
@@ -43,31 +43,21 @@
4343
<artifactId>logback-classic</artifactId>
4444
<version>1.1.7</version>
4545
</dependency>
46-
<dependency>
47-
<groupId>io.appium</groupId>
48-
<artifactId>java-client</artifactId>
49-
<version>4.1.2</version>
50-
</dependency>
5146
<dependency>
5247
<groupId>org.slf4j</groupId>
5348
<artifactId>slf4j-api</artifactId>
5449
<version>1.7.4</version>
5550
</dependency>
5651
<dependency>
57-
<groupId>opencv</groupId>
52+
<groupId>org.openpnp</groupId>
5853
<artifactId>opencv</artifactId>
59-
<version>2.4.13</version>
54+
<version>3.2.0-1</version>
6055
</dependency>
6156
<dependency>
6257
<groupId>org.json</groupId>
6358
<artifactId>json</artifactId>
6459
<version>20151123</version>
6560
</dependency>
66-
<dependency>
67-
<groupId>junit</groupId>
68-
<artifactId>junit</artifactId>
69-
<version>4.12</version>
70-
</dependency>
7161
</dependencies>
7262
<build>
7363
<plugins>

library/src/main/java/imagerecognition/AkazeImageFinder.java

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
import org.json.JSONObject;
66
import org.opencv.calib3d.Calib3d;
77
import org.opencv.core.*;
8-
import org.opencv.highgui.Highgui;
8+
import org.opencv.imgcodecs.Imgcodecs;
9+
import org.opencv.imgproc.Imgproc;
10+
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113

1214
import objects.ImageLocation;
1315
import objects.ImageSearchResult;
1416

1517
import java.io.*;
16-
import java.lang.reflect.Field;
1718
import java.math.BigDecimal;
1819
import java.math.RoundingMode;
1920
import java.util.LinkedList;
@@ -28,22 +29,20 @@ public class AkazeImageFinder {
2829
private static final Logger logger = LoggerFactory.getLogger(AkazeImageFinder.class);
2930

3031
protected double getSceneHeight(String sceneFile) {
31-
Mat img_scene = Highgui.imread(sceneFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
32-
double scene_height = img_scene.rows();
33-
return scene_height;
32+
Mat img_scene = Imgcodecs.imread(sceneFile, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
33+
return img_scene.rows();
3434
}
3535

3636
protected double getSceneWidth(String sceneFile) {
37-
Mat img_scene = Highgui.imread(sceneFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
38-
double scene_width = img_scene.cols();
39-
return scene_width;
37+
Mat img_scene = Imgcodecs.imread(sceneFile, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
38+
return img_scene.cols();
4039
}
4140

4241
protected ImageLocation findImage(String queryImageFile, String sceneFile, double tolerance) {
4342

4443
long start_time = System.nanoTime();
45-
Mat img_object = Highgui.imread(queryImageFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
46-
Mat img_scene = Highgui.imread(sceneFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
44+
Mat img_object = Imgcodecs.imread(queryImageFile, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
45+
Mat img_scene = Imgcodecs.imread(sceneFile, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
4746

4847

4948
double scene_height = img_scene.rows();
@@ -60,8 +59,8 @@ protected ImageLocation findImage(String queryImageFile, String sceneFile, doubl
6059
Mat resized_img_scene = new Mat();
6160
Size size = new Size(scene_width / resizeFactor, scene_height / resizeFactor);
6261
resize(img_scene, resized_img_scene, size);
63-
Highgui.imwrite(sceneFile, resized_img_scene);
64-
img_scene = Highgui.imread(sceneFile, Highgui.CV_LOAD_IMAGE_UNCHANGED);
62+
Imgcodecs.imwrite(sceneFile, resized_img_scene);
63+
img_scene = Imgcodecs.imread(sceneFile, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
6564
logger.info("Image was resized, resize factor is: " + resizeFactor);
6665
} else{
6766
resizeFactor = 1;
@@ -84,7 +83,7 @@ protected ImageLocation findImage(String queryImageFile, String sceneFile, doubl
8483
double initial_height = img_object.size().height;
8584
double initial_width = img_object.size().width;
8685

87-
Highgui.imwrite(sceneFile, img_scene);
86+
Imgcodecs.imwrite(sceneFile, img_scene);
8887

8988
//finding homography
9089
LinkedList<Point> objList = new LinkedList<Point>();
@@ -253,7 +252,7 @@ protected void cropImage(ImageSearchResult imageDto) {
253252
int scaleFactor = imageDto.getImageLocation().getScaleFactor();
254253
double resizeFactor = imageDto.getImageLocation().getResizeFactor();
255254

256-
Mat img_object = Highgui.imread(scene_filename);
255+
Mat img_object = Imgcodecs.imread(scene_filename);
257256

258257
int x_resized = (int) (x / resizeFactor)*scaleFactor;
259258
int y_resized = (int) (y / resizeFactor)*scaleFactor;
@@ -263,7 +262,7 @@ protected void cropImage(ImageSearchResult imageDto) {
263262
log(img_object.toString());
264263
log(croppedRect.toString());
265264
Mat croppedImage = new Mat(img_object, croppedRect);
266-
Highgui.imwrite(scene_filename, croppedImage);
265+
Imgcodecs.imwrite(scene_filename, croppedImage);
267266
}
268267

269268
private Mat drawFoundHomography(Mat img_object, String filename, Mat h) {
@@ -277,14 +276,14 @@ private Mat drawFoundHomography(Mat img_object, String filename, Mat h) {
277276

278277
Core.perspectiveTransform(obj_corners, scene_corners, h);
279278

280-
Mat img = Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_COLOR);
279+
Mat img = Imgcodecs.imread(filename, Imgcodecs.CV_LOAD_IMAGE_COLOR);
281280

282-
Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
283-
Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
284-
Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
285-
Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);
281+
Imgproc.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(0, 255, 0), 4);
282+
Imgproc.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(0, 255, 0), 4);
283+
Imgproc.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(0, 255, 0), 4);
284+
Imgproc.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(0, 255, 0), 4);
286285

287-
Highgui.imwrite(filename, img);
286+
Imgcodecs.imwrite(filename, img);
288287

289288
return scene_corners;
290289
}
@@ -377,42 +376,10 @@ private String runAkazeMatch(String object_filename, String scene_filename) thro
377376
}
378377

379378
protected static void setupOpenCVEnv() {
380-
addOpenCvToJavaLibraryPath();
381-
setSysPathAccessible();
382-
logger.info("java.library.path: " + System.getProperty("java.library.path"));
379+
nu.pattern.OpenCV.loadShared();
383380
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
384381
}
385382

386-
private static void setSysPathAccessible() {
387-
Field fieldSysPath = null;
388-
try {
389-
fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
390-
} catch (NoSuchFieldException e) {
391-
e.printStackTrace();
392-
}
393-
fieldSysPath.setAccessible(true);
394-
try {
395-
fieldSysPath.set(null, null);
396-
} catch (IllegalAccessException e) {
397-
}
398-
}
399-
400-
private static void addOpenCvToJavaLibraryPath() {
401-
String platformName = System.getProperty("os.name");
402-
logger.info(platformName);
403-
if (platformName.toLowerCase().contains("mac")) {
404-
System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + System.getProperty("user.dir") + "/lib/mac/opencv");
405-
} else if (platformName.toLowerCase().contains("win")) {
406-
if (System.getProperty("os.arch").contains("64")) {
407-
System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + System.getProperty("user.dir") + "/lib/win/opencv/x64");
408-
} else {
409-
System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + System.getProperty("user.dir") + "/lib/win/opencv/x86");
410-
}
411-
} else {
412-
System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + System.getProperty("user.dir") + "/lib/linux/opencv");
413-
}
414-
}
415-
416383
private JSONObject getJsonObject(String filename) {
417384
File jsonFile = new File(filename);
418385
InputStream is = null;

library/src/main/java/imagerecognition/ImageRecognition.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import org.apache.commons.io.FilenameUtils;
1010
import org.opencv.core.Point;
11-
import org.openqa.selenium.Dimension;
11+
import org.opencv.core.Size;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

@@ -58,26 +58,26 @@ public static ImageLocation findImage(String searchedImageFilePath, String scene
5858
ImageLocation imgLocation = imageFinder.findImage(searchedImageFilePath, sceneImageFilePath, settings.getTolerance());
5959

6060
if (imgLocation != null) {
61-
Dimension screenSize = getScreenSize(platform, sceneImageFilePath);
61+
Size screenSize = getScreenSize(platform, sceneImageFilePath);
6262
if (platform.equals(PlatformType.IOS)) {
6363
imgLocation = scaleImageRectangleForIos(screenSize, imgLocation, sceneImageFilePath);
6464
}
6565
Point center = imgLocation.getCenter();
6666
if (!isPointInsideScreenBounds(center, screenSize)) {
67-
log("Screen size is (width, height): " + screenSize.getWidth() + ", " + screenSize.getHeight());
67+
log("Screen size is (width, height): " + screenSize.width + ", " + screenSize.height);
6868
log("WARNING: Coordinates found do not match the screen --> image not found.");
6969
imgLocation = null;
7070
}
7171
}
7272
return imgLocation;
7373
}
7474

75-
private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, ImageLocation imageLocation, String sceneImageFilePath) {
75+
private static ImageLocation scaleImageRectangleForIos(Size screenSize, ImageLocation imageLocation, String sceneImageFilePath) {
7676
//for retina devices we need to recalculate coordinates
7777
double sceneHeight = imageFinder.getSceneHeight(sceneImageFilePath);
7878
double sceneWidth = imageFinder.getSceneWidth(sceneImageFilePath);
79-
int screenHeight = screenSize.getHeight();
80-
int screenWidth = screenSize.getWidth();
79+
int screenHeight = (int) screenSize.height;
80+
int screenWidth = (int) screenSize.width;
8181

8282
// Make sure screenshot size values are "landscape" for comparison
8383
if (sceneHeight > sceneWidth) {
@@ -108,7 +108,7 @@ private static ImageLocation scaleImageRectangleForIos(Dimension screenSize, Ima
108108
return imageLocation;
109109
}
110110

111-
private static boolean isPointInsideScreenBounds(Point center, Dimension screenSize) {
111+
private static boolean isPointInsideScreenBounds(Point center, Size screenSize) {
112112
return !((center.x >= screenSize.width) || (center.x < 0) || (center.y >= screenSize.height) || (center.y < 0));
113113
}
114114

@@ -280,27 +280,25 @@ private static void takeIDeviceScreenshot(String screenShotFilePath) throws Exce
280280
}
281281
}
282282

283-
private static Dimension getScreenSize(PlatformType platform, String sceneImageFilePath) throws Exception {
283+
private static Size getScreenSize(PlatformType platform, String sceneImageFilePath) throws Exception {
284284
if (platform.equals(PlatformType.IOS)) {
285285
return getIosScreenSize(sceneImageFilePath);
286286
} else {
287287
return getAndroidScreenSize();
288288
}
289289
}
290290

291-
private static Dimension getIosScreenSize(String sceneImageFilePath) throws Exception {
291+
private static Size getIosScreenSize(String sceneImageFilePath) throws Exception {
292292
String udid = getIosUdid();
293293
String productType = getIosProductType(udid);
294294
try {
295-
Dimension screenSize = getIosScreenSizePointsFromPropertiesFile(productType);
296-
return screenSize;
295+
return getIosScreenSizePointsFromPropertiesFile(productType);
297296
} catch(UnsupportedOperationException e){
298297
logger.warn("Current device not included in the ios-screen-size.properties-file. Assuming x3 Retina display.");
299298
logger.warn("Add the devices screen size information to the ios-screen-size.properties-file");
300299
int screenHeight = (int) (imageFinder.getSceneHeight(sceneImageFilePath)/3);
301300
int screenWidth = (int) (imageFinder.getSceneWidth(sceneImageFilePath)/3);
302-
Dimension screenSize = new Dimension(screenWidth, screenHeight);
303-
return screenSize;
301+
return new Size(screenWidth, screenHeight);
304302
}
305303
}
306304

@@ -313,8 +311,7 @@ private static String getIosProductType(String udid) throws IOException, Interru
313311
if (exitVal != 0) {
314312
throw new Exception("ideviceinfo process exited with value: " + exitVal);
315313
}
316-
String productType = in.readLine();
317-
return productType;
314+
return in.readLine();
318315
}
319316

320317
private static String getIosUdid() throws Exception {
@@ -325,7 +322,7 @@ private static String getIosUdid() throws Exception {
325322
return udid;
326323
}
327324

328-
private static Dimension getIosScreenSizePointsFromPropertiesFile(String productType) throws UnsupportedOperationException, Exception {
325+
private static Size getIosScreenSizePointsFromPropertiesFile(String productType) throws UnsupportedOperationException, Exception {
329326
Properties screenSizeProperties = fetchProperties();
330327
String screenDimensionString = (String) screenSizeProperties.get(productType);
331328
if (screenDimensionString == null){
@@ -337,7 +334,7 @@ private static Dimension getIosScreenSizePointsFromPropertiesFile(String product
337334
}
338335
int width = Integer.parseInt(screenDimensions[0]);
339336
int height = Integer.parseInt(screenDimensions[1]);
340-
return new Dimension(width, height);
337+
return new Size(width, height);
341338
}
342339

343340
private static Properties fetchProperties() throws Exception {
@@ -366,7 +363,7 @@ private static Properties fetchProperties() throws Exception {
366363
return iosScreenSizeProperties;
367364
}
368365

369-
private static Dimension getAndroidScreenSize() throws IOException, InterruptedException {
366+
private static Size getAndroidScreenSize() throws IOException, InterruptedException {
370367
String adb = "adb";
371368
String[] adbCommand = {adb, "shell", "dumpsys", "window"};
372369
ProcessBuilder p = new ProcessBuilder(adbCommand);
@@ -386,8 +383,7 @@ private static Dimension getAndroidScreenSize() throws IOException, InterruptedE
386383
}
387384
int width = Integer.parseInt(size[0]);
388385
int height = Integer.parseInt(size[1]);
389-
Dimension screenSize = new Dimension(width, height);
390-
return screenSize;
386+
return new Size(width, height);
391387
}
392388

393389

0 commit comments

Comments
 (0)