I was trying to answer a question lately on Experts Exchange. The person wanted to get a free OCR library to include in his application.
Since I never tried of one of these and had a bit of time, I did some testing.
I found an interesting free library that has been around for a long time and has been supported by major companies. This free library is Tesseract. The development was initiated by Hewlett Packard and has been sponsored by Google since 2006. It is released under the Apache License.
The downloadable demo
This month’s downloadable demo solution contains both VB and C# projects. The solution was created using Visual Studio 2017 but should also work in VS2015.
Figure 1: The demo application in action
Required reference
There are various implementations of this library available. I have decided to give a try to the most popular one. If you open the “Manage NuGet Packages for Solution…” dialog and search for tesseract (see figure 2), the first implementation by Charles Weld shows 233K downloads which is really good for a specialized component. You can also see that it seems to be still actively maintained as the latest published date is December 2018 (at the time of writing).
Figure 2: Adding the NuGet package to your solution
You can also get the package from GitHub by navigating to https://github.com/charlesw/tesseract.
Need an image
Because we will be testing an OCR, we need an image with some text on it. So, I used my favorite search engine and searched for an image appropriate to this time of the year (also packaged into the download package).
Figure 3: A test image containing text
Need some code
The library does all the job for you. If you use a different library, your code will surely change. The good news for me is that there isn’t much code to show!
Private Sub btnScan_Click(sender As Object, e As EventArgs) Handles btnScan.Click
txtResults.Clear()
Using engine = New TesseractEngine("./tessdata", "eng", EngineMode.Default)
Using img = Pix.LoadFromFile(txtSourceFile.Text)
Using page = engine.Process(img)
txtResults.Text += "Confidence = " + page.GetMeanConfidence().ToString()
txtResults.Text += Environment.NewLine + Environment.NewLine
For Each strLine As String In page.GetText().Split(CType(vbLf, Char))
txtResults.Text += strLine + Environment.NewLine
Next
End Using
End Using
End Using
End Sub
private void button1_Click(object sender, EventArgs e)
{
txtResults.Clear();
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromFile(txtSourceFile.Text))
{
using (var page = engine.Process(img))
{
txtResults.Text += "Confidence = " + page.GetMeanConfidence();
txtResults.Text += Environment.NewLine + Environment.NewLine;
foreach (string strLine in page.GetText().Split('\n'))
{
txtResults.Text += strLine + Environment.NewLine;
}
}
}
}
}
You first need to initialize the engine by special folder (more on this in the next section) and saying the language (eng in my demo – that needs to match the prefix of the traindeddata file you want to use) you want to try to decrypt the image.
Once your engine has been initialized, you need to provide an image, call the Process method. The result is returned in a string that can be parsed from the return of the GetText function. You can find each rows of text because each line is separated with a newline character (vbLf or \n). You can also call the GetMeanConfidence method to return a value representing the confidence the library has on the job it did parsing the image.
And you are ready
That’s everything you need. You can now hit F5 and run the application. The multiline textbox should show the text that was decrypted from the image.
Other components
If you want better accuracy and more flexibility, you can use one of the commercial components available. One I would recommend you test is https://products.aspose.com/ocr/net.
if you are still using an older than VS2015, you can rely on older implementations of Tesseract like the one from Tessnet2. Notice that if you are using an older version of an engine, you might have to find the tessdata files matching that version of the engine.
Conclusion
This feature can be a nice addition to your application if you need that kind of feature.
And when it is free and so easy to develop, it is even better!