Error Could not load file or assembly Tesseract on Ubuntu using .Net Core 3.1

Viewed 669

I am using Tesseract version 4.1.1 on dotnetcore 3.1 project which works perfectly on windows but when I publish it on ubuntu it throws the following error on this line

new TesseractEngine(Tessdatapath, LanguageCode, EngineMode.TesseractAndLstm);

Could not load file or assembly 'Tesseract, Version=4.1.1.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

I copied the x64 & x86 dlls with the publish files and made sure they are on the same level with tessdata

I tried to install tesseract on ubuntu and copied the .so files inside the x64 & x86 folders but still no luck

3 Answers

So here is how I fixed it

It turned out that system didnt display the correct error message because it couldnt use the library System.Drawing.Common which is not supported by Linux.

Fixed that by using libgdiplux the Linux implementation of System.Drawing.Common

sudo apt-get -f install libgdiplus

Then it displayed the correct message which is

Failed to find library "libleptonica-1.80.0.so" for platform x64.

To fix that I had to compile this leptonica version from here http://www.leptonica.org/download.html

this helped me to compile it http://www.leptonica.org/source/README.html

So now that I have "libleptonica-1.80.0.so" installed I created link inside my x64 folder to leptonica files following this comment Tesseract Issue #503

As I had to do a lot of trial-and-error even with the great answer from @Lemo I want to post my resulting Docker file as it might save someone a few hours :)

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

# Install libs for Tesseract
RUN apt-get update
RUN apt-get install -y git cmake build-essential
RUN mkdir leptonica
RUN git clone https://github.com/DanBloomberg/leptonica.git /leptonica

WORKDIR /leptonica
RUN mkdir build
WORKDIR /leptonica/build
RUN cmake ..

RUN apt-get install -y libleptonica-dev libtesseract-dev

# Link libs for Tesseract
WORKDIR /app/x64
RUN ln -s /usr/lib/x86_64-linux-gnu/liblept.so.5 libleptonica-1.80.0.so
RUN ln -s /usr/lib/x86_64-linux-gnu/libtesseract.so.4.0.1 libtesseract41.so

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ProjectName/ProjectName.csproj", "ProjectName/"]
RUN dotnet restore "ProjectName/ProjectName.csproj"
COPY . .

WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]

Nothing that is described here hasn't worked for me.
I've kept receiving the "libleptonica-1.80.0.so error for platform x64" whatever I did.

The problem is inside the Tesseract library. Some guy made a patch that applied the fix using reflection. His comment is here.

That works perfectly for me. I will leave here the full Dockerfile code along with steps on how I made it works.

  1. Put inside your project folder these files libleptonica-1.80.0.so, libtesseract41.so by the same file path as this guy made it in his project: <your_solution>/<project>/runtimes/linux-x64/native/

  2. Add this class TesseractLinuxLoaderFix.cs to your project

  3. Call Path method before using Tesseract. TesseractLinuxLoaderFix.Patch();

  4. Describe your Dockerfile as this:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

# Install libs for Tesseract. Despite this build a few libraries still will be not available
RUN apt-get update
RUN apt-get install -y git cmake build-essential
RUN mkdir leptonica
RUN git clone --depth 1 --branch 1.80.0 https://github.com/DanBloomberg/leptonica.git /leptonica 
RUN cd leptonica

WORKDIR /leptonica
RUN mkdir build
WORKDIR /leptonica/build
RUN cmake ..

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "SolutionName/ProjectName.csproj"
COPY . .
WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

# copy Tesseract missing dependencies
COPY --from=build /src/ProjectName/runtimes ./runtimes

ENTRYPOINT ["dotnet", "ProjectName.dll"]

Then all works for me. Finally. Good luck everybody with this mess

Related