Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on creating TesseractEngine #673

Open
Vernizze opened this issue Jun 26, 2024 · 4 comments
Open

Error on creating TesseractEngine #673

Vernizze opened this issue Jun 26, 2024 · 4 comments

Comments

@Vernizze
Copy link

Vernizze commented Jun 26, 2024

Hi!

I'm starting with Tesseract and have this code, that runs very well on Windows:

using (var engine = new TesseractEngine("tessdata", "por"))
{            
    var image = Pix.LoadFromFile(filePath);

    var page = engine.Process(image);

    text = page.GetText();
}

But I need run this one on Linux, most specifically on Mint distribution, and I use in this form:

using (var engine = new TesseractEngine("./tessdata", "por"))
{            
    var image = Pix.LoadFromFile(filePath);

    var page = engine.Process(image);

    text = page.GetText();
}

And I receive this error in the 'new TesseractEngine' line:

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
at System.ArgumentNullException.Throw(String paramName)
at System.IO.Path.Combine(String path1, String path2)
at InteropDotNet.LibraryLoader.InternalLoadLibrary(String baseDirectory, String platformName, String fileName)
at InteropDotNet.LibraryLoader.CheckExecutingAssemblyDomain(String fileName, String platformName)
at InteropDotNet.LibraryLoader.LoadLibrary(String fileName, String platformName)
at InteropRuntimeImplementer.LeptonicaApiSignaturesInstance.LeptonicaApiSignaturesImplementation..ctor(LibraryLoader loader)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr)
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at InteropDotNet.InteropRuntimeImplementer.CreateInstanceT
at Tesseract.Interop.LeptonicaApi.Initialize()
at Tesseract.Interop.TessApi.Initialize()
at Tesseract.Interop.TessApi.get_Native()
at Tesseract.TesseractEngine..ctor(String datapath, String language, EngineMode engineMode, IEnumerable1 configFiles, IDictionary2 initialOptions, Boolean setOnlyNonDebugVariables)
at Tesseract.TesseractEngine..ctor(String datapath, String language)
at TesteConversaoPdfParaImagem.Program.ReadImage(String prefix, String filePath, String resultFileName, Boolean isSingleblock) in C:\Users\carlo\source\repos\TesteConversaoPdfParaImagem\TesteConversaoPdfParaImagem\Program.cs:line 127
at TesteConversaoPdfParaImagem.Program.Main(String[] args) in C:\Users\carlo\source\repos\TesteConversaoPdfParaImagem\TesteConversaoPdfParaImagem\Program.cs:line 53

Do you help me with this, please?

Thanks a lot, guys! :)

@Yanik39
Copy link

Yanik39 commented Jun 27, 2024

System.ArgumentNullException: Value cannot be null. (Parameter 'path1')
at System.ArgumentNullException.Throw(String paramName)
at System.IO.Path.Combine(String path1, String path2)

I am not expert but, its definitely about your path :D.
I would Console.WriteLine(filePath); to see what is result of your Path.Combine. Probably you have null there.

@mitchuhl
Copy link

mitchuhl commented Jun 28, 2024

Yesterday I discovered the same error on the project that I just started. When I debug from Visual Studio there is no issue. When I publish and run it somewhere else, I get the same error message.

image
I discovered that I get the error when I enable the checkbox "Produce single file". When it's enabled I get the error. When it's disabled, I don't get the error.

In both situations the tessdata folder is on the same location (on the same level as my console executable). Also in both situations I confirmed my code was pointing to the right location by adding a console message:

string tesseractDataPath = Path.Combine(AppContext.BaseDirectory, "tessdata");
Console.WriteLine($"Tesseract data folder: {tesseractDataPath}");
using var ocrEngine = new TesseractEngine(tesseractDataPath, "nld+eng", EngineMode.Default);

@Vernizze
Copy link
Author

Hi guys!

I found the error. It's happen on the class 'InteropDotNet.LibraryLoader', at the line 86:

var baseDirectory = Path.GetDirectoryName(executingAssembly.Location);

The code executingAssembly.Location return null and it's the cause of the crash. I created a handler class called 'EnvironmentUtils.cs' in 'Tesseract.Internal' with this code:

` using System;
using System.IO;
using System.Reflection;

namespace Tesseract.Internal
{
internal static class EnvironmentUtils
{
public static string AppPath(Assembly assembly)
{
string appPath = Path.GetDirectoryName(Path.GetDirectoryName(assembly?.Location));

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        return AppPath();
    }

    public static string AppPath() 
    {
        string appPath;

        appPath = Directory.GetCurrentDirectory();

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        appPath = Environment.CurrentDirectory;

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        appPath = Path.Combine(AppContext.BaseDirectory, "tessdata");

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        appPath = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location));

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        appPath = AppDomain.CurrentDomain.BaseDirectory;

        if (!string.IsNullOrWhiteSpace(appPath))
            return appPath;

        throw new ArgumentNullException("Application path not found");
    }
}

} `

And the problem has solved. But after this, the error change to this:

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Failed to find library "libleptonica-1.82.0.so" for platform x64. at InteropDotNet.LibraryLoader.LoadLibrary(String fileName, String platformName) in D:\git\tesseract\src\Tesseract\Internal\InteropDotNet\LibraryLoader.cs:line 57 at InteropRuntimeImplementer.LeptonicaApiSignaturesInstance.LeptonicaApiSignaturesImplementation..ctor(LibraryLoader loader) at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span1 copyOfArgs, BindingFlags invokeAttr) at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) at InteropDotNet.InteropRuntimeImplementer.CreateInstance[T]() in D:\git\tesseract\src\Tesseract\Internal\InteropDotNet\InteropRuntimeImplementer.cs:line 45 at Tesseract.Interop.LeptonicaApi.Initialize() in D:\git\tesseract\src\Tesseract\Interop\LeptonicaApi.cs:line 563 at Tesseract.Interop.TessApi.Initialize() in D:\git\tesseract\src\Tesseract\Interop\BaseApi.cs:line 583 at Tesseract.Interop.TessApi.get_Native() in D:\git\tesseract\src\Tesseract\Interop\BaseApi.cs:line 372 at Tesseract.TesseractEngine..ctor(String datapath, String language, EngineMode engineMode, IEnumerable1 configFiles, IDictionary2 initialOptions, Boolean setOnlyNonDebugVariables) in D:\git\tesseract\src\Tesseract\TesseractEngine.cs:line 181 at Tesseract.TesseractEngine..ctor(String datapath, String language) in D:\git\tesseract\src\Tesseract\TesseractEngine.cs:line 37 at TesteConversaoPdfParaImagem.Program.ReadImage(String prefix, String filePath, String resultFileName, Boolean isSingleblock) in C:\Users\carlo\source\repos\TesteConversaoPdfParaImagem\TesteConversaoPdfParaImagem\Program.cs:line 147 at TesteConversaoPdfParaImagem.Program.Extract(String pdfName, String readedFilePath) in C:\Users\carlo\source\repos\TesteConversaoPdfParaImagem\TesteConversaoPdfParaImagem\Program.cs:line 68 at TesteConversaoPdfParaImagem.Program.Main(String[] args) in C:\Users\carlo\source\repos\TesteConversaoPdfParaImagem\TesteConversaoPdfParaImagem\Program.cs:line 14

In the next week I expect return to see this new problem.

Thank's for your help :)

@charlesw
Copy link
Owner

charlesw commented Jun 29, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants