Optical Character Recognition(OCR) in C#

Hello everyone! Welcome to my very first article on diGit. In this article, we will look at how to perform Optical Character Recognition (OCR) in C#.

For this we will use Puma.net library which makes our task much easier. You can download it from CodePlex, which hosts Open Source Projects.

After downloading, run the downloaded file and install it in your machine. I will illustrate this to you by using a small WPF application. After installing Puma .Net go to the “Assemblies” folder in the installation location. Usually they are in C:\Program Files (x86)\Puma.NET\Assemblies (in a 64-bit machine).

You should see three DLL(Dynamic-Link Library or .dll) files as follows.

Figure (a)

Next go to the properties of the”COM Server” folder which resides at C:\Program Files (x86)\Puma.NET

Go to the security tab and click edit.

Figure (b)

And make sure you have given full control for the current user.

Next open Visual Studio (I am using 2012 version) and go to File=> new=> project

Figure (c)

Under “Windows” category choose “WPF Application” and under “Name” give a name for your application and click OK.

Right click on your project and select “Add Reference” and go to “Browse” section.

Right click on your project and select “Rebuild”. After rebuild completes right click on the project again and select “open folder in file explorer”. Go to Bin => Debug and you must have “Puma.Net.dll” , and “puma.interop.dll” files inside that folder. From the location in figure (a) you must copy “dibapi.dll” file into this location. Now your Debug folder must have the following content:

Figure (e)

Now go to “MainWindow.xaml ” in our WPF Application and create an interface like the following by simply using the items in the toolbox.

Figure (f)

The first File Selector is used to select the source image file. We can get the output of the OCR as a string or we can directly write it into a file. The second file selector is used to select the destination file for the second scenario.

So now let’s look at the code written in event handlers.

First let’s consider the click event of the “Select Source” button.

Figure (g)

Here I have created an object from the “OpenFileDialog” class which helps us to locate the source image that we use to read. The “ShowDialog()” method in this class will open a file chooser window and it returns a boolean value which indicates whether a file has been selected or not. And I have taken the path of the selected file using the “FileName” property and set it into the first TextBox in Figure (f).

Now let’s consider the actions caused by the click event of the “Read to screen” button

Figure (h)

Here we are creating an object from the “PumaPage” class and we have to pass the path of the source image file which we got in the previous step into the constructor of this object. And then we can set the output format we want (line 54), and set the language which is used in the source image(line 55). Using the “RecongnizeToString()” method we can take the output of the reading into a string. I have applied that string into a “TextBlock” in our MainPage.xaml. Finally I have called the “Dispose()” method so this PumaPage object will be disposed allowing another PumaPage object to get hold of the source image file.

Here is the code under “SelectDestination” button

Figure (i)

This has a similar functionality as the “Select Source” button in Figure (g) and it will let us select the destination file where we would like to save the output if we plan to save it in a file. And that path is assigned to the Second TextBox in the MainWindow.xaml page.

Click event in the “Read to File” button

Figure (j)

The functionality of this event is similar to “read to screen” in Figure (h). Instead of the “RecongnizeToString()” method we must use the “RecognizeToFile()” method (line 85) and pass the path of the destination file which we got in the previous step, as a parameter.

Before running the application make sure to add a reference to the “System.Drawing” assembly by going to the Reference Manager in the same way we did in Figure (d). Under “Assemblies” section select “FrameWork” and select “System.Drawing ” as shown in below and click OK.

Figure (k)

This is the image which I will use for recognition:

Figure (l)

Now let’s run our application by clicking on “Start” button.

This is the result which I got by clicking the “Read to Screen” button after selecting the above source file through “Select Source” button.

Figure (m)

And we can also use the “Read to File” button after selecting a destination file using the “Select Destination” button.

Figure (n)

Here is the destination file after the operation:

Figure (o)

There are some other properties under “PumaPage” class which can be set for much sensitive and thorough recognition, just play around with them a little bit and you will understand their usage and power in no time.

I hope you understood my explanation and learned something new. If you need any further clarifications, please feel free to contact me. And since this is my very first article I very much appreciate your feedback in order to make my future work much beneficial to you all!

Online IT magazine founded in February 2009 with 50+ contributors. Up-to-date and comprehensive coverage of IT news with focus on bringing out IT news from all corners of Sri Lanka. The Sri Lankan IT site that engages with an unprecedented 150,000 readers on a monthly basis. Get in touch with us if you have news that needs publishing on our site or to cover an event in Sri Lanka.