Идентифицикация и распознавание документа, содержащего несколько заполненных форм
В этом разделе
Если вы хотите идентифицировать и распознавать документ, содержащий несколько заполненных форм, вам необходимо выполнить следующие шаги:
- Идентифицировать шаблон для каждой заполненной формы
- Распознавать значения полей в заполненных формах
Вот C#/VB.NET код, демонстрирующий, как асинхронно идентифицировать и распознавать несколько заполненных форм.
/// <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