VintaSoft Imaging .NET SDK v8.8 for .NET Framework
Vintasoft.Imaging.FormsProcessing Namespace / FormRecognitionManager Class
Members Example



In This Topic
    FormRecognitionManager Class
    In This Topic
    Provides the synchronous and asynchronous forms recognition.
    Object Model
    FormRecognitionManager ClassFormTemplateManager ClassImageCollection ClassVintasoftImage ClassTemplateMatchingCommand Class
    Syntax
    'Declaration
     
    Public Class FormRecognitionManager 
    public class FormRecognitionManager 
    public __gc class FormRecognitionManager 
    public ref class FormRecognitionManager 
    Example
    This example shows how to identify and recognize a form asynchronously.
    ''' <summary>
    ''' Recognizes single image asynchronously.
    ''' </summary>
    ''' <param name="templateManager">The template manager.</param>
    ''' <param name="image">The image.</param>
    Public Shared Sub RecognizeSingleImageAsync(templateManager As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager, image As Vintasoft.Imaging.VintasoftImage)
        ' create template matching command
        Dim templateMatching As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand()
        ' set minimal confidence
        templateMatching.MinConfidence = 0.6F
        ' set template images
        templateMatching.TemplateImages = templateManager.TemplateImages
        ' create recognition manager
        Dim recognitionManager As New Vintasoft.Imaging.FormsProcessing.FormRecognitionManager(templateMatching, templateManager)
                
        ' if your form template contains OCR fields,
        ' make sure OCR engine manager is initialized before recognition
        ' (otherwise recognition will return null (Nothing)),
        ' see OCR field recognition examples
                
        ' create recognition task
        Dim singleImageRecognitionTask As New Vintasoft.Imaging.FormsProcessing.FormRecognitionTask(image)
        ' subscribe to Finished event
        AddHandler singleImageRecognitionTask.Finished, New System.EventHandler(AddressOf singleImageRecognitionTask_Finished)
        ' subscribe to ImageRecognitionError event to output recognition errors
        AddHandler singleImageRecognitionTask.ImageRecognitionError, AddressOf singleImageRecognitionTask_ImageRecognitionError
        ' start recognition of the image
        recognitionManager.RecognizeAsync(singleImageRecognitionTask)
    End Sub
                
    ''' <summary>
    ''' Handles the Finished event of the recognition task
    ''' and outputs the recognition results.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    Private Shared Sub singleImageRecognitionTask_Finished(sender As Object, e As System.EventArgs)
        ' get the recognition task
        Dim task As Vintasoft.Imaging.FormsProcessing.FormRecognitionTask = DirectCast(sender, Vintasoft.Imaging.FormsProcessing.FormRecognitionTask)
        ' get the result of recognition
        Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = task.Results(0)
                
        ' if recognition failed with error (see ImageRecognitionError event handler output)
        If recognitionResult Is Nothing Then
            Return
        End If
                
        ' get the result of image comparison
        Dim imageCompareResult As Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult = recognitionResult.TemplateMatchingResult.ImageCompareResult
        ' if result is not reliable
        If Not imageCompareResult.IsReliable Then
            ' matching template is not found
            System.Console.WriteLine("Matching template is not found.")
        Else
            ' get recognized page
            Dim recognizedPage As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormPage = recognitionResult.RecognizedPage
            ' write page info
            System.Console.WriteLine(String.Format("Matching template: {0}; confidence: {1:F1}%.", recognizedPage.Name, imageCompareResult.Confidence * 100))
                
            ' get form field count
            If recognizedPage.Items.Count = 0 Then
                System.Console.WriteLine("No form fields were recognized.")
            Else
                System.Console.WriteLine(String.Format("Recognized form field count: {0}", recognizedPage.Items.Count))
                ' for each recognized form field
                For Each recognizedField As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormField In recognizedPage.Items
                    ' write field info
                    System.Console.WriteLine(String.Format("  Name: {0}; value: {1}; confidence: {2:F1}%", recognizedField.Name, recognizedField.Value, recognizedField.Confidence * 100))
                Next
            End If
        End If
    End Sub
                
    ''' <summary>
    ''' Handles the ImageRecognitionError event of the FormRecognitionTask.
    ''' </summary>
    Private Shared Sub singleImageRecognitionTask_ImageRecognitionError(sender As Object, e As Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs)
        System.Console.WriteLine(e.Exception.Message)
    End Sub
    /// <summary>
    /// Recognizes single image asynchronously.
    /// </summary>
    /// <param name="templateManager">The template manager.</param>
    /// <param name="image">The image.</param>
    public static void RecognizeSingleImageAsync(
        Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager templateManager, 
        Vintasoft.Imaging.VintasoftImage image)
    {
        // create template matching command
        Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand templateMatching = 
            new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand();
        // set minimal confidence
        templateMatching.MinConfidence = 0.6f;
        // set template images
        templateMatching.TemplateImages = templateManager.TemplateImages;
        // create recognition manager
        Vintasoft.Imaging.FormsProcessing.FormRecognitionManager recognitionManager = 
            new Vintasoft.Imaging.FormsProcessing.FormRecognitionManager(
                templateMatching, templateManager);
                
        // if your form template contains OCR fields,
        // make sure OCR engine manager is initialized before recognition
        // (otherwise recognition will return null (Nothing)),
        // see OCR field recognition examples
                
        // create recognition task
        Vintasoft.Imaging.FormsProcessing.FormRecognitionTask singleImageRecognitionTask = 
            new Vintasoft.Imaging.FormsProcessing.FormRecognitionTask(image);
        // subscribe to Finished event
        singleImageRecognitionTask.Finished += 
            new System.EventHandler(singleImageRecognitionTask_Finished);
        // subscribe to ImageRecognitionError event to output recognition errors
        singleImageRecognitionTask.ImageRecognitionError += singleImageRecognitionTask_ImageRecognitionError;
        // start recognition of the image
        recognitionManager.RecognizeAsync(singleImageRecognitionTask);
    }
                
    /// <summary>
    /// Handles the Finished event of the recognition task
    /// and outputs the recognition results.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private static void singleImageRecognitionTask_Finished(object sender, System.EventArgs e)
    {
        // get the recognition task
        Vintasoft.Imaging.FormsProcessing.FormRecognitionTask task = 
            (Vintasoft.Imaging.FormsProcessing.FormRecognitionTask)sender;
        // get the result of recognition
        Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult = task.Results[0];
                
        // if recognition failed with error (see ImageRecognitionError event handler output)
        if (recognitionResult == null)
            return;
                
        // get the result of image comparison
        Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult imageCompareResult =
            recognitionResult.TemplateMatchingResult.ImageCompareResult;
        // if result is not reliable
        if (!imageCompareResult.IsReliable)
        {
            // matching template is not found
            System.Console.WriteLine("Matching template is not found.");
        }
        else
        {
            // get recognized page
            Vintasoft.Imaging.FormsProcessing.FormRecognition.FormPage recognizedPage = recognitionResult.RecognizedPage;
            // write page info
            System.Console.WriteLine(string.Format(
                "Matching template: {0}; confidence: {1:F1}%.",
                recognizedPage.Name,
                imageCompareResult.Confidence * 100));
                
            // get form field count
            if (recognizedPage.Items.Count == 0)
            {
                System.Console.WriteLine("No form fields were recognized.");
            }
            else
            {
                System.Console.WriteLine(string.Format(
                    "Recognized form field count: {0}",
                    recognizedPage.Items.Count));
                // for each recognized form field
                foreach (Vintasoft.Imaging.FormsProcessing.FormRecognition.FormField recognizedField in recognizedPage.Items)
                {
                    // write field info
                    System.Console.WriteLine(string.Format(
                        "  Name: {0}; value: {1}; confidence: {2:F1}%",
                        recognizedField.Name,
                        recognizedField.Value,
                        recognizedField.Confidence * 100));
                }
            }
        }
    }
                
    /// <summary>
    /// Handles the ImageRecognitionError event of the FormRecognitionTask.
    /// </summary>
    static void singleImageRecognitionTask_ImageRecognitionError(object sender, 
        Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs e)
    {
        System.Console.WriteLine(e.Exception.Message);
    }
    Inheritance Hierarchy

    System.Object
       Vintasoft.Imaging.FormsProcessing.FormRecognitionManager

    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also