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