I tried the following sample snippets but got error “The type initializer for ' ’ threw an exception. - The type initializer for ‘Microsoft.ML.OnnxRuntime.NativeMethods’ threw an exception.” at this line var res = api.RecognizePdf(dataDir + “BRZ0000001.pdf”, settings);
Can you tell me what I did wrong?
// C# Code
var api = new OCR.AsposeOcr();
var settings = new OCR.DocumentRecognitionSettings();
settings.StartPage = 0;
//settings.PagesNumber = 2;
//settings.LinesFiltration = true;
settings.DetectAreas = true;
settings.DetectAreasMode = OCR.DetectAreasMode.COMBINE;
settings.ThreadsCount = 1;
var res = api.RecognizePdf(dataDir + "BRZ0000001.pdf", settings);
OCR.AsposeOcr.SaveMultipageDocument(dataDir + "test2.pdf", OCR.SaveFormat.Pdf, res);
The Aspose.OCR for .NET API has a dependency on OnnxRutime library. Please try to install the API using NuGet Package Manager and all the dependencies would get installed automatically. In case you still face any issues, please let us know.
I did try to install the Aspose.OCR for .NET API using NuGetPackage Manager but still got the same error. I do see the Microsoft.ML.OnnXRuntime in my project’s Reference.
It seems strange because we tested using the same setting i.e. .NET Framework 4.8 and Visual Studio 2017 and we did not notice any issue. Please check the packages folder in the screenshot that shows the version numbers of the dependencies as well. Please try to create a fresh console application and install the API from NuGet and share that application with us if you still notice any issues in it.dep.png (5.9 KB)
We were able to reproduce the issue in our environment and have logged it as OCRNET-559 in our issue tracking system. We will further look into its details and let you know as soon as it is rectified. Please be patient and spare us some time.
We are afraid that the earlier logged ticket could not get resolved due to other pending issues in the queue. We will surely resolve and fix it on first come first serve basis and let you know as soon as it we have further updates in this regard.
Please try to create a project with Target x86, then install Aspose.OCR.
The problem with Onnxruntime is that it doesn’t work with AnyCPU. And when you install Aspose.OCR with the AnyCPU project, you have the wrong version of the ONNX runtime. Please try this solution. Hope it helps.
We are trying to use Aspose.OCR (v22.5.0)and we are also using .NET Framework 4.8 and Visual Studio 2022.
We are getting the same problem with Onnxruntime.
“Could not load file or assembly ‘Microsoft.ML.OnnxRuntime, Version=1.21.0.0, Culture=neutral, PublicKeyToken=f27f157f0a5b7bb6’ or one of its dependencies. The system cannot find the file specified.”:“Microsoft.ML.OnnxRuntime, Version=1.21.0.0, Culture=neu…”
Can you please confirm if this (OCRNET-559) has already been resolved? And which version?
Problem of the Onnxruntime is that it doesn’t work with AnyCPU. And when you install Aspose.OCR with the AnyCPU project, you have the wrong version of the ONNX runtime. This was the finding against ticket (OCRNET-559). Additionally, 22.5 is an older version and we recommend using latest version always. Please try with the latest version and let us know if you still face this issue.
Same issue occurs when using the latest version (25.3.0). But was able to make it run by updating ONNX runtime version from 1.20.1 (version installed by nuget) to 1.21.0 and adding a PlatformTarget of “AnyCPU” to my csproj file.
Can you confirm if that is the expected version of ONNX runtime for the latest Aspose.OCR?
Above is the version that is supported in the latest Aspose.OCR for .NET. However, it should also work with 1.21.0. We have checked in our environment and could not notice any issues with 1.20.1 version.
It can be an issue with the wrong ONNX DLL connected to the project (x86 for example). We have checked the 25.3.0 with Framework 4.6.1 AnyCPU (Prefer 32bit) and all works fine.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.