Lyo.Images.Ocr.Tesseract
1.0.2
dotnet add package Lyo.Images.Ocr.Tesseract --version 1.0.2
NuGet\Install-Package Lyo.Images.Ocr.Tesseract -Version 1.0.2
<PackageReference Include="Lyo.Images.Ocr.Tesseract" Version="1.0.2" />
<PackageVersion Include="Lyo.Images.Ocr.Tesseract" Version="1.0.2" />
<PackageReference Include="Lyo.Images.Ocr.Tesseract" />
paket add Lyo.Images.Ocr.Tesseract --version 1.0.2
#r "nuget: Lyo.Images.Ocr.Tesseract, 1.0.2"
#:package Lyo.Images.Ocr.Tesseract@1.0.2
#addin nuget:?package=Lyo.Images.Ocr.Tesseract&version=1.0.2
#tool nuget:?package=Lyo.Images.Ocr.Tesseract&version=1.0.2
Lyo.Images.Ocr.Tesseract
Tesseract implementation of IOcrEngine from Lyo.Images.Ocr. Calls are serialized with an internal lock because native Tesseract instances are not safely concurrent.
Examples
Register with DI
services.AddTesseractOcrEngine(
shared => shared.DefaultLanguages = "eng",
tess => tess.TessdataDirectory = "/path/to/tessdata");
Register with DI (2)
services.AddTesseractOcrEngineFromConfiguration(configuration);
Register with DI (3)
{
"OcrEngine": {
"EnableMetrics": false,
"DefaultLanguages": "eng",
"DefaultPageSegmentationMode": "Auto",
"Tesseract": {
"TessdataDirectory": "/usr/share/tesseract-ocr/5/tessdata"
}
}
}
1. Native runtime
Windows
- Restore/build your app so the Tesseract NuGet copies
x64\tesseract50.dll(and Leptonica) into your output folder. - Run your process from that output folder (or ensure those DLLs are on the loader search path).
Linux (Debian/Ubuntu-style)
libtesseract5 is only the native library. Integration tests and IOcrEngine still need eng.traineddata on disk — that comes from a language-data package, not
from libtesseract5 alone.
- Update package lists:
sudo apt-get update - Install native libraries:
- Tesseract:
sudo apt-get install -y libtesseract5 - Leptonica (required by the NuGet interop; package name varies):
sudo apt-get install -y libleptonica6If that package does not exist on your release:sudo apt-get install -y libleptonica-devlibleptonica-devonly installslibleptonica.so/libleptonica.so.*— it never createslibleptonica-1.82.0.so. You still need the symlink in the subsection below.
- Tesseract:
- Install English traineddata (pick one):
- Minimal:
sudo apt-get install -y tesseract-ocr-eng - Or full meta (CLI + common languages):
sudo apt-get install -y tesseract-ocr
- Minimal:
- Confirm the
.soexists:ldconfig -p | grep tesseract(expectlibtesseract.so.5) - Confirm
eng.traineddataexists (path varies by distro/version):
You should see something like /usr/share/tesseract-ocr/5/tessdata/eng.traineddata.
find /usr/share/tesseract-ocr -name eng.traineddata 2>/dev/null
Linux: libleptonica-1.82.0.so / libtesseract50.so (NuGet vs distro filenames)
The charlesw/Tesseract NuGet asks InteropDotNet for those exact basenames. Distros ship liblept.so.*, libleptonica.so, libtesseract.so.5, etc. apt
does not install libleptonica-1.82.0.so.
Critical (why /usr/local/lib often fails): on Linux the loader looks next to your built app, under x64/ inside the configuration output folder — for example *
bin/Debug/net10.0/x64/* — not at global ldconfig paths. See upstream discussion: charlesw/tesseract#687.
Directory layout: the real test project lives next to the Tesseract project:
Lyo.Net/Data/Images/Lyo.Images.Ocr.Tesseract.Tests/— this is whatdotnet testbuilds (output:.../bin/Debug/net10.0/).
Do not confuse it with a nested Lyo.Images.Ocr.Tesseract/Lyo.Images.Ocr.Tesseract.Tests/ folder; that path is not the SDK layout and is often created accidentally (
sometimes root-owned). Remove it with sudo rm -rf if it appears.
libdl.so: InteropDotNet loads the library name libdl. Glibc only provides libdl.so.2 (often under /lib/x86_64-linux-gnu/), so **libdl.so is missing
** unless you add a symlink. The setup script drops libdl.so → libdl.so.2 next to $(OutputPath) (same folder as your *.dll), which is one of the paths .NET
probes.
Automatic (Linux): PrepareTesseractLinuxNativeLibs=true triggers scripts/setup_linux_tesseract_nuget_libs.py after each Build (Linux only). The MSBuild *
PrepareTesseractLinuxNativeLibs* target lives in Lyo.Net/Data/Images/Directory.Build.targets.
Projects under
Lyo.Net/Data/Images/pick that file up automatically.Lyo.Nethost apps (Lyo.TestConsole,Lyo.Comic.Api,Lyo.Gateway, etc.) must import it explicitly:<Import Project="..\..\..\Data\Images\Directory.Build.targets"/>Adjust the relative path from your
.csprojtoData/Images/(three levels works fromApps/Comic/Lyo.Comic.Api/).
Lyo.Images.Ocr.Tesseract.Tests sets the property (the library does not — compile/pack must not require distro Tesseract); hosts outside Data/Images/ need Import plus the property (otherwise the
symlink step never runs).
Manual / CI-only: run the script yourself after dotnet build / dotnet publish when you do not use that property:
Pass the TFM output directory (**/bin/Debug/net10.0, not **/x64):
python3 Lyo.Net/Data/Images/Lyo.Images.Ocr.Tesseract/scripts/setup_linux_tesseract_nuget_libs.py \
"$PWD/Lyo.Net/Data/Images/Lyo.Images.Ocr.Tesseract.Tests/bin/Debug/net10.0"
Re-run after a clean build if output was deleted.
Optional: --also-system uses sudo to mirror Leptonica/Tesseract under /usr/local/lib, libdl.so next to the system libdl.so.2, and runs ldconfig.
Usually unnecessary if app-local symlinks exist.
Manual equivalent (same idea as the script):
OUT=/path/to/your/app/bin/Debug/net10.0
ARCH=x86_64-linux-gnu
mkdir -p "$OUT/x64"
ln -sf "$(readlink -f /usr/lib/$ARCH/libleptonica.so 2>/dev/null || readlink -f /usr/lib/$ARCH/liblept.so.5)" \
"$OUT/x64/libleptonica-1.82.0.so"
ln -sf "$(readlink -f /usr/lib/$ARCH/libtesseract.so.5)" "$OUT/x64/libtesseract50.so"
ln -sf "$(readlink -f /lib/$ARCH/libdl.so.2)" "$OUT/libdl.so"
macOS (Homebrew)
- Install:
brew install tesseract - Confirm:
tesseract --version - Confirm traineddata:
ls "$(brew --prefix)/share/tessdata/eng.traineddata"
2. Language data (traineddata) — manual / custom layout
- If you use distro packages (Linux/macOS above),
TessdataDirectoryshould be thetessdatafolder that containseng.traineddata(tests auto-detect common Linux paths and any/usr/share/tesseract-ocr/*/tessdatadirectory). - Otherwise ship or download
eng.traineddatainto a folder you control (other languages = more*.traineddatafiles). - Manual download (English fast model): tessdata_fast — eng.traineddata
- Point
TesseractOcrEngineOptions.TessdataDirectoryat the directory that containseng.traineddata, not the parent oftessdata.
3. Wire configuration
- Set
OcrEngine:DefaultLanguagesto match your files (e.g.eng, oreng+jpnif botheng.traineddataandjpn.traineddataare present). - Set
OcrEngine:Tesseract:TessdataDirectoryto the absolute path from step 2. - Start the app and exercise
IOcrEngine.ReadAsynconce; if tessdata is wrong you typically get engine creation errors referencing missing*.traineddata.
Automated tests
- Install library + language data as above (
tesseract-ocr-engortesseract-ocron Debian/Ubuntu). - Enable OCR integration either way:
- Recommended: set
OcrTesseractTests:RunIntegrationtotrueinappsettings.Development.json, or editappsettings.jsonlocally (don’t committrueif CI must stay off). - Alternate:
LYO_RUN_TESSERACT_INTEGRATION=1(still supported; overrides/appsettings merge viaAddEnvironmentVariables). - Tessdata path resolution (first match wins):
OcrTesseractTests:TessdataDirectory, thenOcrEngine:Tesseract:TessdataDirectory, then outputtessdata/, * *LYO_TESSDATA_DIRECTORY**, then distro paths under/usr/share/tesseract-ocr. - Run:
dotnet test Lyo.Net/Data/Images/Lyo.Images.Ocr.Tesseract.Tests/ - Native integration tests (
ReadAsync_*with real Tesseract) useAssert.SkipUnless/Assert.SkipWhen: they show as skipped (not passed) when integration is off,eng.traineddatacannot be resolved, or native libraries are missing (OCR_NATIVE_LIBRARY_NOT_FOUND). The always-on tests (ReadAsync_missing_tessdata_*, configuration tests) still run. - If assertions mention missing tessdata, confirm
find /usr/share/tesseract-ocr -name eng.traineddata—ldconfigshowinglibtesseract.so.5alone is not enough.
Dependency injection
Or from configuration: With appsettings.json: If OcrEngineOptions was already registered (e.g. via AddOcrEngineOptionsFromConfiguration), use AddTesseractOcrEngine without the configureShared delegate so shared options are not registered twice.
Coordinate space
Word boxes follow Lyo.Images.Ocr conventions (Y-up pixel coordinates). Use OcrCoordinateTransforms when integrating with PDF points (Lyo.Pdf.Ocr).
Dependencies
Generated from ProjectReference / PackageReference (same model as docs/Lyo.ProjectGraph.html).
Lyo.Exceptions— (direct, lyo)Lyo.Images.Ocr— (direct, lyo)Lyo.Metrics— (direct, lyo)Microsoft.Extensions.Configuration.Binder10.0.5— (direct, microsoft)Microsoft.Extensions.DependencyInjection.Abstractions10.0.5— (direct, microsoft)Microsoft.Extensions.Logging.Abstractions10.0.5— (direct, microsoft)Tesseract5.2.0— (direct, third-party)Lyo.Common— (transitive, lyo)Lyo.Result— (transitive, lyo)Microsoft.Extensions.Options.ConfigurationExtensions10.0.5— (transitive, microsoft)System.Memory4.6.3— (transitive, microsoft, netstandard2.0)System.Text.Json10.0.5— (transitive, microsoft, netstandard2.0)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Lyo.Exceptions (>= 1.0.2)
- Lyo.Images.Ocr (>= 1.0.2)
- Lyo.Metrics (>= 1.0.2)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.5)
- Tesseract (>= 5.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.