VintaSoft Imaging .NET SDK 14.0: Документация для .NET разработчика
В этом разделе
    ICR: Распознавание рукописных цифр
    В этом разделе
    Интеллектуальное распознавание символов (ICR) используется для извлечения рукописного текста из изображений. Это более сложный тип технологии OCR, которая распознает различные стили рукописного ввода и шрифты для интеллектуальной интерпретации данных в формах и физических документах.

    Начиная с версии 12.5 VintaSoft OCR .NET Plug-in предоставляет сборку Vintasoft.Imaging.Ocr.ML.dll для распознавания рукописных цифр (символов "0123456789)(+-/") с использованием нейронной сети.
    Сборка Vintasoft.Imaging.Ocr.ML.dll выполняет машинное обучение с использованием библиотеки "Microsoft.ML" и имеет ссылки на версию 4.0.0 Nuget-пакетов "Microsoft.ML" и "Microsoft.ML.LightGbm".

    Сборка Vintasoft.Imaging.Ocr.ML.dll содержит следующие классы для распознавания рукописных цифр:
    Также сборку Vintasoft.Imaging.Ocr.ML.dll можно использовать совместно с VintaSoft Forms Processing .NET Plug-in для распознавания рукописных цифр в полях заполненной формы.
    Класс OcrFieldTemplate позволяет определить шаблон OCR-поля, содержащего одну строку рукописных цифр.


    Вот C#/VB.NET код, который демонстрирует, как распознать одну строку рукописных цифр на изображении с помощью класса HandwrittenDigitsLineRecognizer:
    /// <summary>
    /// Recognizes the line of handwritten digits using HandwrittenDigitsLineRecognizer class.
    /// </summary>
    /// <param name="image">The image.</param>
    /// <returns>Recognized line.</returns>
    public static string RecognizeHandwrittenLineUsingHandwrittenDigitsLineRecognizer(Vintasoft.Imaging.VintasoftImage image)
    {
        // create handwritten digits recognizer
        Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsLineRecognizer lineRecognizer = 
            new Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsLineRecognizer(image, "1234567890");
    
        // recognize line of handwritten digits
        Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsRecognitionResult result = lineRecognizer.RecognizeLine();
    
        // print result
        for (int i = 0; i < result.Symbols.Length; i++)
            System.Console.WriteLine(string.Format("{0}: {1}%", result.Symbols[i], System.Math.Round(result.Confidences[i] * 100)));
    
        return result.ToString();
    }
    
    ''' <summary>
    ''' Recognizes the line of handwritten digits using HandwrittenDigitsLineRecognizer class.
    ''' </summary>
    ''' <param name="image">The image.</param>
    ''' <returns>Recognized line.</returns>
    Public Shared Function RecognizeHandwrittenLineUsingHandwrittenDigitsLineRecognizer(image As Vintasoft.Imaging.VintasoftImage) As String
        ' create handwritten digits recognizer
        Dim lineRecognizer As New Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsLineRecognizer(image, "1234567890")
    
        ' recognize line of handwritten digits
        Dim result As Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsRecognitionResult = lineRecognizer.RecognizeLine()
    
        ' print result
        For i As Integer = 0 To result.Symbols.Length - 1
            System.Console.WriteLine(String.Format("{0}: {1}%", result.Symbols(i), System.Math.Round(result.Confidences(i) * 100)))
        Next
    
        Return result.ToString()
    End Function
    


    Вот C#/VB.NET код, который демонстрирует, как распознать одну строку рукописных цифр в области изображения с помощью класса HandwrittenDigitsOcrEngine:
    /// <summary>
    /// Recognizes the line of handwritten digits.
    /// </summary>
    /// <param name="image">The image.</param>
    /// <returns>Recognized line.</returns>
    public static string RecognizeHandwrittenLineUsingHandwrittenDigitsOcrEngine(Vintasoft.Imaging.VintasoftImage image)
    {
        // create OCR engine for recognition of handwritten digits
        Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrEngine ocrEngine =
            new Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrEngine();
        
        // create OCR settings for recognition of handwritten digits
        Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrSettings ocrSettings = 
            new Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrSettings();
        ocrSettings.CharWhiteList = "1234567890";
        
        // init OCR engine
        ocrEngine.Init(ocrSettings);
    
        // recognize line of handwritten digits
        Vintasoft.Imaging.Ocr.Results.OcrPage page = ocrEngine.Recognize(image);
        Vintasoft.Imaging.Ocr.Results.OcrTextRegion textRegion = (Vintasoft.Imaging.Ocr.Results.OcrTextRegion)page.Regions[0];
    
        // print result
        System.Console.WriteLine(string.Format("{0}: {1}%", textRegion.Text, textRegion.Confidence));
        
        return textRegion.Text;
    }
    
    ''' <summary>
    ''' Recognizes the line of handwritten digits.
    ''' </summary>
    ''' <param name="image">The image.</param>
    ''' <returns>Recognized line.</returns>
    Public Shared Function RecognizeHandwrittenLineUsingHandwrittenDigitsOcrEngine(image As Vintasoft.Imaging.VintasoftImage) As String
        ' create OCR engine for recognition of handwritten digits
        Dim ocrEngine As New Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrEngine()
    
        ' create OCR settings for recognition of handwritten digits
        Dim ocrSettings As New Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrSettings()
        ocrSettings.CharWhiteList = "1234567890"
    
        ' init OCR engine
        ocrEngine.Init(ocrSettings)
    
        ' recognize line of handwritten digits
        Dim page As Vintasoft.Imaging.Ocr.Results.OcrPage = ocrEngine.Recognize(image)
        Dim textRegion As Vintasoft.Imaging.Ocr.Results.OcrTextRegion = DirectCast(page.Regions(0), Vintasoft.Imaging.Ocr.Results.OcrTextRegion)
    
        ' print result
        System.Console.WriteLine(String.Format("{0}: {1}%", textRegion.Text, textRegion.Confidence))
    
        Return textRegion.Text
    End Function
    


    Вот C#/VB.NET код, который демонстрирует, как распознать поле с строкой рукописных цифр в заполненной форме:
    /// <summary>
    /// Recognizes the form with OCR fields, which contain handwritten digits.
    /// </summary>
    /// <param name="formRecognitionManager">The form recognition manager.</param>
    /// <param name="image">The image.</param>
    public static void RecognizeHandwrittenDigitsInFilledForm(
        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)
        {
            // create OCR engine for recognition of handwritten digits
            Vintasoft.Imaging.Ocr.OcrEngine handwrittenDigitsOcrEngine = new Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrEngine();
    
            // create and set OCR engine manager for the OCR field templates
            Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager =
                new Vintasoft.Imaging.Ocr.OcrEngineManager(handwrittenDigitsOcrEngine);
        }
    
        // recognize filled form in an 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>
    ''' Recognizes the form with OCR fields, which contain handwritten digits.
    ''' </summary>
    ''' <param name="formRecognitionManager">The form recognition manager.</param>
    ''' <param name="image">The image.</param>
    Public Shared Sub RecognizeHandwrittenDigitsInFilledForm(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
            ' create OCR engine for recognition of handwritten digits
            Dim handwrittenDigitsOcrEngine As Vintasoft.Imaging.Ocr.OcrEngine = New Vintasoft.Imaging.Ocr.ML.HandwrittenDigits.HandwrittenDigitsOcrEngine()
    
            ' create and set OCR engine manager for the OCR field templates
            Vintasoft.Imaging.FormsProcessing.FormRecognition.Ocr.OcrFieldTemplate.OcrEngineManager = New Vintasoft.Imaging.Ocr.OcrEngineManager(handwrittenDigitsOcrEngine)
        End If
    
        ' recognize filled form in an 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