Skip to main content

Tesseract

Tesseract adds optical character recognition (OCR) to Wisej.NET applications. It's built on Tesseract.js (version 4.1.1), a JavaScript port of the Tesseract OCR engine that supports more than 100 languages. It works in two ways:

  • Scanning an image: pass an image or an image URL to ScanImageAsync and get back the recognized text, the individual words and a confidence score;
  • Scanning a live camera feed: attach the component to a Camera control. It samples the video at a fixed interval, draws boxes around the words it finds, and fires TextRecognized on the server.

Recognition runs in the user's browser, in Web Workers. The server does no OCR work, and the images don't have to be uploaded for it.

Installation​

Add the Wisej-4-Tesseract package to your Wisej.NET project:

dotnet add package Wisej-4-Tesseract

The package targets .NET Framework 4.8 and .NET 8 or later. After a rebuild, Tesseract appears in the Visual Studio toolbox as a component. Dropping it on a page puts it in the component tray. To scan a camera feed, also add the Camera extension (Wisej-4-Camera).

The browser downloads the OCR engine and language data

Tesseract.js fetches its WebAssembly core from the jsDelivr CDN and the trained language data (<language>.traineddata, several megabytes per language) from tessdata.projectnaptha.com the first time it starts. The user's browser needs access to those hosts, so the extension won't work on an isolated network without further configuration. The browser caches the data afterwards.

Scanning an image​

ScanImageAsync accepts a System.Drawing.Image, or an image URL: absolute, relative, a Wisej.NET resource URL or a base64 data URL. It returns a ScanResult:

using Wisej.Ext.Tesseract;

private async void buttonScan_Click(object sender, EventArgs e)
{
// Scan the image shown in a PictureBox...
ScanResult result = await this.tesseract1.ScanImageAsync(this.pictureBox1.Image);

// ...or an image by URL.
// result = await this.tesseract1.ScanImageAsync("Images/invoice.png");

this.textBoxText.Text = result.Text;
this.labelInfo.Text = $"{result.Words.Length} words, confidence {result.Confidence}%";
}
ScanResult propertyWhat it holds
TextAll the recognized text.
WordsThe recognized words, as a string[].
ConfidenceHow confident the engine is in the result, from 0 to 100. Higher is more accurate.

A System.Drawing.Image is sent to the browser as a PNG data URL, so large photos travel over the connection once. Scale them down first when you can.

Scanning a camera feed​

Set Camera to a Camera control on the same page, and handle TextRecognized:

public Page1()
{
InitializeComponent();

this.tesseract1.Camera = this.camera1;
this.tesseract1.Language = "eng";
this.tesseract1.Interval = 1000; // scan once a second
this.tesseract1.MinimumConfidence = 70; // ignore weak results
this.tesseract1.Keywords = new[] { "INVOICE", "TOTAL" };
}

private void tesseract1_TextRecognized(object sender, TextRecognizedEventArgs e)
{
this.textBoxText.Text = e.Text;
}

private void tesseract1_WorkerError(object sender, string error)
{
AlertBox.Show("OCR error: " + error, MessageBoxIcon.Error);
}

TextRecognizedEventArgs has the same Text, Words and Confidence as a ScanResult.

For each frame, the component skips results whose confidence isn't above MinimumConfidence. When Keywords is set, it fires TextRecognized only if the text contains one of the keywords, once for each keyword found. With no keywords, it fires for every accepted frame.

Camera feeds need HTTPS

The Camera extension uses the browser's media APIs, which need a secure context (HTTPS, or localhost) and the user's permission to use the camera.

Properties​

PropertyDefaultWhat it does
Language"eng"The Tesseract language code, such as eng, deu or ita. Combine languages with +, as in "eng+deu". See the list of language codes.
WhitelistnullRestricts recognition to these characters, for example "0123456789" for numbers only. This helps a lot when the content is known.
WorkerCount1The number of Tesseract.js workers for the camera feed. More workers take turns on successive frames, but they use more CPU, memory and battery. Each worker loads its own copy of the language data.
CameranullThe Camera control to scan.
EnabledtruePauses or resumes scanning of the camera feed.
Interval1500Milliseconds between two scans of the camera feed.
MinimumConfidence60Camera results at or below this confidence are ignored.
KeywordsnullOnly report camera text containing one of these strings (case-sensitive).
ShowWordstrueDraws a rectangle around each recognized word over the camera preview.
note

Language, Whitelist and WorkerCount configure the workers when they start. Set them before scanning, for example in the designer or in the constructor.

Events​

EventWhen
TextRecognizedText is recognized in the camera feed (see Scanning a camera feed).
WorkerErrorA Tesseract.js worker fails, for example because the language data couldn't be downloaded. The argument is the error message.
Try it

The Wisej.NET demo browser has a live Tesseract demo.

API reference​