Showing posts with label OCR. Show all posts
Showing posts with label OCR. Show all posts

Tuesday, November 3, 2015

Testing OCR with Tesseract

Moment of truth, let's see how well the "stock" tesseract install performs.

For reference, here are the versions of the various libraries I am testing with

Anils-MacBook-Air:tesseract-test anilmurty$ tesseract -v
tesseract 3.02.02
 leptonica-1.71
  libgif 4.2.3 : libjpeg 9a : libpng 1.6.18 : libtiff 4.0.6 : zlib 1.2.8 : libwebp 0.4.4 : libopenjp2 2.1.0
Anils-MacBook-Air:tesseract-test anilmurty$ 

For testing the OCR capabilities, I went on google and found a few sample files to read. My ultimate goal is to be able to read receipts and invoices but I figure I'll start with something more basic:

TEST #1: A PNG file with lots of special characters but with no crazy formatting, like you would find on a bill or an invoice

OUTPUT: Pretty impressive. Only messed up uber
Anils-MacBook-Air:tesseract-test anilmurty$ tesseract /Users/anilmurty/Desktop/ocr-test-image-1.png test-png-1
Tesseract Open Source OCR Engine v3.02.02 with Leptonica
Anils-MacBook-Air:tesseract-test anilmurty$ cat test-png-1.txt 
The (quick) [brown] {fox} jumps!
Over the $43,456.78 #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der ,,schnelle” braune Fuchs springt
fiber den faulen Hund. Le renard brun
«rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra il cane pigro. El zorro
marrén répido salta sobre el perro
perezoso. A raposa marrom rzipida
salta sobre o cfio preguicoso.
Anils-MacBook-Air:tesseract-test anilmurty$ 



TEST #2: A PNG Format of my Blog's logo:



OUTPUT: Totally messed up the tagline!
Anils-MacBook-Air:tesseract-test anilmurty$ tesseract /Users/anilmurty/Desktop/Geeking-Out.png Geeking-out
Tesseract Open Source OCR Engine v3.02.02 with Leptonica
Anils-MacBook-Air:tesseract-test anilmurty$ cat Geeking-out.txt 
Geeking Out

’caz Fm sun a geek at man .)

Anils-MacBook-Air:tesseract-test anilmurty$ 

Monday, November 2, 2015

Installing Tesseract using Macports

Follow these steps to install Tesseract using Macports:

1. Install Tesseract dependencies: autoconf, automake, libtool, libpng (with support for jpeg and tiff) and leptonica.
2. Install tesseract. (I installed with just the english language support).
3. Set the TESSDATA_PREFIX env variable to point to the location of parent directory that contains the "tessdata" folder, which contains the eng.traineddata file (you may need to do a "find" to locate this file and point it to the correct path).



Last login: Mon Nov  2 10:37:56 on ttys000
Anils-MacBook-Air:~ anilmurty$ 
Anils-MacBook-Air:~ anilmurty$ 
Anils-MacBook-Air:~ anilmurty$ sudo port install autoconf
--->  Computing dependencies for autoconf
--->  Cleaning autoconf
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ sudo port install automake
--->  Cleaning automake
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ sudo port install libtool
--->  Cleaning libtool
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ sudo port install jpeg tiff libpng
--->  Cleaning jpeg
--->  Computing dependencies for tiff
--->  Cleaning tiff
--->  Computing dependencies for libpng
--->  Cleaning libpng
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ sudo port install leptonica
--->  Computing dependencies for leptonica
--->  Cleaning leptonica
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ sudo port selfupdate
--->  Updating MacPorts base sources using rsync
MacPorts base version 2.3.4 installed,
MacPorts base version 2.3.4 downloaded.
--->  Updating the ports tree
--->  MacPorts base is already the latest version

The ports tree has been updated. To upgrade your installed ports, you should run
  port upgrade outdated
Anils-MacBook-Air:~ anilmurty$ port upgrade outdated
Nothing to upgrade.
Anils-MacBook-Air:~ anilmurty$ 


Anils-MacBook-Air:~ anilmurty$ sudo port install tesseract-eng
--->  Computing dependencies for tesseract-eng
--->  Dependencies to be installed: tesseract
--->  Fetching archive for tesseract
--->  Attempting to fetch tesseract-3.02.02_2.darwin_14.x86_64.tbz2 from http://packages.macports.org/tesseract
--->  Attempting to fetch tesseract-3.02.02_2.darwin_14.x86_64.tbz2.rmd160 from http://packages.macports.org/tesseract
--->  Installing tesseract @3.02.02_2
--->  Activating tesseract @3.02.02_2
--->  Cleaning tesseract
--->  Fetching archive for tesseract-eng
--->  Attempting to fetch tesseract-eng-3.02_1.darwin_14.noarch.tbz2 from http://packages.macports.org/tesseract-eng
--->  Attempting to fetch tesseract-eng-3.02_1.darwin_14.noarch.tbz2.rmd160 from http://packages.macports.org/tesseract-eng
--->  Installing tesseract-eng @3.02_1
--->  Activating tesseract-eng @3.02_1
--->  Cleaning tesseract-eng
--->  Updating database of binaries
--->  Scanning binaries for linking errors
--->  No broken files found.
Anils-MacBook-Air:~ anilmurty$ 

Anils-MacBook-Air:/ anilmurty$ export TESSDATA_PREFIX="/opt/local/share"



 Quick Test

Anils-MacBook-Air:/ anilmurty$ tesseract
Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]

pagesegmode values are:
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
-l lang and/or -psm pagesegmode must occur before anyconfigfile.

Single options:
  -v --version: version info
  --list-langs: list available languages for tesseract engine
Anils-MacBook-Air:/ anilmurty$ 

Saturday, October 31, 2015

Cloning the Tesseract OCR Engine

What is Tesseract and why am I cloning it?

The real definition of a Tesseract is a "4 Dimensional Analog of a Cube" -- read more about it at this wikipedia page.

In this context, Tesseract is the name of the Optical Character Recognition (OCR) engine, originally developed at HP between 1984 and 1995 and then later on enhanced by Google and released under the Apache License 2.0. Here is the GitHub page for it.

Here is some formal documentation from the README.md, once you clone and unpack it:

=============================================================
History
=======
The engine was developed at Hewlett-Packard Laboratories Bristol and
at Hewlett-Packard Co, Greeley Colorado between 1985 and 1994, with some
more changes made in 1996 to port to Windows, and some C++izing in 1998.
A lot of the code was written in C, and then some more was written in C++.
Since then all the code has been converted to at least compile with a C++
compiler. Currently it builds under Linux with gcc 4.4.3 and under Windows
with VC++2010. The C++ code makes heavy use of a list system using macros.
This predates stl, was portable before stl, and is more efficient than stl
lists, but has the big negative that if you do get a segmentation violation,
it is hard to debug.

The most recent change is that Tesseract can now recognize 39 languages,
including Arabic, Hindi, Vietnamese, plus 3 Fraktur variants, 
is fully UTF8 capable, and is fully trainable. See TrainingTesseract for
more information on training.

Tesseract was included in UNLV's Fourth Annual Test of OCR Accuracy. 
Results were available on https://github.com/tesseract-ocr/docs/blob/master/AT-1995.pdf.
With Tesseract 2.00, scripts were included to allow anyone to reproduce 
some of these tests. See TestingTesseract for more details. 


About the Engine
================
This code is a raw OCR engine. It has limited PAGE LAYOUT ANALYSIS, simple
OUTPUT FORMATTING (txt, hocr/html), and NO UI. 
Having said that, in 1995, this engine was in the top 3 in terms of character
accuracy, and it compiles and runs on both Linux and Windows.
As of 3.01, Tesseract is fully unicode (UTF-8) enabled, and can recognize 39
languages "out of the box." Code and documentation is provided for the brave
to train in other languages. 
See [Tesseract Training wiki](https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesseract) 
for more information on training. Additional [code and extracted documentation](http://tesseract-ocr.github.io/) was generated by Doxygen.

===============================================

My goal is to try and build an app that utilizes this engine and hence, I'm "checking out" the code as below. I'm hoping to write the wrapper in Python (another item on my learning list), hence the "pytesseract" reference.



Anils-MacBook-Air:Projects anilmurty$ mkdir pytesseract
Anils-MacBook-Air:Projects anilmurty$ cd pytesseract/
Anils-MacBook-Air:pytesseract anilmurty$ git clone https://github.com/tesseract-ocr/tesseract.git
Cloning into 'tesseract'...
remote: Counting objects: 11607, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 11607 (delta 6), reused 0 (delta 0), pack-reused 11579
Receiving objects: 100% (11607/11607), 32.35 MiB | 1.38 MiB/s, done.
Resolving deltas: 100% (9073/9073), done.
Checking connectivity... done.
Anils-MacBook-Air:pytesseract anilmurty$