This is a short Processing sketch to demonstrate the use optical character recognition (OCR) with the Tesseract OCR engine. I used the Mac OSX platform for testing. Here are the steps.
Install the Tesseract in OSX with all supported languages by using Homebrew
brew install imagemagick brew install tesseract --all-languages
Install the Java JNA binding from Tess4j
I just download and unzip the package. From the dist folder, I copy the tess4j.jar to the code folder of the Processing sketch.
Install the jai_imageio.jar
It also needs the Java Advanced Imaging Image Tools from the Java archive. Copy the jai_imageio.jar from the lib folder to the code folder of the Processing sketch.
Copy the dynamic libraries
I also copy the libtesseract.dylib, liblept.dylib from the Homebrew directories to the code folder of the Processing sketch, with their loading path patched with the @loader_path.
Copy the trained language data
Finally, I copy the English trained language file, eng.traineddata, from the Homebrew directory to the data folder of the sketch. It also needs a sub-folder named tessdata.
Here is the testing result. The top section is the original test image in PNG format (400 x 300). The bottom section is the recognised string with each character shown one by one in the program.
The complete source code
import net.sourceforge.tess4j.*; import java.awt.image.BufferedImage; Tesseract ocr; BufferedImage img; PImage pimg; String res, show; int idx; void setup() { size(400, 600); background(0); ocr = new Tesseract(); ocr.setDatapath(dataPath("")); pimg = loadImage("testing.png"); img = (BufferedImage) pimg.getNative(); show = ""; idx = 0; try { res = ocr.doOCR(img); // println(res); } catch (TesseractException e) { println(e.getMessage()); } frameRate(25); } void draw() { background(0); image(pimg, 0, 0); if (idx < res.length()) { show += res.charAt(idx); idx++; } else { noLoop(); } text(show, 20, pimg.height+30); } |
The only mistake I can spot is ‘ocr’ becomes ‘cor’.