image2data

Der Tech-Blog

...weitermachen, wo OCR aufhört

Seite segmentieren, dann Texterkennung auf Segmente (sample_9.i2dspt)

Dez 012014
// *****************************************************
// * sample_9.i2dspt                                   *
// *                                                   *
// * - Retrieving the objects of an image              *
// * - Retrieving the structure of an image            *
// * - Performing an OCR on every found zone           *
// * - Showing the result in the i2dTextViewer         *
// *                                                   *
// * Press F9 to execute the code or F7/F8 to debug it *
// *                                                   *
// * Be sure that the sample data was installed!       *
// *                                                   *
// * Contact www.norpa.eu for more information         *
// *****************************************************
 
var
  // Declare some variables
  i: Integer;
  sText: String;
 
  oPage: TBitmap;
  oPageBak: TBitmap;
 
  aOCRObjects: Ti2dOCRObjects;
  aOCRZones: Ti2dOCRZones;
 
  rProcessSettings: Ti2dProcessSettings;
 
begin
  // Get the configured process settings. We must know the configured "in"-folder because that's
  // where the sample docs are located in!
  i2dGetProcessSettings(rProcessSettings);
 
  // Create the bitmap objects
  oPage := TBitmap.Create;
  oPageBak := TBitmap.Create;
 
  try
    // Load first page of the image into the bitmap object
    i2dLoadBitmap(rProcessSettings.InPath + 'sample_invoice_1.tif', 1, oPage);
 
    // Deskew the image
    i2dDeskewBitmap(oPage);
 
    // Get the image objects
    i2dGetBitmapObjects(oPage, 2, 2, 4, 11, 5, 5, 5, 5, 4, aOCRObjects);
 
    // Get the image structure
    i2dGetBitmapStructure(oPage, 200, 100, 10, 10, aOCRObjects, aOCRZones);
 
    // Backup the page
    oPageBak.Assign(oPage);
 
    // Perform an OCR on every image segment
    for i := 0 to Length(aOCRZones) - 1 do begin
      // Restore the page
      oPage.Assign(oPageBak);
 
      // Crop the page to the zones dimension
      i2dCropBitmap(oPage, aOCRZones[i].Left, aOCRZones[i].Top, aOCRZones[i].Width, aOCRZones[i].Height);
 
      // Perform the OCR on the cropped zone
      sText := sText + i2dOCRBitmap(oPage, 0, False);
      if (sText > '') and (sText[Length(sText)] <> #10) then
        sText := sText + #10;
    end;
 
    // And show it
    i2dShowInTextViewer(sText);
  finally
   // Destroy the bitmap objects and release memory
   oPage.Free;
   oPageBak.Free;
  end;
end.

Atom

powered by Nibbleblog