| Back | Main view

Using IMiS/OCR Server as webservice

Product:IMiS/OCR Server
Release:7.6.907
Date:08/25/2009

Case: This article will demonstrate how to use IMiS/OCR Server as webservice. A sample code is provided to show one of the way of using the webservice. For more information on how to set up webservice functionality in IMiS/OCR Server see Setting IMiS/OCR Server as webservice.

Description:

IMiS/OCR Server provides OCR functionality through a webservice interface as seen in the screenshot below.



IMiS/OCR Server webservice separates OCR process to several stages as follows:

Note that client can only request server to perform stages in the above order. For example, client can not request analysis after recognition. There is an exception. Client can skip analysis and request recognition of an image document right after the prepare stage. IMiS/OCR Server saves webservice request data in a local database. This data can be reset to the initial state and returned to the prepare stage with the use of the ResetDocument function. Image document and token data can be removed from IMiS/OCR Server database with the use of DeleteDocument function.

The following example shows how to OCR a document from start (i.e. supplying an image) to finish (i.e. retrieving OCR result) with IMiS/OCR Server webservice.

Notes:
1. C# code is Console Application project created in Visual Studio 2005. Add Web Reference is added using URL to the WSDL document of the IMiS/OCR Server webservice.
2. Delphi code is VCL Forms Application project created in Delphi 2009. WSDL importer is used to create webservice interface unit. HTTPRIO component with webservice URL and a button are added to the form.

C#
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
  {
    static void RecognizeAndExportDocument()
    {
      try
      {
        // Create IMiSOCRServerservice object
        IMiSOCRServer.IMiSOCRServerservice ServiceObj = new IMiSOCRServer.IMiSOCRServerservice();


        /*** 1. PREPARE DOCUMENT STAGE ***/
        // Read a document into byte array
        FileStream InputStream = new FileStream("c:\\test.tif", FileMode.Open);
        byte[] Image = new byte[InputStream.Length];
        InputStream.Read(Image, 0, Image.Length);
        InputStream.Close();

        // Get IMiS/OCR Server job token
        string Token = ServiceObj.PrepareDocument(Image);


        /*** 2. ANALYZE DOCUMENT STAGE ***/
        // Create and set AnalysisParams object
        IMiSOCRServer.AnalysisParams AParams = new IMiSOCRServer.AnalysisParams();
        AParams.LayoutType = IMiSOCRServer.LayoutType.Autodetect;
        AParams.DeskewImage = true;
        AParams.RemoveNoise = true;

        // Create and set AnalysisReply object
        IMiSOCRServer.AnalysisReply AReply = new IMiSOCRServer.AnalysisReply();

        // Send AnalyzeDocument request
        ServiceObj.AnalyzeDocument(Token, AParams, AReply);


        /*** 3. WAIT FOR ANALYZE STAGE TO COMPLETE ***/
        // Check analysis progress details - finished when 100 (success) or -1 (error)
        IMiSOCRServer.ProgressDetails ProgressObj = ServiceObj.QueryProgress(Token);
        while ((ProgressObj.Stage != IMiSOCRServer.RequestStage.Analysis) ||
          ((ProgressObj.Progress != 100) && (ProgressObj.Progress != -1)))
        {
          Thread.Sleep(100); // Give IMiS/OCR Server time to process request
          ProgressObj = ServiceObj.QueryProgress(Token);
        }


        /*** 4. RECOGNIZE DOCUMENT STAGE ***/
        // Create and set RecognitionParams object
        IMiSOCRServer.RecognitionParams RParams = new IMiSOCRServer.RecognitionParams();
        RParams.ErrorMarkingLevel = IMiSOCRServer.ErrorMarkingLevel.None;
        RParams.Languages = new IMiSOCRServer.Language[2] { IMiSOCRServer.Language.English,
          IMiSOCRServer.Language.Slovenian };
        RParams.TextType = IMiSOCRServer.TextType.Normal;

        // Create and set RecognitionReply object
        IMiSOCRServer.RecognitionReply RReply = new IMiSOCRServer.RecognitionReply();

        // Send RecognizeDocument request
        ServiceObj.RecognizeDocument(Token, RParams, RReply);


        /*** 5. WAIT FOR RECOGNIZE STAGE TO COMPLETE ***/
        // Check recognition progress details - finished when 100 (success) or -1 (error)
        ProgressObj = ServiceObj.QueryProgress(Token);
        while ((ProgressObj.Stage != IMiSOCRServer.RequestStage.Recognition) ||
          ((ProgressObj.Progress != 100) && (ProgressObj.Progress != -1)))
        {
          Thread.Sleep(100); // Give IMiS/OCR Server time to process request
          ProgressObj = ServiceObj.QueryProgress(Token);
        }


        /*** 6. EXPORT DOCUMENT STAGE ***/
        // Create and set ExportParams object
        IMiSOCRServer.ExportParams EParams = new IMiSOCRServer.ExportParams();
        EParams.FileFormat = IMiSOCRServer.FileFormat.PDF;
        EParams.PDFParams = new IMiSOCRServer.PDFParams();
        EParams.PDFParams.SaveMode = IMiSOCRServer.PDFMode.ImageOnText;
        EParams.PDFParams.UncertainWordsAsImage = false;
        EParams.PDFParams.KeepColor = false;
        EParams.PDFParams.PictureQuality = 50;
        EParams.PDFParams.PictureResolution = 200;

        // Create and set ExportReply object
        IMiSOCRServer.ExportReply EReply = new IMiSOCRServer.ExportReply();

        // Send ExportDocument request
        ServiceObj.ExportDocument(Token, EParams, EReply);


        /*** 7. WAIT FOR EXPORT STAGE TO COMPLETE ***/
        // Check export progress details - finished when 100 (success) or -1 (error)
        ProgressObj = ServiceObj.QueryProgress(Token);
        while ((ProgressObj.Stage != IMiSOCRServer.RequestStage.Export) ||
          ((ProgressObj.Progress != 100) && (ProgressObj.Progress != -1)))
        {
          Thread.Sleep(100); // Give IMiS/OCR Server time to process request
          ProgressObj = ServiceObj.QueryProgress(Token);
        }


        /*** 8. RETRIEVE EXPORTED DOCUMENT ***/
        // Get ExportResult object
        IMiSOCRServer.ExportResult ExportResult = ServiceObj.GetExportResult(Token);

        // Set output file
        String FileName = Path.ChangeExtension(Path.GetTempFileName(), ".pdf");

        // Read byte array into output file        
        FileStream OutputStream = new FileStream(FileName, FileMode.Create);
        OutputStream.Write(ExportResult.OutputFile, 0, ExportResult.OutputFile.Length);
        OutputStream.Close();

        // Execute output file with associated application
        System.Diagnostics.Process.Start(FileName);
      }
      catch (Exception ex)
      {
        // show error message
        Console.Out.WriteLine(String.Format("Error: {0}: {1}", ex.GetType().FullName,
          ex.Message));
      }
    }

    static void Main(string[] args)
    {
      RecognizeAndExportDocument();
    }
  }
}
 
 
Delphi
 
unit Unit1;

interface

uses
  Windows, SysUtils, Classes, Forms, Dialogs, Controls, StdCtrls, InvokeRegistry,
  Rio, SOAPHTTPClient, ShellAPI, Types, IMiSOCRServer1;

type
  TForm1 = class(TForm)
    HTTPRIO1: THTTPRIO;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  IMiSOCRServerWebservice: IMiSOCRServer;
  Image: TByteDynArray;
  Token: string;
  AParams: AnalysisParams;
  AReply: AnalysisReply;
  ProgressObj: ProgressDetails;
  RParams: RecognitionParams;
  RReply: RecognitionReply;
  Langs: Languages;
  EParams: ExportParams;
  EReply: ExportReply;
  EResult: ExportResult;
  FileName: string;
begin 
  try
    IMiSOCRServerWebservice := HTTPRIO1 as IMiSOCRServer;


    (*** 1. PREPARE DOCUMENT STAGE ***)
    // Read a document into byte array
    with TFileStream.Create('C:\test.tif', fmOpenRead) do
    try
      SetLength(Image, Size);
      Read(Image[0], Size);
    finally
      Free;
    end;

    // Get IMiS/OCR Server job token
    Token := IMiSOCRServerWebservice.PrepareDocument(Image);


    (*** 2. ANALYZE DOCUMENT STAGE ***)
    // Create and set AnalysisParams object
    AParams := AnalysisParams.Create;
    AParams.LayoutType := LayoutType.Autodetect;
    AParams.DeskewImage := True;
    AParams.RemoveNoise := True;

    // Create and set AnalysisReply object
    AReply := AnalysisReply.Create;

    // Send AnalyzeDocument request
    IMiSOCRServerWebservice.AnalyzeDocument(Token, AParams, AReply);


    (*** 3. WAIT FOR ANALYZE STAGE TO COMPLETE ***)
    // Check analysis progress details - finished when 100 (success) or -1 (error)
    ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    while ((ProgressObj.Stage <> RequestStage.Analysis) or
      ((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
      Sleep(100); // Give IMiS/OCR Server time to process request
      ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    end;


    (*** 4. RECOGNIZE DOCUMENT STAGE ***)
    // Create and set RecognitionParams object
    RParams := RecognitionParams.Create;
    RParams.ErrorMarkingLevel := ErrorMarkingLevel.None;
    SetLength(Langs, 2);
    Langs[0] := Language.English;
    Langs[1] := Language.Slovenian;
    RParams.Languages := Langs;
    RParams.TextType := TextType.Normal;

    // Create and set RecognitionReply object
    RReply := RecognitionReply.Create;

    // Send RecognizeDocument request
    IMiSOCRServerWebservice.RecognizeDocument(Token, RParams, RReply);


    (*** 5. WAIT FOR RECOGNIZE STAGE TO COMPLETE ***)
    // Check analysis progress details - finished when 100 (success) or -1 (error)
    ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    while ((ProgressObj.Stage <> RequestStage.Recognition) or
      ((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
      Sleep(100); // Give IMiS/OCR Server time to process request
      ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    end;


    (*** 6. EXPORT DOCUMENT STAGE ***)
    // Create and set ExportParams object
    EParams := ExportParams.Create;
    EParams.FileFormat := FileFormat.PDF;
    EParams.PDFParams := PDFParams.Create;
    EParams.PDFParams.SaveMode := PDFMode.ImageOnText;
    EParams.PDFParams.UncertainWordsAsImage := False;
    EParams.PDFParams.KeepColor := False;
    EParams.PDFParams.PictureQuality := 50;
    EParams.PDFParams.PictureResolution := 200;

    // Create and set ExportReply object
    EReply := ExportReply.Create;

    // Send ExportDocument request
    IMiSOCRServerWebservice.ExportDocument(Token, EParams, EReply);


    (*** 7. WAIT FOR EXPORT STAGE TO COMPLETE ***)
    // Check export progress details - finished when 100 (success) or -1 (error)
    ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    while ((ProgressObj.Stage <> RequestStage.Export) or
      ((ProgressObj.Progress <> 100) and (ProgressObj.Progress <> -1))) do begin
      Sleep(100); // Give IMiS/OCR Server time to process request
      ProgressObj := IMiSOCRServerWebservice.QueryProgress(Token);
    end;


    (*** 8. RETRIEVE EXPORTED DOCUMENT ***)
    // Get ExportResult object
    EResult := IMiSOCRServerWebservice.GetExportResult(Token);

    // Set output file name
    FileName := ChangeFileExt(Application.ExeName, '.pdf');

    // Read byte array into output file
    with TFileStream.Create(FileName, fmCreate) do
    try
      Write(EResult.OutputFile[0], Length(EResult.OutputFile));
    finally
      Free;
    end;

    // Execute output file with associated application
    ShellExecute(0, PChar('OPEN'), PChar(FileName), nil, nil, SW_SHOWNORMAL)
  except
    on E: Exception do
      ShowMessageFmt('Error: %s: %s', [E.ClassName, E.Message]);
  end;
end;

end.
 


Related Documents:

Database 'IMiS Knowledge database', View 'All Documents', Document 'Setting IMiS/OCR Server as webservice' Setting IMiS/OCR Server as webservice
Database 'IMiS Knowledge database', View 'All Documents', Document 'IMiS/OCR Server 7.6.907' IMiS/OCR Server 7.6.907

| Back | Main view