Links
Comment on page

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 by passing one of the input sources.
private TextRecognizer CreateTextRecognizer()
{
IInputSource inputSource = CreateImageInputSource(TEXTURE);
scanner = new TextRecognizer(inputSource);
return scanner;
}
private IInputSource CreateImageInputSource(Texture2D texture)
{
return new ImageInputSource(texture);
}
private IInputSource CreateLiveCameraInputSource()
{
IInputSource inputSource = new LiveCameraInputSource()
{
EnableFlash = false,
IsFrontFacing = false
};
return inputSource;
}

Prepare

For preparing, you need to pass on TextRecognizerOptions and a callback to know when prepare is complete.
private TextRecognizerOptions CreateTextRecognizerOptions()
{
TextRecognizerOptions.Builder builder = new TextRecognizerOptions.Builder();
builder.SetInputLanguage(TextRecognizerInputLanguage.Latin); //TextRecognizerInputLanguage.Japanese or TextRecognizerInputLanguage.Korean
return builder.Build();
}
private void Prepare()
{
TextRecognizerOptions options = CreateTextRecognizerOptions();
Debug.Log("Starting prepare...");
scanner.Prepare(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