Click here to Skip to main content
15,867,895 members
Articles / Programming Languages / C#

How to Use Tessnet2 Library

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
13 Dec 2013CPOL1 min read 61.8K   13   8
This article aims to show how to use the Tessnet2 library.

This article has the goal to show how to use the Tessnet2 library.

Tessnet2 is a .NET 2.0 Open Source OCR assembly using Tesseract engine. Download binary here.

Another important thing for Tessnet2 work is get the languages packages, get it here for the languages you want. For the sample, we are using the English language.

Let’s start the sample.

Create one console application:

console aplication

Then copy the folder with binaries and languages packages to the same folder.
Note: Don't forget to unzip the languages packages until you have the tessdata folder with eng.DangAmbigs, eng.freq-dawg, eng.inttemp, eng.normproto, eng.pffmtable, eng.unicharset, eng.user-words and eng.word-dawg files.

The aspect should be something like:

ocr test folder

Now add the Tessnet2 reference to the project, do a right click in References > Add References > Browse> Select the folder with C:\OCRTest\tessnet2\Release64\tessnet2_64.dll and click Ok.

After add the reference for System.Drawing:

reference

Now, we need to configure the project to compile in x64, for it Right Click in project> Properties>Build> Platform target.

configs

In Program.cs, add the following code:

C#
static void Main(string[] args)
        {
            try
            {
                var image = new Bitmap(@"C:\OCRTest\number.jpg");
                var ocr = new Tesseract();
                ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only
                //@"C:\OCRTest\tessdata" contains 
                //the language package, without this the method crash and app breaks
                ocr.Init(@"C:\OCRTest\tessdata", "eng", true); 
                var result = ocr.DoOCR(image, Rectangle.Empty);
                foreach (Word word in result)
                    Console.WriteLine("{0} : {1}", word.Confidence, word.Text);

                Console.ReadLine();
            }
            catch (Exception exception)
            {

            }
        }

and run the application, this gives an error:

error

To solve this, go to directory and change the App.config:

C#
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

For the test:

test

The result is:

result

You can download the source code here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionTessnet2 is deprecated... Pin
ezio200024-Aug-15 23:49
ezio200024-Aug-15 23:49 
QuestionTessnet2 not reading words correctly Pin
Member 1176598827-Jul-15 23:34
Member 1176598827-Jul-15 23:34 
QuestionThank you. Pin
Member 1132684420-Jun-15 10:44
Member 1132684420-Jun-15 10:44 
QuestionNice! It worked fine with numbers Pin
Igor Campos15-Apr-15 7:23
Igor Campos15-Apr-15 7:23 
QuestionCan this be used with Xamarin Pin
Gboye5-Apr-15 22:00
Gboye5-Apr-15 22:00 
Thank you for your article.

Please confirm if this can be used with Xamarin since it is written in C#. Will it work?

Thanks & Regards,

QuestionTessnet2 in C# is reading only one word Pin
Praveen Chougale30-Jan-15 23:32
Praveen Chougale30-Jan-15 23:32 
GeneralMy vote of 2 Pin
Pruthviraj Mohite18-May-14 22:45
Pruthviraj Mohite18-May-14 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.