- Introduction
OCR (Optical Character recognition) is a VCL type component (Windows platform only). Its main purpose is to decode text from a picture into plain text. Take a look at the video below to get the 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 TImage. In this case the Image loads the chosen picture. At the top is the ‘Select Picture’ button. The OnClick event executes TOpenPictureDialog window, in which you can load the image from file. The chosen file is shown with the TImage component into the left panel. A short video shows the Demo in action, see it 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 30 |
<strong>with</strong> OpenPictureDialog <strong>do</strong> <strong>if</strong> Execute <strong>then</strong> <strong>begin</strong> Shape.Width := 0; Shape.Height := 0; ProgressBar.Position := 0; MemoText.Clear; MemoHtml.Clear; MemoUnlv.Clear; ImageWords.Picture := <strong>nil</strong>; ImageRegions.Picture := <strong>nil</strong>; ImageTextLines.Picture := <strong>nil</strong>; ImageComponents.Picture := <strong>nil</strong>; ImageParagraphs.Picture := <strong>nil</strong>; <strong>try</strong> Image.Picture.LoadFromFile(FileName); Ocr.Picture.Assign(Image.Picture); <strong>except</strong> Image.Picture := <strong>nil</strong>; ShowMessage('Preview of this image cannot be displayed. Click Recognize button to start OCR.'); Ocr.PictureFileName := FileName; <strong>end</strong>; <strong>if</strong> <strong>not</strong> Ocr.Active <strong>then</strong> <strong>begin</strong> Ocr.DataPath := ExtractFilePath(Application.ExeName) + 'tessdata'; Ocr.Active := <strong>True</strong>; <strong>end</strong>; ButtonRecognize.Enabled := <strong>True</strong>; Image.Cursor := crCross; <strong>end</strong> |
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 TOcr component which its main purpose 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 TPageControl 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 |
<strong>if</strong> <strong>not</strong> CancelRequest <strong>then</strong> <strong>if</strong> Image.Picture <> <strong>nil</strong> <strong>then</strong> <strong>begin</strong> ImageWords.Picture.Bitmap.Assign(Image.Picture.Graphic); ImageWords.Picture.Bitmap.PixelFormat := pf32bit; ImageWords.Canvas.Brush.Color := TColor($007FFF); <strong>for</strong> i := 0 <strong>to</strong> Ocr.WordCount - 1 <strong>do</strong> ImageWords.Canvas.FrameRect(RectToShapeRect(UseShape, Ocr.Words[i], ShapeRect)); ImageRegions.Picture.Bitmap.Assign(Image.Picture.Graphic); ImageRegions.Picture.Bitmap.PixelFormat := pf32bit; ImageRegions.Canvas.Brush.Color := TColor($7F00FF); <strong>for</strong> i := 0 <strong>to</strong> Ocr.RegionCount - 1 <strong>do</strong> ImageRegions.Canvas.FrameRect(RectToShapeRect(UseShape, Ocr.Regions[i], ShapeRect)); ImageTextLines.Picture.Bitmap.Assign(Image.Picture.Graphic); ImageTextLines.Picture.Bitmap.PixelFormat := pf32bit; ImageTextLines.Canvas.Brush.Color := TColor($00FF7F); <strong>for</strong> i := 0 <strong>to</strong> Ocr.TextLineCount - 1 <strong>do</strong> ImageTextLines.Canvas.FrameRect(RectToShapeRect(UseShape, Ocr.TextLines[i], ShapeRect)); ImageComponents.Picture.Bitmap.Assign(Image.Picture.Graphic); ImageComponents.Picture.Bitmap.PixelFormat := pf32bit; ImageComponents.Canvas.Brush.Color := TColor($FF7F00); <strong>for</strong> i := 0 <strong>to</strong> Ocr.ConnectedComponentCount - 1 <strong>do</strong> ImageComponents.Canvas.FrameRect(RectToShapeRect(UseShape, Ocr.ConnectedComponents[i], ShapeRect)); ImageParagraphs.Picture.Bitmap.Assign(Image.Picture.Graphic); ImageParagraphs.Picture.Bitmap.PixelFormat := pf32bit; ImageParagraphs.Canvas.Brush.Color := TColor($7FFF00); <strong>for</strong> i := 0 <strong>to</strong> Ocr.ParagraphCount - 1 <strong>do</strong> ImageParagraphs.Canvas.FrameRect(Ocr.Paragraphs[i].Location); <strong>end</strong>; |
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.
The code for this OBR demo can be downloaded here.
Like what you see? You can get OCR for Windows 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