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
ScanImageAsyncand 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
TextRecognizedon 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).
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:
- C#
- VB.NET
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}%";
}
Imports Wisej.Ext.Tesseract
Private Async Sub ButtonScan_Click(sender As Object, e As EventArgs) Handles ButtonScan.Click
' Scan the image shown in a PictureBox...
Dim result As ScanResult = Await Me.Tesseract1.ScanImageAsync(Me.PictureBox1.Image)
' ...or an image by URL.
' result = Await Me.Tesseract1.ScanImageAsync("Images/invoice.png")
Me.TextBoxText.Text = result.Text
Me.LabelInfo.Text = $"{result.Words.Length} words, confidence {result.Confidence}%"
End Sub
ScanResult property | What it holds |
|---|---|
Text | All the recognized text. |
Words | The recognized words, as a string[]. |
Confidence | How 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:
- C#
- VB.NET
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);
}
Public Sub New()
InitializeComponent()
Me.Tesseract1.Camera = Me.Camera1
Me.Tesseract1.Language = "eng"
Me.Tesseract1.Interval = 1000 ' scan once a second
Me.Tesseract1.MinimumConfidence = 70 ' ignore weak results
Me.Tesseract1.Keywords = {"INVOICE", "TOTAL"}
End Sub
Private Sub Tesseract1_TextRecognized(sender As Object, e As TextRecognizedEventArgs) Handles Tesseract1.TextRecognized
Me.TextBoxText.Text = e.Text
End Sub
Private Sub Tesseract1_WorkerError(sender As Object, [error] As String) Handles Tesseract1.WorkerError
AlertBox.Show("OCR error: " & [error], MessageBoxIcon.Error)
End Sub
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.
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
| Property | Default | What 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. |
Whitelist | null | Restricts recognition to these characters, for example "0123456789" for numbers only. This helps a lot when the content is known. |
WorkerCount | 1 | The 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. |
Camera | null | The Camera control to scan. |
Enabled | true | Pauses or resumes scanning of the camera feed. |
Interval | 1500 | Milliseconds between two scans of the camera feed. |
MinimumConfidence | 60 | Camera results at or below this confidence are ignored. |
Keywords | null | Only report camera text containing one of these strings (case-sensitive). |
ShowWords | true | Draws a rectangle around each recognized word over the camera preview. |
Language, Whitelist and WorkerCount configure the workers when they start. Set them before scanning, for example in the designer or in the constructor.
Events
| Event | When |
|---|---|
TextRecognized | Text is recognized in the camera feed (see Scanning a camera feed). |
WorkerError | A Tesseract.js worker fails, for example because the language data couldn't be downloaded. The argument is the error message. |
The Wisej.NET demo browser has a live Tesseract demo.
API reference
TesseractScanResultandTextRecognizedEventArgs- The whole
Wisej.Ext.Tesseractnamespace