-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add
GetComponentImagesExample
for Tesseract (pull #673)
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.bytedeco.javacpp-presets</groupId> | ||
<artifactId>tesseract-samples</artifactId> | ||
<version>4.0.0-1.4.4</version> | ||
<name>JavaCPP Presets Samples for Tesseract</name> | ||
|
||
<properties> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>tesseract-platform</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
58 changes: 58 additions & 0 deletions
58
...mples/src/main/java/org/bytedeco/javacpp/samples/tesseract/GetComponentImagesExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
package org.bytedeco.javacpp.samples.tesseract; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import org.bytedeco.javacpp.*; | ||
import static org.bytedeco.javacpp.lept.*; | ||
import static org.bytedeco.javacpp.tesseract.*; | ||
|
||
/** | ||
* To run this program, you need to configure: | ||
* <ul> | ||
* <li>An environment variable pointing to the dictionaries installed on the system | ||
* TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00</li> | ||
* </ul> | ||
* | ||
* @author Arnaud Jeansen | ||
*/ | ||
public class GetComponentImagesExample { | ||
public static void main(String[] args) throws Exception { | ||
BytePointer outText; | ||
|
||
TessBaseAPI api = new TessBaseAPI(); | ||
// Initialize tesseract-ocr with English, intializing tessdata path with the standard ENV variable | ||
if (api.Init(System.getenv("TESSDATA_PREFIX") + "/tessdata", "eng") != 0) { | ||
System.err.println("Could not initialize tesseract."); | ||
System.exit(1); | ||
} | ||
|
||
// Open input image with leptonica library | ||
URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Computer_modern_sample.svg/1920px-Computer_modern_sample.svg.png"); | ||
File file = Loader.cacheResource(url); | ||
PIX image = pixRead(file.getAbsolutePath()); | ||
api.SetImage(image); | ||
|
||
// Lookup all component images | ||
int[] blockIds = {}; | ||
BOXA boxes = api.GetComponentImages(RIL_TEXTLINE, true, null, blockIds); | ||
|
||
for (int i = 0; i < boxes.n(); i++) { | ||
// For each image box, OCR within its area | ||
BOX box = boxes.box(i); | ||
api.SetRectangle(box.x(), box.y(), box.w(), box.h()); | ||
outText = api.GetUTF8Text(); | ||
String ocrResult = outText.getString(); | ||
int conf = api.MeanTextConf(); | ||
|
||
String boxInformation = String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box.x(), box.y(), box.w(), box.h(), conf, ocrResult); | ||
System.out.println(boxInformation); | ||
|
||
outText.deallocate(); | ||
} | ||
|
||
// Destroy used object and release memory | ||
api.End(); | ||
pixDestroy(image); | ||
} | ||
} |