Multi Threading with LEADTOOLS OCR

The .NET OCR class library (Leadtools.Forms.Ocr) provides a common gateway to the various OCR runtime engines available with LEADTOOLS. Use the OcrEngineManager.CreateEngine method with the runtime engine type required to obtain an instance of the IOcrEngine:

// Create an instance of the OCR engine of a given type 
IOcrEngine ocrEngineInstance = OcrEngineManager.CreateEngine(ocrEngineType, useThunkServer) 
// Start up the engine and use it ... 

From this instance, you can then obtain one or more instances of IOcrDocument (or IOcrAutoRecognizeManager, which creates IOcrDocuments internally) to perform all OCR operations needed: from loading source images such as TIF and raster PDF; zoning; to recognize and export to PDF, DOC, DOCX(2007/2010), HTML or TXT final document format. To do so, perform the following steps:

// Create an instance of an OCR document from the engine 
IOcrDocument ocrDocument= ocrEngineInstance.DocumentManager.CreateDocument(); 
// Add pages, zone them, recognize them and save them 
// to the final document: 
ocrDocument.Pages.AddPages(imageFileName, null); 
ocrDocument.Recognize(null); 
ocrDocument.Save(documentFileName, DocumentFormat.Pdf, null); 

Or

// Use IOcrAutoRecognizeManager to automatically recognize a document 
IOcrAutoRecognizeJob ocrJob = ocrEngine.AutoRecognizeManager.CreateJob( 
   new OcrAutoRecognizeJobData(imageFileName, DocumentFormat.Pdf, documentFileName)); 
ocrEngine.AutoRecognizeManager.RunJob(ocrJob); 

All of these operations are performed in an engine runtime-independent manner by making calls to IOcrEngine, IOcrAutoRecognizeManager, IOcrDocument and other interfaces which interact internally with the engine runtime to perform the action required.

LEADTOOLS supports the following engine runtimes:

Contact LEADTOOLS support at https://www.leadtools.com for more information.

Multi-threaded OCR application scan be created two different ways:

Multi-threaded OCR Applications Using Multiple Engines

In this scenario, create and use a dedicated IOcrEngine instance in each thread.

All LEADTOOLS OCR engines in all platforms support this scenario.

The C# and VB demo source code can be found at the following locations:

\Examples\DotNet\CS\OcrMultiThreadingDemo

\Examples\DotNet\VB\OcrMultiThreadingDemo

Multi-threaded OCR Applications Using Multiple Documents

The IOcrDocument is a fully contained object that is used to load source images (such as TIF or raster PDF); then zoning, recognizing and exporting them to a PDF, DOC, DOCX(2007/2010), HTML or TXT format. Thus, another way to achieve multi-threading in an OCR application is to create one instance of IOcrEngine in the main thread. Then, queue work items in dedicated threads with each thread using its own IOcrDocument instance. IOcrAutoRecognizeManager is a helper interface that creates IOcrDocuments internally and can be used in the same way as described above.

The following table shows some constraints when using this scenario:

OCR Engine Type Platform Multi-Document supported
Advantage x86/x64

Yes, with an unlimited number of documents at the same time.

Professional/Arabic x86

Yes, with up to 64 documents at the same time.

Professional/Arabic x64 Yes, with up to 64 documents at the same time

The OCR Multi-threaded Demo source code shipping with LEADTOOLS shows an example of this scenario.

Sample Applications

The following lists sample applications and then recommends a way to achieve thread-safety and process integrity when using LEADTOOLS OCR engines.

OCR HTTP Web Service Version 1

An HTTP Web Service application generally runs in a session-less mode. Resources cannot be shared between multiple connections.

Create a Web Service application and add the following method:

[WebMethod] 
public void Recognize(string imageFileName, DocumentFormat format, string documentFileName) 
{ 
   // Unlock support 
   string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
   string MY_DEVELOPER_KEY = "xyz123abc"; 
   RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
   using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.[EngineTypeHere], false)) 
   { 
      // Start it 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
      // Recognize 
      IOcrAutoRecognizeJob ocrJob = ocrEngine.AutoRecognizeManager.CreateJob( 
         new OcrAutoRecognizeJobData(imageFileName, format, documentFileName)); 
      ocrEngine.AutoRecognizeManager.RunJob(ocrJob); 
   } 
} 

In this version, a web method in an HTTP Web Service is created to recognize an input image file and output a document file in a specific format.

Engines and Platforms Supported: All.

Pros: Easy to implement.

Cons: Process memory and resources are shared between all connections.

OCR HTTP Web Service Version 2

Create an x86 console application (MyOcrRecognize.exe) that performs OCR:

static void Main(string[] args) 
{ 
   // Get the parameters 
   string imageFileName = args[0]; 
   DocumentFormat format = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), args[1]); 
   string documentFileName = args[2]; 
             
   // Unlock support 
   string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
   string MY_DEVELOPER_KEY = "xyz123abc"; 
   RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
   // Create the engine without the THUNK Server 
   using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.[EngineTypeHere], false)) 
   { 
      // Start it 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
      // Recognize 
      IOcrAutoRecognizeJob ocrJob = ocrEngine.AutoRecognizeManager.CreateJob( 
         new OcrAutoRecognizeJobData(imageFileName, format, documentFileName)); 
      ocrEngine.AutoRecognizeManager.RunJob(ocrJob); 
   } 
} 

Create a Web Service application and add the following method:

[WebMethod] 
public void Recognize(string imageFileName, DocumentFormat format, string documentFileName) 
{ 
   // Call the OCR console app 
   string arguments = "\"" + imageFileName + "\"" + format.ToString() + "\"" + documentFileName + "\""; 
   Process.Start("MyOcrRecognize.exe", arguments); 
} 

In this version, two applications are created:

  1. An x86 console application that creates an IOcrEngine and performs OCR without using the THUNK Server since each instance of this application will run in its own process. Options are passed through the standard command line.

  2. A web method in an HTTP Web Service that creates a new instance of the OCR application for each request.

Engines and Platforms Supported: All.

Pros: Complete process separation and safety. Each connection uses its own dedicated process to OCR.

Cons: Performance hit from creating and destroying processes. More complex to implement than the first version.

OCR Windows Service or Server Version 1

A Windows Service or Server generally runs in a session-enabled mode. Resources can be shared between multiple connections. The server will usually create a thread to process each connection.

Typically, a Windows Service or Server has the following methods: StartServer, ProcessRequest and StopServer.

This is the first version implementation of the service/server:

void StartServer() 
{ 
   // Unlock support once here 
   string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
   string MY_DEVELOPER_KEY = "xyz123abc"; 
   RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
} 
             
void StopServer() 
{ 
} 
             
void ProcessRequest(string imageFileName, DocumentFormat format, string documentFileName) 
{ 
   // Queue the work 
   ThreadPool.QueueUserWorkItem(delegate(object o) 
   { 
      using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine([EngineTypeHere], false)) 
      { 
         // Start it 
         ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
         // Recognize 
         IOcrAutoRecognizeJob ocrJob = ocrEngine.AutoRecognizeManager.CreateJob( 
            new OcrAutoRecognizeJobData(imageFileName, format, documentFileName)); 
         ocrEngine.AutoRecognizeManager.RunJob(ocrJob); 
      } 
   }); 
} 

Engines and Platforms Supported: All.

Pros: Easy to implement.

Cons: Process memory and resources are shared between all connections.

OCR Windows Service or Server Version 2

In version 2, we will re-use MyOcrRecognize.exe from earlier to perform the OCR operation in a separate process:

void StartServer() 
{ 
} 
             
void StopServer() 
{ 
} 
             
void ProcessRequest(string imageFileName, DocumentFormat format, string documentFileName) 
{ 
   // Queue the work 
   ThreadPool.QueueUserWorkItem(delegate(object o) 
   { 
      // Call our OCR console app 
      string arguments = "\"" + imageFileName + "\"" + format.ToString() + "\"" + documentFileName + "\""; 
      Process.Start("MyOcrRecognize.exe", arguments); 
   }); 
} 

Engines and Platforms Supported: All.

Pros: No performance hit from marshalling using the THUNK Server. Complete process separation and safety. Each connection uses its own dedicated process to OCR.

Cons: Performance hit from creating and destroying processes. More complex to implement than the first version.

OCR Windows Service or Server Version 3

In version 3, the multi-document capabilities of the engine (if supported) are tapped to perform true multi-threading:

// Shared instance of IOcrEngine 
private IOcrEngine ocrEngine; 
             
void StartServer() 
{ 
   // Unlock support 
   string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
   string MY_DEVELOPER_KEY = "xyz123abc"; 
   RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
   // Start the OCR engine without using the THUNK Server 
   ocrEngine = OcrEngineManager.CreateEngine([EngineTypeHere], false)); 
   // Start it 
   ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
} 
             
void StopServer() 
{ 
   // Stop the OCR engine 
   ocrEngine.Dispose(); 
} 
             
void ProcessRequest(string imageFileName, DocumentFormat format, string documentFileName) 
{ 
   // Queue the work 
   ThreadPool.QueueUserWorkItem(delegate(object o) 
   { 
      // Recognize 
      IOcrAutoRecognizeJob ocrJob = ocrEngine.AutoRecognizeManager.CreateJob( 
         new OcrAutoRecognizeJobData(imageFileName, format, documentFileName)); 
      ocrEngine.AutoRecognizeManager.RunJob(ocrJob); 
   }); 
} 

Engines and Platforms Supported: All.

Pros: True multi-threading.

Cons: Restriction on number of recognition operations in some engines (64 in Professional and Arabic engines).

Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize