- Introduction
OCR (Optical Character recognition) for FireMonkey is a FireMonkey type component (it supports Windows, macOS, iOS and Android). Its main purpose is to decode text from a picture into plain text. Check the video below for installation instructions.
2. Components in the Demo and what they do
There are two main panels – one on the left and one on the right witch keep all the other visual components inside. On the left is the TPanel, containing TImageViewer In this case the Image loads the chosen picture. At the top is the ‘Select Picture’ button. The OnClick event executes TOpenDialog window, in which you can load the image from file. The chosen file is shown with the TImageViewer component into the left panel. Watch the Demo in action the video below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
with OpenDialog do if Execute then begin ProgressBar.Value := 0; MemoText.Lines.Clear; MemoHtml.Lines.Clear; MemoUnlv.Lines.Clear; ImageViewerWords.Bitmap.Clear(TAlphaColorRec.White); ImageViewerRegions.Bitmap.Clear(TAlphaColorRec.White); ImageViewerTextLines.Bitmap.Clear(TAlphaColorRec.White); ImageViewerParagraphs.Bitmap.Clear(TAlphaColorRec.White); ImageViewerComponents.Bitmap.Clear(TAlphaColorRec.White); try ImageViewer.Bitmap.LoadFromFile(FileName); FOcr.Picture.Assign(ImageViewer.Bitmap); ImageViewer.RealignContent; except ImageViewer.Bitmap.Clear(TAlphaColorRec.White); FOcr.PictureFileName := FileName; ShowMessage('Preview of this image cannot be displayed. Click Recognize button to start OCR.'); end; if not FOcr.Active then begin FOcr.DataPath := ExtractFilePath(ParamStr(0)) + 'tessdata'; FOcr.Active := True; end; ButtonRecognize.Enabled := True; ImageViewer.Cursor := crCross; end |
There are more options on the right. When an image with text is loaded from file on the right you can press the ‘Recognize’ button which starts exaction of text in image recognizing. While the process is ACTIVE the button Recognize changes to Cancel and the process could be stopped before it is already finished. The process of text recognition itself uses the TFOcr component which its main purpse is exactly text recognition in an image.
As you can see from the above code the image with text is already assigned by the OCR component, which means the text can be gotten with the .Text property of the OCR component. This is mainly done on the Recognize button click, containing also different image settings with Canvas, etc.
All these settings can be checked, using the TTabControl component. It reacts as a tab control, which changes the content on the right depending on the chosen page. First page shows the text in its simple variant. Next there is an html format of the text, then UNLV, followed by Regions, which puts the paragraphs in a frame. ‘The Paragraph’ Tab Sheet divides the recognized text on paragraphs (if there is more than one). The ‘Text Lines’ tab sheet count the lines of the hall text. Next to it is ‘Words’, which frames each word in the recognized text. The ‘Components’ sheet tab divides the text to separate ‘components’.
You can see all them in the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
if not CancelRequest then if ImageViewer.Bitmap <> nil then begin ImageViewerWords.Bitmap.Assign(ImageViewer.Bitmap); Canvas := ImageViewerWords.Bitmap.Canvas; Canvas.Stroke.Color := TAlphaColor($FF7FFF00); Canvas.BeginScene; for i := 0 to FOcr.WordCount - 1 do Canvas.DrawRect(TRectF.Create(RectToImageRect(UseRectangle, FOcr.Words[i], Rectangle)), 0, 0, [], 100); Canvas.EndScene; ImageViewerRegions.Bitmap.Assign(ImageViewer.Bitmap); Canvas := ImageViewerRegions.Bitmap.Canvas; Canvas.Stroke.Color := TAlphaColor($FF7F00FF); Canvas.BeginScene; for i := 0 to FOcr.RegionCount - 1 do Canvas.DrawRect(TRectF.Create(RectToImageRect(UseRectangle, FOcr.Regions[i], Rectangle)), 0, 0, [], 100); Canvas.EndScene; ImageViewerTextLines.Bitmap.Assign(ImageViewer.Bitmap); Canvas := ImageViewerTextLines.Bitmap.Canvas; Canvas.Stroke.Color := TAlphaColor($FF00FF7F); Canvas.BeginScene; for i := 0 to FOcr.TextLineCount - 1 do Canvas.DrawRect(TRectF.Create(RectToImageRect(UseRectangle, FOcr.TextLines[i], Rectangle)), 0, 0, [], 100); Canvas.EndScene; ImageViewerComponents.Bitmap.Assign(ImageViewer.Bitmap); Canvas := ImageViewerComponents.Bitmap.Canvas; Canvas.Stroke.Color := TAlphaColor($FFFF7F00); Canvas.BeginScene; for i := 0 to FOcr.ConnectedComponentCount - 1 do Canvas.DrawRect(TRectF.Create(RectToImageRect(UseRectangle, FOcr.ConnectedComponents[i], Rectangle)), 0, 0, [], 100); Canvas.EndScene; ImageViewerParagraphs.Bitmap.Assign(ImageViewer.Bitmap); Canvas := ImageViewerParagraphs.Bitmap.Canvas; Canvas.Stroke.Color := TAlphaColor($FF7FFF00); Canvas.BeginScene; for i := 0 to FOcr.ParagraphCount - 1 do Canvas.DrawRect(TRectF.Create(FOcr.Paragraphs[i].Location), 0, 0, [], 100); Canvas.EndScene; end; |
If Button ’Cancel’ is pressed while text recognition is in progress the flag ‘CancelRequest’ will become true, which will cause the code above to be NOT executed and no result will be shown on the right.
You can download the Demo here.
Check out FastMath if you need faster calculations that don’t need full accuracy.
Like what you see? You can get PDF Library for Android and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4.1 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition