TsgcAIOCRTesseract

Local OCR based on the Tesseract command line tool.

Introduction

TsgcAIOCRTesseract recognises the text of an image with the Tesseract OCR command line tool. It is the local OCR used by TsgcAIDocumentExtractor when OCRMode is domFallback or domAlways, and it can also be used on its own.

 

Requirements

 

Properties

PropertyTypeDefaultDescription
ExecutablePathstringtesseractPath of the Tesseract executable. The default finds it through the PATH.
LanguagestringengTesseract language codes, joined with + for several languages (for example eng+spa). Passed as -l.
PageSegModeInteger3Tesseract page segmentation mode, passed as --psm. 3 is fully automatic, 6 treats the image as one block of text.
TimeoutMsInteger60000Maximum time for one recognition, in milliseconds. 0 waits without limit. Not applied on macOS in Delphi and C++Builder; .NET applies it on every OS.
ExtraArgumentsstringExtra command line arguments appended as they are, for example --oem 1.

 

Methods

MethodDescription
function RecognizeFile(const aFileName: string): string;Returns the text of the image. Raises EsgcAIOCR when the file does not exist, when it is a PDF, or when Tesseract ends with an error or does not end within TimeoutMs.
function RecognizeStream(const aStream: TStream; const aExtension: string): string;Writes the stream to a temporary file with the given extension, recognises it and deletes the file.
function IsAvailable: Boolean;Runs tesseract --version and returns True when it succeeds. Use it to check the installation before the first extraction.

 

Events

EventDescription
OnOCRText(Sender: TObject; const aFileName: string; var aText: string)Raised with the recognised text before it is returned, so it can be cleaned or corrected.

 

Example

oOCR := TsgcAIOCRTesseract.Create(nil);
oOCR.Language := 'eng+spa';
if not oOCR.IsAvailable then
  raise Exception.Create('Tesseract is not installed');
ShowMessage(oOCR.RecognizeFile('receipt.png'));

oExtractor.OCR := oOCR;
oExtractor.OCRMode := domFallback;

 

TsgcAIOCRTesseract is available in Delphi, C++Builder and .NET. In .NET the methods are RecognizeFile(string aFileName), RecognizeStream(Stream aStream, string aExtension) and IsAvailable(), and the OnOCRText event passes the text as ref string aText.

 

See Also