Multi-thread OCR with the Auto Recognize Manager - Console C#

This tutorial shows how to multi-thread the OCR engine for parallel processing in a C# Window Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to multi-thread the OCR Engine using the IOcrAutoRecognizeManager in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Before working on the Multi-thread OCR with the Auto Recognize Manager - Console C# tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

Add the Multi-threading OCR Code

With the project created, the references added, and the license set, coding can begin.

In Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Ocr; 
using Leadtools.Document.Writer; 
using System.Diagnostics; 
using System.Threading.Tasks; 

The IOcrAutoRecognizeManager interface supports using multiple threads when processing a job. The number of threads can be controlled using the MaximumThreadsPerJob property.

It is also possible to execute each job in its own thread, which is done in the code below using Parallel.ForEach.

Add a new method to the Program class, named OCR(). Call the new method inside the Main() method, below the call to SetLicense. Add the code below to the OCR() method to test four different combinations of sequential and parallel processing.

C#
static void OCR() 
{ 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      string inputDirectory = @"FILE PATH TO INPUT DIRECTORY"; 
      string outputDirectory = @"FILE PATH TO OUTPUT DIRECTORY"; 
 
      Stopwatch stopwatch = new Stopwatch(); 
 
      ocrEngine.Startup(null, null, null, null); 
      Console.WriteLine("OCR Engine started"); 
      Console.WriteLine(); 
 
      IOcrAutoRecognizeManager manager = ocrEngine.AutoRecognizeManager; 
      var files = Directory.EnumerateFiles(inputDirectory, "*.tif"); 
      manager.MaximumThreadsPerJob = 1; //1 thread per job 
 
      Console.WriteLine("Processing Everything Sequentially..."); 
      stopwatch.Reset(); 
      stopwatch.Start(); 
      Console.Write($"Processed file "); 
      foreach (var file in files) 
      { 
         Guid id = Guid.NewGuid(); //Create new file name 
         manager.Run(file, outputDirectory + id.ToString() + ".pdf", DocumentFormat.Pdf, null, null); 
         Console.Write($"{Path.GetFileName(file)}.. "); 
      } 
      stopwatch.Stop(); 
      Console.WriteLine("Processing Done."); 
      Console.WriteLine($"Total time: {stopwatch.ElapsedMilliseconds} ms (Everything Sequentially)"); 
      Console.WriteLine(); 
 
      // Set to use Maximum CPU's/Cores of current machine. 
      manager.MaximumThreadsPerJob = 0; 
 
      Console.WriteLine("Processing pages in parallel, and the directory sequentially..."); 
      stopwatch.Reset(); 
      stopwatch.Start(); 
      Console.Write($"Processed file "); 
      foreach (var file in files) 
      { 
         Guid id = Guid.NewGuid(); //Create new file name 
         manager.Run(file, outputDirectory + id.ToString() + ".pdf", DocumentFormat.Pdf, null, null); 
         Console.Write($"{Path.GetFileName(file)}.. "); 
      } 
      stopwatch.Stop(); 
      Console.WriteLine("Processing Done."); 
      Console.WriteLine($"Total time: {stopwatch.ElapsedMilliseconds} ms (pages in parallel, directory sequentially)"); 
      Console.WriteLine(); 
 
      Console.WriteLine("Processing Everything in Parallel..."); 
      stopwatch.Reset(); 
      stopwatch.Start(); 
      Console.Write($"Processed file "); 
      Parallel.ForEach(files, (file) => 
      { 
         Guid id = Guid.NewGuid(); //Create new file name 
         manager.Run(file, outputDirectory + id.ToString() + ".pdf", DocumentFormat.Pdf, null, null); 
         Console.Write($"{Path.GetFileName(file)}.. "); 
      }); 
      stopwatch.Stop(); 
      Console.WriteLine("Processing Done."); 
      Console.WriteLine($"Total time: {stopwatch.ElapsedMilliseconds} ms (Everything in Parallel)"); 
      Console.WriteLine(); 
 
      manager.MaximumThreadsPerJob = 1; //1 thread per job 
      Console.WriteLine("Processing pages sequentially, and the directory in parallel..."); 
      stopwatch.Reset(); 
      stopwatch.Start(); 
      Console.Write($"Processed file "); 
      Parallel.ForEach(files, (file) => 
      { 
         Guid id = Guid.NewGuid(); //Create new file name 
         manager.Run(file, outputDirectory + id.ToString() + ".pdf", DocumentFormat.Pdf, null, null); 
         Console.Write($"{Path.GetFileName(file)}.. "); 
      }); 
      stopwatch.Stop(); 
      Console.WriteLine("Processing Done."); 
      Console.WriteLine($"Total time: {stopwatch.ElapsedMilliseconds} ms (pages sequentially, directory in parallel)"); 
 
      Console.WriteLine(); 
      Console.WriteLine("Press Any Key..."); 
      Console.ReadKey(); 
   } 
} 

Note

Make sure to set appropriate folder names in the inputDirectory and outputDirectory variables. The input directory must contain TIFF files (*.tif) to be processed by the application created in this tutorial.

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application converts the TIFF images to PDF format and saves them to the specified location. This is repeated four times using different combinations of sequential and parallel processing.

The application displays OCR progress

Important Note

In the test shown by the image, the best results were obtained when processing pages sequentially and the directory in parallel. However, this is not always the case, and a different combination could produce better results with a different set of images or when using a different computer with a different number of CPU cores. Therefore, it is important to test with actual images that represent the use case, on the actual hardware where the application will be used.

Wrap-up

This tutorial showed how to create a console-based OCR application that uses the LEAD OCR Engine to compare the performance when using different techniques of sequential and parallel processing.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.