Forms processing: Forms identification and recognition
In This Topic
Form identification - is a process that compares the form image with one or more images of
form templates and determines the
form template that matches the
form.
During the identification SDK calculates the transformation matrix that defines the translation,
rotation and scale of
form relative to the
form template.
Also during the identification SDK determines the confidence to the results of
form recognition.
The SDK would identify a
form, even if image of form is rotated or scaled comparing to the
form template.
The SDK would NOT identify the
form, if image of form is dimensionally distorted or warped comparing to the
form template.
Form recognition - is a process of reading data from
form fields.
During the
form recognition SDK creates a list of recognized
form fields.
For each recognized field SDK returns the field value and confidence to the recognition result.
FormRecognitionManager class is intended for identification and recognition of form.
The process of identification and recognition can be performed for a single form or for several forms at once,
that process can be synchronous or asynchronous. The class can provide information about progress of recognition.
Also class allows to apply the processing commands to the template form image and filled form image before form identification.
For synchronous recognition of form is necessary to use
FormRecognitionManager.Recognize method, for asynchronous
recognition of form is necessary to use
FormRecognitionManager.RecognizeAsync method.
The result of recognition is returned as an instance of
FormRecognitionResult class.
The class
FormRecognitionResult contains the result of
form identification -
FormRecognitionResult.TemplateMatchingResult,
and the result of form recognition -
FormRecognitionResult.RecognizedPage.
A
form field is represented by
FormField class, which determines the field name,
field value and confidence to the result of form recognition.
Also class
FormField contains reference to a template of form field.
Here is a list of standard classes, which define form fields:
- FormField - a base class for any form field
- OmrField - a form field, which defines an abstract optical mark
- OcrField - a form field, which defines a text
- FormFieldGroup - a group of form fields
- FormFieldTable - a table of form fields
Architecture of
FormField class is open, so user may define its own custom form fields if necessary.
Forms Processing Demo application contains an example of form field that defines a barcode.
Identify and recognize a document that contains only one form
For identification and recognition of document that contains only one form is necessary to do the following:
- Identify the template of document form
- Perform the form recognition
Here is an example that demonstrates how to identify and recognize a form synchronously.
/// <summary>
/// Recognizes single image synchronously.
/// </summary>
/// <param name="templateManager">The template manager.</param>
/// <param name="image">The image.</param>
public static void RecognizeSingleImageSync(
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.5f;
// 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
// subscribe to ImageRecognitionError event to output recognition errors
recognitionManager.ImageRecognitionError += recognitionManager_ImageRecognitionError;
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult = recognitionManager.Recognize(image);
// unsubscribe from ImageRecognitionError event
recognitionManager.ImageRecognitionError -= recognitionManager_ImageRecognitionError;
// 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 FormRecognitionManager.
/// </summary>
static void recognitionManager_ImageRecognitionError(object sender,
Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs e)
{
System.Console.WriteLine(e.Exception.Message);
}
''' <summary>
''' Recognizes single image synchronously.
''' </summary>
''' <param name="templateManager">The template manager.</param>
''' <param name="image">The image.</param>
Public Shared Sub RecognizeSingleImageSync(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.5F
' 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
' subscribe to ImageRecognitionError event to output recognition errors
AddHandler recognitionManager.ImageRecognitionError, AddressOf recognitionManager_ImageRecognitionError
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = recognitionManager.Recognize(image)
' unsubscribe from ImageRecognitionError event
RemoveHandler recognitionManager.ImageRecognitionError, AddressOf recognitionManager_ImageRecognitionError
' 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 FormRecognitionManager.
''' </summary>
Private Shared Sub recognitionManager_ImageRecognitionError(sender As Object, e As Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs)
System.Console.WriteLine(e.Exception.Message)
End Sub
Here is an example that demonstrates 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 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);
}
''' <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
Identify and recognize a document that contains several forms
For identification and recognition of document that contains several forms is necessary to do the following:
- Identify templates of all forms
- Perform the forms recognition
Here is an example that demonstrates how to identify and recognize several forms asynchronously.
/// <summary>
/// Recognizes array of images synchronously.
/// </summary>
/// <param name="templateManager">The template manager.</param>
/// <param name="images">The images.</param>
public static void RecognizeImagesSync(
Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager templateManager,
Vintasoft.Imaging.VintasoftImage[] images)
{
// 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
// subscribe to ImageRecognitionError event to output recognition errors
recognitionManager.ImageRecognitionError += manager_ImageRecognitionError;
// recognize the images
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult[] recognitionResults = recognitionManager.Recognize(images);
// unsubscribe from ImageRecognitionError event
recognitionManager.ImageRecognitionError -= manager_ImageRecognitionError;
// for each recognition result
for (int i = 0; i < recognitionResults.Length; i++)
{
System.Console.WriteLine(string.Format(
"Image {0} of {1}:",
i + 1,
recognitionResults.Length));
System.Console.WriteLine();
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult = recognitionResults[i];
// if recognition failed with error (see ImageRecognitionError event handler output)
if (recognitionResult == null)
continue;
// 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));
}
}
}
System.Console.WriteLine();
System.Console.WriteLine();
}
}
/// <summary>
/// Handles the ImageRecognitionError event of the FormRecognitionManager.
/// </summary>
static void manager_ImageRecognitionError(object sender,
Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs e)
{
System.Console.WriteLine(e.Exception.Message);
}
''' <summary>
''' Recognizes array of images synchronously.
''' </summary>
''' <param name="templateManager">The template manager.</param>
''' <param name="images">The images.</param>
Public Shared Sub RecognizeImagesSync(templateManager As Vintasoft.Imaging.FormsProcessing.FormRecognition.FormTemplateManager, images 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
' subscribe to ImageRecognitionError event to output recognition errors
AddHandler recognitionManager.ImageRecognitionError, AddressOf manager_ImageRecognitionError
' recognize the images
Dim recognitionResults As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult() = recognitionManager.Recognize(images)
' unsubscribe from ImageRecognitionError event
RemoveHandler recognitionManager.ImageRecognitionError, AddressOf manager_ImageRecognitionError
' for each recognition result
For i As Integer = 0 To recognitionResults.Length - 1
System.Console.WriteLine(String.Format("Image {0} of {1}:", i + 1, recognitionResults.Length))
System.Console.WriteLine()
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = recognitionResults(i)
' if recognition failed with error (see ImageRecognitionError event handler output)
If recognitionResult Is Nothing Then
Continue For
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
System.Console.WriteLine()
System.Console.WriteLine()
Next
End Sub
''' <summary>
''' Handles the ImageRecognitionError event of the FormRecognitionManager.
''' </summary>
Private Shared Sub manager_ImageRecognitionError(sender As Object, e As Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs)
System.Console.WriteLine(e.Exception.Message)
End Sub
Identify and recognize a document that contains forms with marks
For identification and recognition of document that contains forms with marks is necessary to do the following:
- Identify templates of all forms
- Perform the forms recognition
Here is an example that demonstrates how to identify and recognize a form, that contains marks.
/// <summary>
/// Recognizes the form with OMR fields.
/// </summary>
/// <param name="formRecognitionManager">The form recognition manager.</param>
/// <param name="image">The image.</param>
public static void RecognizeFormWithOmrFields(
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager,
Vintasoft.Imaging.VintasoftImage image)
{
// 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
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(image);
// 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;
// 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)
{
if (recognizedField is Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField)
{
Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField omrField =
(Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField)recognizedField;
// write field info
System.Console.WriteLine(string.Format(
" OMR field: name: {0}; state: {1}; confidence: {2:F1}%",
omrField.Name,
omrField.State,
omrField.Confidence * 100));
}
}
}
}
}
''' <summary>
''' Recognizes the form with OMR fields.
''' </summary>
''' <param name="formRecognitionManager">The form recognition manager.</param>
''' <param name="image">The image.</param>
Public Shared Sub RecognizeFormWithOmrFields(formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager, image As Vintasoft.Imaging.VintasoftImage)
' 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
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(image)
' 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
' 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
If TypeOf recognizedField Is Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField Then
Dim omrField As Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField = DirectCast(recognizedField, Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrField)
' write field info
System.Console.WriteLine(String.Format(" OMR field: name: {0}; state: {1}; confidence: {2:F1}%", omrField.Name, omrField.State, omrField.Confidence * 100))
End If
Next
End If
End If
End Sub
Here is an example that demonstrates how to identify and recognize a form, that contains bubble sheet (group of marks).
/// <summary>
/// Recognizes the form with a group of marks (OMR bubbles).
/// </summary>
/// <param name="formRecognitionManager">The form recognition manager.</param>
/// <param name="image">The image.</param>
public static void RecognizeFormWithOmrFieldTables(
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager,
Vintasoft.Imaging.VintasoftImage image)
{
// 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
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(image);
// 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;
// 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)
{
if (recognizedField is Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable)
{
Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable omrFieldTable =
(Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable)recognizedField;
Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTemplateTable omrTemplateTable =
omrFieldTable.FieldTemplate as Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTemplateTable;
// write field info
System.Console.WriteLine(string.Format(
" OMR field table: name: {0}; value: {1}; confidence: {2:F1}%; row count: {3}; column count: {4}",
omrFieldTable.Name,
omrFieldTable.Value,
omrFieldTable.Confidence * 100,
omrTemplateTable.RowCount,
omrTemplateTable.ColumnCount));
}
}
}
}
}
''' <summary>
''' Recognizes the form with a group of marks (OMR bubbles).
''' </summary>
''' <param name="formRecognitionManager">The form recognition manager.</param>
''' <param name="image">The image.</param>
Public Shared Sub RecognizeFormWithOmrFieldTables(formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager, image As Vintasoft.Imaging.VintasoftImage)
' 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
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(image)
' 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
' 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
If TypeOf recognizedField Is Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable Then
Dim omrFieldTable As Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable = DirectCast(recognizedField, Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTable)
Dim omrTemplateTable As Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTemplateTable = TryCast(omrFieldTable.FieldTemplate, Vintasoft.Imaging.FormsProcessing.FormRecognition.Omr.OmrFieldTemplateTable)
' write field info
System.Console.WriteLine(String.Format(" OMR field table: name: {0}; value: {1}; confidence: {2:F1}%; row count: {3}; column count: {4}", omrFieldTable.Name, omrFieldTable.Value, omrFieldTable.Confidence * 100, omrTemplateTable.RowCount, omrTemplateTable.ColumnCount))
End If
Next
End If
End If
End Sub
Identify and recognize a document that contains forms with text
For identification and recognition of document that contains forms with text is necessary to do the following:
- Identify templates of all forms.
- Specify the OCR engine that must be used for text recognition.
The OcrFieldTemplate.OcrEngineManager property allows to specify OCR engine, which must be used for recognizing text in OCR fields.
- Perform the forms recognition.
Here is an example that demonstrates how to identify and recognize a form, that contains text.
/// <summary>
/// Recognizes the form with OCR fields.
/// </summary>
/// <param name="formRecognitionManager">The form recognition manager.</param>
/// <param name="image">The image.</param>
public static void RecognizeFormWithOcrFields(
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager,
Vintasoft.Imaging.VintasoftImage image)
{
// check whether OCR engine manager of the OCR field templates is initialized
// (this initialization can be moved to the start of your application)
if (Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager == null)
{
// get or create OCR engine
Vintasoft.Imaging.Ocr.OcrEngine ocrEngine = GetOcrEngine();
// create and set OCR engine manager of the OCR field templates
Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager =
new Vintasoft.Imaging.Ocr.OcrEngineManager(ocrEngine);
}
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(image);
// 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;
// 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)
{
if (recognizedField is Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField)
{
Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField ocrField =
(Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField)recognizedField;
// write field info
System.Console.WriteLine(string.Format(
" OCR field: name: {0}; value: {1}; confidence: {2:F1}%",
ocrField.Name,
ocrField.Value,
ocrField.Confidence * 100));
Vintasoft.Imaging.Ocr.Results.OcrPage ocrResult = ocrField.OcrResult;
// get all words
Vintasoft.Imaging.Ocr.Results.OcrObject[] words = ocrResult.GetWords(75, 75);
// write words info
for (int i = 0; i < words.Length; i++)
{
Vintasoft.Imaging.Ocr.Results.OcrObject word = words[i];
System.Console.WriteLine(string.Format(
" OCR word: {0}; confidence: {1:F1}%",
word.ToString(),
word.Confidence));
}
}
}
}
}
}
/// <summary>
/// Gets the OCR engine used for OCR field recognition.
/// </summary>
/// <remarks>
/// To create a Tesseract OCR engine,
/// add a reference to Vintasoft.Imaging.Ocr.Tesseract.dll
/// into your project.
/// </remarks>
private static Vintasoft.Imaging.Ocr.OcrEngine GetOcrEngine()
{
// full path to the Tesseract5.Vintasoft.xXX.dll files
// NOTE: specify here the actual path to the Tesseract OCR dll files
string tesseractDllDirectory = @"C:\Program Files\VintaSoft\VintaSoft Imaging .NET\Bin\TesseractOCR\";
// create Tesseract OCR engine (Vintasoft.Imaging.Ocr.Tesseract.dll is required)
return new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr(tesseractDllDirectory);
}
''' <summary>
''' Recognizes the form with OCR fields.
''' </summary>
''' <param name="formRecognitionManager">The form recognition manager.</param>
''' <param name="image">The image.</param>
Public Shared Sub RecognizeFormWithOcrFields(formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager, image As Vintasoft.Imaging.VintasoftImage)
' check whether OCR engine manager of the OCR field templates is initialized
' (this initialization can be moved to the start of your application)
If Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager Is Nothing Then
' get or create OCR engine
Dim ocrEngine As Vintasoft.Imaging.Ocr.OcrEngine = GetOcrEngine()
' create and set OCR engine manager of the OCR field templates
Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager = New Vintasoft.Imaging.Ocr.OcrEngineManager(ocrEngine)
End If
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(image)
' 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
' 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
If TypeOf recognizedField Is Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField Then
Dim ocrField As Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField = DirectCast(recognizedField, Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrField)
' write field info
System.Console.WriteLine(String.Format(" OCR field: name: {0}; value: {1}; confidence: {2:F1}%", ocrField.Name, ocrField.Value, ocrField.Confidence * 100))
Dim ocrResult As Vintasoft.Imaging.Ocr.Results.OcrPage = ocrField.OcrResult
' get all words
Dim words As Vintasoft.Imaging.Ocr.Results.OcrObject() = ocrResult.GetWords(75, 75)
' write words info
For i As Integer = 0 To words.Length - 1
Dim word As Vintasoft.Imaging.Ocr.Results.OcrObject = words(i)
System.Console.WriteLine(String.Format(" OCR word: {0}; confidence: {1:F1}%", word.ToString(), word.Confidence))
Next
End If
Next
End If
End If
End Sub
''' <summary>
''' Gets the OCR engine used for OCR field recognition.
''' </summary>
''' <remarks>
''' To create a Tesseract OCR engine,
''' add a reference to Vintasoft.Imaging.Ocr.Tesseract.dll
''' into your project.
''' </remarks>
Private Shared Function GetOcrEngine() As Vintasoft.Imaging.Ocr.OcrEngine
' full path to the Tesseract5.Vintasoft.xXX.dll files
' NOTE: specify here the actual path to the Tesseract OCR dll files
Dim tesseractDllDirectory As String = "C:\Program Files\VintaSoft\VintaSoft Imaging .NET\Bin\TesseractOCR\"
' create Tesseract OCR engine (Vintasoft.Imaging.Ocr.Tesseract.dll is required)
Return New Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr(tesseractDllDirectory)
End Function
Identify and recognize a document that contains forms with barcodes
For identification and recognition of document that contains forms with barcodes is necessary to do the following:
- Identify templates of all forms
- Perform the forms recognition
Here is an example that demonstrates how to identify and recognize a form, that contains barcodes.
/// <summary>
/// Recognizes the form with barcode fields.
/// </summary>
/// <param name="formRecognitionManager">The form recognition manager.</param>
/// <param name="image">The image.</param>
/// <remarks>
/// The BarcodeField and BarcodeFieldTemplate classes are available
/// in the VintaSoft Forms Processing Demo source codes.
/// </remarks>
public static void RecognizeFormWithBarcodeFields(
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager,
Vintasoft.Imaging.VintasoftImage image)
{
// 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
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(image);
// 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;
// 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)
{
// an example of BarcodeField class implementation can be found
// in the VintaSoft Forms Processing Demo source codes
if (recognizedField is BarcodeField)
{
BarcodeField barcodeField = (BarcodeField)recognizedField;
// write field info
System.Console.WriteLine(string.Format(
" Barcode field: name: {0}; type: {1}; value: {2}; confidence: {3:F1}%",
barcodeField.Name,
barcodeField.BarcodeInfo.BarcodeType,
barcodeField.Value,
barcodeField.Confidence * 100));
}
}
}
}
}
''' <summary>
''' Recognizes the form with barcode fields.
''' </summary>
''' <param name="formRecognitionManager">The form recognition manager.</param>
''' <param name="image">The image.</param>
''' <remarks>
''' The BarcodeField and BarcodeFieldTemplate classes are available
''' in the VintaSoft Forms Processing Demo source codes.
''' </remarks>
Public Shared Sub RecognizeFormWithBarcodeFields(formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager, image As Vintasoft.Imaging.VintasoftImage)
' 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
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(image)
' 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
' 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
' an example of BarcodeField class implementation can be found
' in the VintaSoft Forms Processing Demo source codes
If TypeOf recognizedField Is BarcodeField Then
Dim barcodeField As BarcodeField = DirectCast(recognizedField, BarcodeField)
' write field info
System.Console.WriteLine(String.Format(" Barcode field: name: {0}; type: {1}; value: {2}; confidence: {3:F1}%", barcodeField.Name, barcodeField.BarcodeInfo.BarcodeType, barcodeField.Value, barcodeField.Confidence * 100))
End If
Next
End If
End If
End Sub
Process template form image and filled form image before form identification and recognition
Sometimes template form image and filled form images are not "ideal" and SDK allows to specify a list of image processing commands,
which must be applied to the template form image and filled form image before form identification.
Here is an example that demonstrates how to process form images before form identification (remove noise and apply Dilate command), identify and recognize the form:
/// <summary>
/// Recognizes single image synchronously.
/// </summary>
/// <param name="templateManager">The template manager.</param>
/// <param name="image">The image.</param>
public static void RecognizeSingleImageSync(
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.5f;
// 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
// subscribe to ImageRecognitionError event to output recognition errors
recognitionManager.ImageRecognitionError += recognitionManager_ImageRecognitionError;
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult = recognitionManager.Recognize(image);
// unsubscribe from ImageRecognitionError event
recognitionManager.ImageRecognitionError -= recognitionManager_ImageRecognitionError;
// 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 FormRecognitionManager.
/// </summary>
static void recognitionManager_ImageRecognitionError(object sender,
Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs e)
{
System.Console.WriteLine(e.Exception.Message);
}
''' <summary>
''' Recognizes single image synchronously.
''' </summary>
''' <param name="templateManager">The template manager.</param>
''' <param name="image">The image.</param>
Public Shared Sub RecognizeSingleImageSync(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.5F
' 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
' subscribe to ImageRecognitionError event to output recognition errors
AddHandler recognitionManager.ImageRecognitionError, AddressOf recognitionManager_ImageRecognitionError
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = recognitionManager.Recognize(image)
' unsubscribe from ImageRecognitionError event
RemoveHandler recognitionManager.ImageRecognitionError, AddressOf recognitionManager_ImageRecognitionError
' 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 FormRecognitionManager.
''' </summary>
Private Shared Sub recognitionManager_ImageRecognitionError(sender As Object, e As Vintasoft.Imaging.FormsProcessing.FormRecognitionErrorEventArgs)
System.Console.WriteLine(e.Exception.Message)
End Sub
Display the recognized filled form on image in WinForms image viewer
FormFieldViewerTool class is intended for displaying the recognized filled form on image in WinForms image viewer.
Here is an example that demonstrates how to recognize a form and display the results in WinForms image viewer.
/// <summary>
/// Recognizes the current image of specified viewer
/// and shows recognition results in viewer.
/// </summary>
/// <param name="viewer">The viewer.</param>
/// <param name="formRecognitionManager">The form recognition manager.</param>
public static bool RecognizeAndShowInViewer(
Vintasoft.Imaging.UI.ImageViewer viewer,
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager)
{
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(viewer.Image);
// get the result of image comparison
Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult imageCompareResult =
recognitionResult.TemplateMatchingResult.ImageCompareResult;
// if image is recognized
if (imageCompareResult.IsReliable)
{
// create a visual tool
Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.VisualTools.FormFieldViewerTool formFieldViewerTool =
new Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.VisualTools.FormFieldViewerTool();
// create a view for the recognized page
Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.FormFieldView pageView =
Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.FormFieldViewFactory.CreateView(recognitionResult.RecognizedPage);
// add the view to the collection of the visual tool
formFieldViewerTool.FieldViewCollection.Add(pageView);
// set the visual tool as current tool of the viewer
viewer.VisualTool = formFieldViewerTool;
return true;
}
else
{
return false;
}
}
''' <summary>
''' Recognizes the current image of specified viewer
''' and shows recognition results in viewer.
''' </summary>
''' <param name="viewer">The viewer.</param>
''' <param name="formRecognitionManager">The form recognition manager.</param>
Public Shared Function RecognizeAndShowInViewer(viewer As Vintasoft.Imaging.UI.ImageViewer, formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager) As Boolean
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(viewer.Image)
' get the result of image comparison
Dim imageCompareResult As Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult = recognitionResult.TemplateMatchingResult.ImageCompareResult
' if image is recognized
If imageCompareResult.IsReliable Then
' create a visual tool
Dim formFieldViewerTool As New Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.VisualTools.FormFieldViewerTool()
' create a view for the recognized page
Dim pageView As Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.FormFieldView = Vintasoft.Imaging.FormsProcessing.FormRecognition.UI.FormFieldViewFactory.CreateView(recognitionResult.RecognizedPage)
' add the view to the collection of the visual tool
formFieldViewerTool.FieldViewCollection.Add(pageView)
' set the visual tool as current tool of the viewer
viewer.VisualTool = formFieldViewerTool
Return True
Else
Return False
End If
End Function
Display the recognized filled form on image in WPF image viewer
WpfFormFieldViewerTool class is intended for displaying the recognized filled form on image in WPF image viewer.
Here is an example that demonstrates how to recognize a form and display the results in WPF image viewer.
/// <summary>
/// Recognizes the current image of specified viewer
/// and shows recognition results in viewer.
/// </summary>
/// <param name="viewer">The viewer.</param>
/// <param name="formRecognitionManager">The form recognition manager.</param>
public static bool RecognizeAndShowInViewer(
Vintasoft.Imaging.Wpf.UI.WpfImageViewer viewer,
Vintasoft.Imaging.FormsProcessing.FormRecognitionManager formRecognitionManager)
{
// recognize the image
Vintasoft.Imaging.FormsProcessing.FormRecognitionResult recognitionResult =
formRecognitionManager.Recognize(viewer.Image);
// get the result of image comparison
Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult imageCompareResult =
recognitionResult.TemplateMatchingResult.ImageCompareResult;
// if image is recognized
if (imageCompareResult.IsReliable)
{
// create a visual tool
Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.VisualTools.WpfFormFieldViewerTool formFieldViewerTool =
new Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.VisualTools.WpfFormFieldViewerTool();
// create a view for the recognized page
Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.WpfFormFieldView pageView =
Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.WpfFormFieldViewFactory.CreateView(recognitionResult.RecognizedPage);
// add the view to the collection of the visual tool
formFieldViewerTool.FieldViewCollection.Add(pageView);
// set transform matrix and resolution of template image
formFieldViewerTool.SetTransform(
imageCompareResult.TransformMatrix,
imageCompareResult.SourceImprint.ImageSize.Resolution);
// set the visual tool as current tool of the viewer
viewer.VisualTool = formFieldViewerTool;
return true;
}
else
{
return false;
}
}
''' <summary>
''' Recognizes the current image of specified viewer
''' and shows recognition results in viewer.
''' </summary>
''' <param name="viewer">The viewer.</param>
''' <param name="formRecognitionManager">The form recognition manager.</param>
Public Shared Function RecognizeAndShowInViewer(viewer As Vintasoft.Imaging.Wpf.UI.WpfImageViewer, formRecognitionManager As Vintasoft.Imaging.FormsProcessing.FormRecognitionManager) As Boolean
' recognize the image
Dim recognitionResult As Vintasoft.Imaging.FormsProcessing.FormRecognitionResult = formRecognitionManager.Recognize(viewer.Image)
' get the result of image comparison
Dim imageCompareResult As Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintCompareResult = recognitionResult.TemplateMatchingResult.ImageCompareResult
' if image is recognized
If imageCompareResult.IsReliable Then
' create a visual tool
Dim formFieldViewerTool As New Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.VisualTools.WpfFormFieldViewerTool()
' create a view for the recognized page
Dim pageView As Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.WpfFormFieldView = Vintasoft.Imaging.FormsProcessing.FormRecognition.Wpf.UI.WpfFormFieldViewFactory.CreateView(recognitionResult.RecognizedPage)
' add the view to the collection of the visual tool
formFieldViewerTool.FieldViewCollection.Add(pageView)
' set transform matrix and resolution of template image
formFieldViewerTool.SetTransform(imageCompareResult.TransformMatrix, imageCompareResult.SourceImprint.ImageSize.Resolution)
' set the visual tool as current tool of the viewer
viewer.VisualTool = formFieldViewerTool
Return True
Else
Return False
End If
End Function