From 49f1bf3e5330365c97085b9533050bd538119c62 Mon Sep 17 00:00:00 2001 From: RaiMan Date: Tue, 25 Aug 2020 12:46:01 +0200 Subject: [PATCH] fixed: OCR/TextRecognizer: Mac: Java 11+: problem with loading libtesseract.dylib (jna.library.path) --- API/pom.xml | 3 +- .../main/java/org/sikuli/basics/Commons.java | 6 +- API/src/main/java/org/sikuli/script/OCR.java | 18 ----- .../org/sikuli/script/TextRecognizer.java | 79 +++++++++++++------ 4 files changed, 58 insertions(+), 48 deletions(-) diff --git a/API/pom.xml b/API/pom.xml index beb2ac09c..52a61b83c 100755 --- a/API/pom.xml +++ b/API/pom.xml @@ -81,6 +81,7 @@ org.openpnp opencv 4.3.0-2 + commons-cli @@ -107,7 +108,7 @@ net.sourceforge.tess4j tess4j - 4.5.2 + 4.5.3 diff --git a/API/src/main/java/org/sikuli/basics/Commons.java b/API/src/main/java/org/sikuli/basics/Commons.java index e1a3bd541..05d1d887c 100644 --- a/API/src/main/java/org/sikuli/basics/Commons.java +++ b/API/src/main/java/org/sikuli/basics/Commons.java @@ -1,14 +1,14 @@ package org.sikuli.basics; public class Commons { - private static final String osVersionSysProp = System.getProperty("os.version"); + private static final String osName = System.getProperty("os.name").toLowerCase(); public static boolean runningWindows() { - return osVersionSysProp.startsWith("windows"); + return osName.startsWith("windows"); } public static boolean runningMac() { - return osVersionSysProp.startsWith("mac"); + return osName.startsWith("mac"); } public static boolean runningLinux() { diff --git a/API/src/main/java/org/sikuli/script/OCR.java b/API/src/main/java/org/sikuli/script/OCR.java index 745940b6c..4e7350bd0 100644 --- a/API/src/main/java/org/sikuli/script/OCR.java +++ b/API/src/main/java/org/sikuli/script/OCR.java @@ -4,7 +4,6 @@ package org.sikuli.script; -import org.sikuli.basics.Commons; import org.sikuli.basics.Debug; import org.sikuli.basics.Settings; @@ -663,23 +662,6 @@ private String logVariablesConfigs() { // // - public static void validate() { - if (!Commons.runningWindows()) { - String libPath = "/usr/local/lib"; - File libTess = new File(libPath, "libtesseract.dylib"); - if (Commons.runningLinux()) { - libTess = new File(libPath, "libtesseract.so"); - } - if (libTess.exists()) { - if (Commons.runningMac()) { - System.setProperty("jna.library.path", libPath); - } - } else { - throw new SikuliXception(String.format("OCR: validate: Tesseract library not in /usr/local/lib")); - } - } - } - /** * Resets the global options to the initial defaults. * @see OCR.Options#reset() diff --git a/API/src/main/java/org/sikuli/script/TextRecognizer.java b/API/src/main/java/org/sikuli/script/TextRecognizer.java index ca255dc3e..4d7b7b169 100755 --- a/API/src/main/java/org/sikuli/script/TextRecognizer.java +++ b/API/src/main/java/org/sikuli/script/TextRecognizer.java @@ -11,6 +11,7 @@ import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgproc.Imgproc; +import org.sikuli.basics.Commons; import org.sikuli.basics.Debug; import org.sikuli.basics.Settings; import org.sikuli.script.Finder.Finder2; @@ -27,27 +28,30 @@ /** * Intended to be used only internally - still public for being backward compatible - *

+ *

* New projects should use class OCR - *

+ *

* Implementation of the Tess4J/Tesseract API - * */ public class TextRecognizer { private TextRecognizer() { } + private static boolean isValid = false; + private static int lvl = 3; - private static final String versionTess4J = "4.5.2"; + private static final String versionTess4J = "4.5.3"; private static final String versionTesseract = "4.1.x"; private OCR.Options options; // + /** * New TextRecognizer instance using the global options. + * * @return instance * @deprecated no longer needed at all */ @@ -58,11 +62,29 @@ public static TextRecognizer start() { /** * INTERNAL + * * @param options an Options set * @return a new TextRecognizer instance */ protected static TextRecognizer get(OCR.Options options) { - RunTime.loadLibrary(RunTime.libOpenCV); + if (!isValid) { + if (Commons.runningMac()) { + String libPath = "/usr/local/lib"; + File libTess = new File(libPath, "libtesseract.dylib"); + if (libTess.exists()) { + String jnaLibPath = System.getProperty("jna.library.path"); + if (jnaLibPath == null) { + System.setProperty("jna.library.path", libPath); + } else { + System.setProperty("jna.library.path", libPath + File.pathSeparator + jnaLibPath); + } + } else { + throw new SikuliXception(String.format("OCR: validate: Tesseract library not in /usr/local/lib")); + } + } + RunTime.loadLibrary(RunTime.libOpenCV); + isValid = true; + } initDefaultDataPath(); @@ -78,6 +100,9 @@ protected static TextRecognizer get(OCR.Options options) { return textRecognizer; } + public static void validate() { + } + private ITesseract getTesseractAPI() { try { ITesseract tesseract = new Tesseract1(); @@ -113,8 +138,8 @@ private ITesseract getTesseractAPI() { } /** - * @deprecated use OCR.reset() instead * @see OCR#reset() + * @deprecated use OCR.reset() instead */ @Deprecated public static void reset() { @@ -122,8 +147,8 @@ public static void reset() { } /** - * @deprecated use OCR.status() instead * @see OCR#status() + * @deprecated use OCR.status() instead */ @Deprecated public static void status() { @@ -136,8 +161,8 @@ public static void status() { /** * @param oem * @return instance - * @deprecated Use options().oem() * @see OCR.Options#oem(OCR.OEM) + * @deprecated Use options().oem() */ @Deprecated public TextRecognizer setOEM(OCR.OEM oem) { @@ -147,8 +172,8 @@ public TextRecognizer setOEM(OCR.OEM oem) { /** * @param oem * @return instance - * @deprecated use OCR.globalOptions().oem() * @see OCR.Options#oem(int) + * @deprecated use OCR.globalOptions().oem() */ @Deprecated public TextRecognizer setOEM(int oem) { @@ -160,8 +185,8 @@ public TextRecognizer setOEM(int oem) { /** * @param psm * @return instance - * @deprecated use OCR.globalOptions().psm() * @see OCR.Options#psm(OCR.PSM) + * @deprecated use OCR.globalOptions().psm() */ @Deprecated public TextRecognizer setPSM(OCR.PSM psm) { @@ -171,8 +196,8 @@ public TextRecognizer setPSM(OCR.PSM psm) { /** * @param psm * @return instance - * @deprecated use OCR.globalOptions().psm() * @see OCR.Options#psm(int) + * @deprecated use OCR.globalOptions().psm() */ @Deprecated public TextRecognizer setPSM(int psm) { @@ -186,8 +211,8 @@ public TextRecognizer setPSM(int psm) { /** * @param dataPath * @return instance - * @deprecated use OCR.globalOptions().datapath() * @see OCR.Options#dataPath() + * @deprecated use OCR.globalOptions().datapath() */ @Deprecated public TextRecognizer setDataPath(String dataPath) { @@ -198,8 +223,8 @@ public TextRecognizer setDataPath(String dataPath) { /** * @param language * @return instance - * @deprecated use OCR.globalOptions().language() * @see OCR.Options#language(String) + * @deprecated use OCR.globalOptions().language() */ @Deprecated public TextRecognizer setLanguage(String language) { @@ -211,8 +236,8 @@ public TextRecognizer setLanguage(String language) { * @param key * @param value * @return instance - * @deprecated use OCR.globalOptions().variable(String key, String value) * @see OCR.Options#variable(String, String) + * @deprecated use OCR.globalOptions().variable(String key, String value) */ @Deprecated public TextRecognizer setVariable(String key, String value) { @@ -223,8 +248,8 @@ public TextRecognizer setVariable(String key, String value) { /** * @param configs * @return instance - * @deprecated Use OCR.globalOptions.configs(String... configs) * @see OCR.Options#configs(String...) + * @deprecated Use OCR.globalOptions.configs(String... configs) */ @Deprecated public TextRecognizer setConfigs(String... configs) { @@ -235,8 +260,8 @@ public TextRecognizer setConfigs(String... configs) { /** * @param configs * @return - * @deprecated Use options.configs * @see OCR.Options#configs(List) + * @deprecated Use options.configs */ @Deprecated public TextRecognizer setConfigs(List configs) { @@ -246,10 +271,11 @@ public TextRecognizer setConfigs(List configs) { // // + /** * @param size expected font size in pt - * @deprecated use OCR.globalOptions().fontSize(int size) * @see OCR.Options#fontSize(int) + * @deprecated use OCR.globalOptions().fontSize(int size) */ @Deprecated public TextRecognizer setFontSize(int size) { @@ -259,8 +285,8 @@ public TextRecognizer setFontSize(int size) { /** * @param height of an uppercase X in px - * @deprecated use OCR.globalOptions().textHeight(int height) * @see OCR.Options#textHeight(float) + * @deprecated use OCR.globalOptions().textHeight(int height) */ @Deprecated public TextRecognizer setTextHeight(int height) { @@ -370,10 +396,10 @@ protected List readTextItems(SFIRBS from, int level) { for (Word textItem : textItems) { Rectangle boundingBox = textItem.getBoundingBox(); Rectangle realBox = new Rectangle( - (int) (boundingBox.x * wFactor) - 1, - (int) (boundingBox.y * hFactor) - 1, - 1 + (int) (boundingBox.width * wFactor) + 2, - 1 + (int) (boundingBox.height * hFactor) + 2); + (int) (boundingBox.x * wFactor) - 1, + (int) (boundingBox.y * hFactor) - 1, + 1 + (int) (boundingBox.width * wFactor) + 2, + 1 + (int) (boundingBox.height * hFactor) + 2); lines.add(new Match(realBox, textItem.getConfidence(), textItem.getText().trim())); } return lines; @@ -381,6 +407,7 @@ protected List readTextItems(SFIRBS from, int level) { // // + /** * @return the current screen resolution in dots per inch * @deprecated Will be removed in future versions
@@ -394,8 +421,8 @@ public int getActualDPI() { /** * @param simg * @return the text read - * @deprecated use OCR.readText() instead * @see OCR#readText(Object) + * @deprecated use OCR.readText() instead */ @Deprecated public String doOCR(ScreenImage simg) { @@ -405,8 +432,8 @@ public String doOCR(ScreenImage simg) { /** * @param bimg * @return the text read - * @deprecated use OCR.readText() instead * @see OCR#readText(Object) + * @deprecated use OCR.readText() instead */ @Deprecated public String doOCR(BufferedImage bimg) { @@ -416,8 +443,8 @@ public String doOCR(BufferedImage bimg) { /** * @param simg * @return text - * @deprecated use OCR.readText() instead * @see OCR#readText(Object) + * @deprecated use OCR.readText() instead */ @Deprecated public String recognize(ScreenImage simg) { @@ -428,8 +455,8 @@ public String recognize(ScreenImage simg) { /** * @param bimg * @return text - * @deprecated use OCR.readText() instead * @see OCR#readText(Object) + * @deprecated use OCR.readText() instead */ @Deprecated public String recognize(BufferedImage bimg) {