Text Recognizer

The ML Kit Text Recognition API can recognize text in any Latin-based character set. It can also be used to automate data-entry tasks such as processing credit cards, receipts, and business cards.The ML Kit Text Recognition API can recognize text in any Latin-based character set. It can also be used to automate data-entry tasks such as processing credit cards, receipts, and business cards.

  • Recognize text across Latin-based languages Supports recognizing text using Latin script

  • Analyze structure of text Supports detection of words/elements, lines and paragraphs

  • Identify language of text Identifies the language of the recognized text

  • Real-time recognition Can recognize text in real-time on a wide range of devices

Import VoxelBusters.EasyMLKit and VoxelBusters.CoreLibrary namespaces

using VoxelBusters.EasyMLKit;
using VoxelBusters.CoreLibrary;

Create Instance

Create an instance of the TextRecognizer instance.

private TextRecognizer CreateTextRecognizer()
{
    TextRecognizer scanner = new TextRecognizer();   
    return scanner;
}

Prepare

For preparing, you need to pass an instance to input source, instance of TextRecognizerOptions and a callback to know when prepare is complete.

private IInputSource CreateImageInputSource(Texture2D texture)
{
    return new ImageInputSource(texture);
}

private IInputSource CreateLiveCameraInputSource()
{
    IInputSource inputSource = new LiveCameraInputSource()
    {
        EnableFlash = false,
        IsFrontFacing = false
    };

    return inputSource;
}

private TextRecognizerOptions CreateTextRecognizerOptions()
{
    TextRecognizerOptions.Builder builder = new TextRecognizerOptions.Builder();
    builder.SetInputLanguage(TextRecognizerInputLanguage.Latin); //TextRecognizerInputLanguage.Japanese or TextRecognizerInputLanguage.Korean
    return builder.Build();
}

private void Prepare()
{
    IInputSource inputSource = CreateImageInputSource(TEXTURE);//TEXTURE -> "Readable" Texture 2D instance
    TextRecognizerOptions options = CreateTextRecognizerOptions();
    Debug.Log("Starting prepare...");
    scanner.Prepare(inputSource, options, OnPrepareComplete);
}

private void OnPrepareComplete(TextRecognizer scanner, Error error)
{
        Debug.Log("Prepare complete..." + error);
        if (error == null)
        {
            Debug.Log("Prepare completed successfully!");
        }
        else
        {
            Debug.Log("Failed preparing Text recognizer : " + error.Description);
        }
}

Process

Once prepare is complete, you can start processing which gives the result in a callback.

private void OnPrepareComplete(TextRecognizer scanner, Error error)
{
        Debug.Log("Prepare complete..." + error);
        if (error == null)
        {
            Debug.Log("Prepare completed successfully!");
            scanner.Process(OnProcessUpdate);
        }
        else
        {
            Debug.Log("Failed preparing Text Recognizer : " + error.Description);
        }
}

private void OnProcessUpdate(TextRecognizer scanner, TextRecognizerResult result)
{
    if (!result.HasError())
    {
        Debug.Log(string.Format("Received {0}", result.TextGroup));

        if(result.TextGroup != null)
        {
            scanner.Close(null);
        }
    }
    else
    {
        Debug.Log("Text Recognizer failed processing : " + result.Error.Description, false);
    }
});

Close

Close the scanner once you are done processing.

scanner.Close(null);//Or pass a callback to know once its complete

Last updated