←Select platform

Recognize Method

Summary
Recognizes the OCR data found on this IOcrPage.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void Recognize( 
   OcrProgressCallback callback 
) 
- (BOOL)recognize:(nullable LTOcrProgressHandler)progressHandler error:(NSError **)error 
public void recognize(OcrProgressListener callback) 
void Recognize(  
   OcrProgressCallback^ callback 
)  
def Recognize(self,callback): 

Parameters

callback
Optional callback to show operation progress.

Remarks

Perform image pre-processing by calling the IOcrPage.AutoPreprocess method prior to calling Recognize.

Recognize utilizes the zone information to activate the appropriate recognition module on every zone Zones property. Each recognition module recognizes the page parts assigned to it in the zones.

If the zone collection Zones of this IOcrPage is empty (i.e. there are no zones defined), then the page-layout decomposition process will be activated automatically in order to create a zone list for the image, before recognition. Hence, AutoZone will be implicitly called.

Note: If this IOcrPage is an empty page, in other words, when the OCR engine performs automatic page decomposing with the AutoZone method and could not find any zones in it, the Recognize method will fail with an exception. It is recommended you call AutoZone and then check if there is at least one zone found by the engine (using Zones.Count). If the count is zero, do not call Recognize.

If a recognition module is not able to recognize an object (i.e. character, or checkmark etc.), this object will be marked as a rejected one. It will become marked by a rejection symbol during conversion to the final output document. Note that IOcrDocumentManager.RejectionSymbol can be set to specify the rejection symbol used in the final document.

This method uses the checking subsystem (IOcrSpellCheckManager) to either flag suspicious characters or words, or to allow auto-correction during the recognition process.

After having recognized all the zones on the page, Recognize stores the necessary information about the recognized characters internally. You can later use the IOcrDocument that owns this page to save the data to a file or memory using the many formats supported by this IOcrEngine such as Text, PDF or Microsoft Word.

To recognize a multipage document you can iterate through the pages of the IOcrDocument object and call Recognize on each page. You can The IOcrPageCollection.Recognize method will also loop through the pages in the IOcrPage in the order they are stored in the engine and call Recognize once for each page. If you save the result IOcrDocument to a disk file, you will get a document file containing as many pages as the number of items in the IOcrDocument.Pages collection.

You can use the OcrProgressCallback to show the operation progress or to abort it. For more information and an example, refer to OcrProgressCallback.

Since the format of the recognized data file is not documented, you can use GetRecognizedCharacters and SetRecognizedCharacters to examine or modify the data. Any changes you make to the recognition data will be saved in the result document when you save IOcrDocument.

After the page is successfully recognized, the value of the IsRecognized property should be true.

Use Unrecognize to clear the recognition data stored in a page.

If you are only interested in the recognition data as text (in other words, you are not planning to save the result document to disk), then you can use the GetText method and obtain the result data as a string. A common technique in OCR is to perform form processing by adding a zone manually around the required "field" and calling GetText to get the string value of the field.

Since the recognition algorithm may use the checking subsystem, you must set up the IOcrSpellCheckManager prior to calling Recognize.

To get the accuracy and timing data of the latest successful recognition process use GetLastStatistic after calling Recognize.

Note on AutoZone/Recognize and the page image: In certain cases, AutoZone and Recognize will perform image processing on the page that might result in the page being manipulated. For example, if you add a zone of type table, the engine might automatically deskew the page if required. This result in the image representation of the image to be different after AutoZone or Recognize is called. If your application has a requirement to view the image of the page, then call GetRasterImage after AutoZone or Recognize to get the latest version of the image representation of the page in case it has changed. The LEADTOOLS Main OCR C# demo does exactly that.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.Document.Writer; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public void OcrPageExample() 
{ 
   string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"); 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); 
 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Create an OCR document 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
         // Add this image to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null); 
 
         // Auto-recognize the zones in the page 
         ocrPage.AutoZone(null); 
 
         // Show its information 
         Console.WriteLine("Size: {0} by {1} pixels", ocrPage.Width, ocrPage.Height); 
         Console.WriteLine("Resolution: {0} by {1} dots/inch", ocrPage.DpiX, ocrPage.DpiX); 
         Console.WriteLine("Bits/Pixel: {0}, Bytes/Line: {1}", ocrPage.BitsPerPixel, ocrPage.BytesPerLine); 
 
         byte[] palette = ocrPage.GetPalette(); 
         int paletteEntries; 
         if (palette != null) 
            paletteEntries = palette.Length / 3; 
         else 
            paletteEntries = 0; 
 
         Console.WriteLine("Number of entries in the palette: {0}", paletteEntries); 
         Console.WriteLine("Original format of this page: {0}", ocrPage.OriginalFormat); 
         Console.WriteLine("Has this page been recognized? : {0}", ocrPage.IsRecognized); 
         ShowZonesInfo(ocrPage); 
 
         // Recognize it and save it as PDF 
         ocrPage.Recognize(null); 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 
} 
 
private void ShowZonesInfo(IOcrPage ocrPage) 
{ 
   Console.WriteLine("Zones:"); 
   foreach (OcrZone ocrZone in ocrPage.Zones) 
   { 
      int index = ocrPage.Zones.IndexOf(ocrZone); 
      Console.WriteLine("Zone index: {0}", index); 
      Console.WriteLine("  Id                  {0}", ocrZone.Id); 
      Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
      Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
      Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
      Console.WriteLine("----------------------------------"); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

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

Leadtools.Ocr Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.