TsgcAIOCRWindows

Local OCR based on the OCR engine built into Windows.

Introduction

TsgcAIOCRWindows recognises the text of an image with the OCR engine included in Windows 10 and later (Windows.Media.Ocr). Nothing has to be installed or deployed with your application: the component uses the OCR language packs already installed in Windows. It can be linked to TsgcAIDocumentExtractor as the local OCR used when OCRMode is domFallback or domAlways, in place of TsgcAIOCRTesseract, and it can also be used on its own.

 

Requirements

 

Properties

PropertyTypeDefaultDescription
LanguagestringBCP-47 language tag, for example en-US or es-ES. Empty uses the languages of the user profile. Tesseract codes such as eng or eng+spa are rejected with EsgcAIOCR, and so is a language whose OCR pack is not installed.
TimeoutMsInteger60000Maximum time to decode the image and recognise the text, in milliseconds. 0 waits without limit.

 

Methods

MethodDescription
function RecognizeFile(const aFileName: string): string;Returns the text of the image, one line of text per recognised line. Raises EsgcAIOCR when the file does not exist, when it is not a supported image, when the language is not valid or not installed, or when the recognition 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;Returns True when the Windows OCR engine can be created on this machine, which needs Windows 10 or later and at least one OCR language installed. Use it to check the machine before the first extraction.
procedure GetAvailableLanguages(const aList: TStrings);Fills the list with the BCP-47 tags of the OCR languages installed in Windows.

 

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 := TsgcAIOCRWindows.Create(nil);
oOCR.Language := 'en-US';
if not oOCR.IsAvailable then
  raise Exception.Create('Windows OCR is not available');
oLanguages := TStringList.Create;
Try
  oOCR.GetAvailableLanguages(oLanguages);
  ShowMessage(oLanguages.Text);
Finally
  oLanguages.Free;
End;
ShowMessage(oOCR.RecognizeFile('receipt.png'));

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

 

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

 

See Also