Skip to content

Commit

Permalink
fixed: OCR/TextRecognizer: Mac: Java 11+: problem with loading libtes…
Browse files Browse the repository at this point in the history
…seract.dylib (jna.library.path)
  • Loading branch information
RaiMan committed Aug 25, 2020
1 parent 257427e commit 49f1bf3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
3 changes: 2 additions & 1 deletion API/pom.xml
Expand Up @@ -81,6 +81,7 @@
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>4.3.0-2</version>
<!-- https://github.com/openpnp/opencv -->
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand All @@ -107,7 +108,7 @@
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.2</version>
<version>4.5.3</version>
<!--License Apache 2-->
<exclusions>
<exclusion>
Expand Down
6 changes: 3 additions & 3 deletions 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() {
Expand Down
18 changes: 0 additions & 18 deletions API/src/main/java/org/sikuli/script/OCR.java
Expand Up @@ -4,7 +4,6 @@

package org.sikuli.script;

import org.sikuli.basics.Commons;
import org.sikuli.basics.Debug;
import org.sikuli.basics.Settings;

Expand Down Expand Up @@ -663,23 +662,6 @@ private String logVariablesConfigs() {
//</editor-fold>

//<editor-fold desc="10 global">
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()
Expand Down
79 changes: 53 additions & 26 deletions API/src/main/java/org/sikuli/script/TextRecognizer.java
Expand Up @@ -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;
Expand All @@ -27,27 +28,30 @@

/**
* Intended to be used only internally - still public for being backward compatible
*<p></p>
* <p></p>
* <b>New projects should use class OCR</b>
*<p></p>
* <p></p>
* 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;

//<editor-fold desc="00 instance, reset">

/**
* New TextRecognizer instance using the global options.
*
* @return instance
* @deprecated no longer needed at all
*/
Expand 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();

Expand All @@ -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();
Expand Down Expand Up @@ -113,17 +138,17 @@ private ITesseract getTesseractAPI() {
}

/**
* @deprecated use OCR.reset() instead
* @see OCR#reset()
* @deprecated use OCR.reset() instead
*/
@Deprecated
public static void reset() {
OCR.globalOptions().reset();
}

/**
* @deprecated use OCR.status() instead
* @see OCR#status()
* @deprecated use OCR.status() instead
*/
@Deprecated
public static void status() {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<String> configs) {
Expand All @@ -246,10 +271,11 @@ public TextRecognizer setConfigs(List<String> configs) {
//</editor-fold>

//<editor-fold desc="10 image optimization">

/**
* @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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -370,17 +396,18 @@ protected <SFIRBS> List<Match> 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;
}
//</editor-fold>

//<editor-fold desc="99 obsolete">

/**
* @return the current screen resolution in dots per inch
* @deprecated Will be removed in future versions<br>
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 49f1bf3

Please sign in to comment.