ImageEn, unit ievision

TIEVisionOCR.recognize

TIEVisionOCR.recognize


Declaration

function recognize(image: TIEVisionImage; const rect: TIEVisionRect): TIEVisionWString; safecall;


Description

Perform OCR on the specified area of the image returning an ANSI string.

Parameter Description
image Source image
rect Rectangle of interest. Setting (0, 0, 0, 0) means "entire image"

Note:
A shortcut method for this is available: OCR
To find text boxes in photographs, you can use detectTexts
Exceptions from recognize will be of type EOleException. You can use E.Message and E.ErrorCode to get more detail (see example below)


Recommendations for Best Results

To improve the your OCR result ensure your input images are of good quality, and you may need to pre-process the image for optimal results. Ideally the text height should be at least 20 pixels, any rotation or skew should be corrected, low-frequency changes in brightness should be high-pass filtered and dark borders manually removed so they are not misinterpreted as characters.

For more information, see: tesseract-ocr.github.io/tessdoc/ImproveQuality


Demos

Demo  Demos\IEVision\OCR\OCR.dpr
Demo  Demos\IEVision\OCRwithLayout\OCRwithLayout.dpr


Example 1

Perform OCR:

OCR := IEVisionLib.createOCR(IEOCRLanguageList[OCR_English_language].Code);
str := OCR.recognize(ImageEnView1.IEBitmap.GetIEVisionImage(), IEVisionRect(0, 0, 0, 0)).c_str();


Example 2

Allow switching of language:

// Set language based on selection
Case LanguageRadioGroup.ItemIndex of
  0 : OCR := IEVisionLib.createOCR(IEOCRLanguageList[OCR_English_language].Code);
  1 : OCR := IEVisionLib.createOCR(IEOCRLanguageList[OCR_French_language].Code);
  2 : OCR := IEVisionLib.createOCR(IEOCRLanguageList[OCR_German_language].Code);
End;

// Perform OCR
str := OCR.recognize(ImageEnView1.IEBitmap.GetIEVisionImage(), IEVisionRect(0, 0, 0, 0)).c_str();

// Reset OCR object
OCR := nil;


Example 3

Multiple languages:

var
  langs: TIEVisionVectorString;
langs := IEVisionLib.createVectorString();
langs.push_back( IEOCRLanguageList[ OCR_English_language ].Code );  // load English
langs.push_back( IEOCRLanguageList[ OCR_Italian_language ].Code );  // load Italian
m_OCR := IEVisionLib.createOCR( '', langs );
str := m_OCR.recognize(ImageEnView1.IEBitmap.GetIEVisionImage(), IEVisionRect(0, 0, 0, 0)).c_str();


Exception handling

try
  OCR := IEVisionLib.createOCR(IEOCRLanguageList[OCR_English_language].Code);
  str := OCR.recognize(ImageEnView1.IEBitmap.GetIEVisionImage(), IEVisionRect(0, 0, 0, 0)).c_str();
except
  on E: EOleException do
    MessageDlg( Format( 'An error was encountered processing this image: %s (%d)', [ E.Message, E.ErrorCode ]), mtError, [mbOK], 0);
end;