I use AsposeOCR to recognize text in pdf file.
On localhost, all is ok.
On server web (in production), I have an error with Microsoft.ML.OnnxRuntime:
The type initializer for ‘Microsoft.ML.OnnxRuntime.NativeMethods’ threw an exception.
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.ML.OnnxRuntime.NativeMethods…cctor()
at Microsoft.ML.OnnxRuntime.SessionOptions…ctor() at .() at ..() at System.Lazy1.CreateValue() at System.Lazy
1.LazyInitValue() at …ctor(RecognitionSettings , , ) at Aspose.OCR.AsposeOcr.Recognize(OcrInput images, RecognitionSettings settings)
Configuration :
Aspose OCR : version 25.7
Aspose PDF : version 25.5
Microsoft.ML.OnnxRuntime : version 1.22.2.0
The code use is below :
Aspose.OCR.AsposeOcr recognitionEngine;
recognitionEngine = new Aspose.OCR.AsposeOcr();
try
{
OcrInput input;
input = new OcrInput(Aspose.OCR.InputType.PDF);
input.Add(filename, i, 1);
Aspose.OCR.RecognitionSettings settings;
settings = new Aspose.OCR.RecognitionSettings();
settings.Language = Aspose.OCR.Language.Latin;
// Recognize PDF
try
{
List<Aspose.OCR.RecognitionResult> results = recognitionEngine.Recognize(input, settings);
foreach (Aspose.OCR.RecognitionResult result in results)
{
//Console.WriteLine(result.RecognitionText);
if (mylist.ContainsKey(i))
{
mylist[i] += result.RecognitionText;
}
else
{
mylist.Add(i, result.RecognitionText);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
mylist.Add(-1, ex.Message);
return mylist;
}
input.Clear();
input.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
mylist.Add(-1, ex.Message);
return mylist;
}
My question : how resolve this problem on web server ?
@pdidier_hyscad_fr
It sounds like you’re encountering an issue with the Microsoft.ML.OnnxRuntime when running your Aspose.OCR code on a web server, while it works fine on your local machine. This type of error often indicates a problem with the environment or configuration on the server.
Here are some steps you can take to troubleshoot and resolve the issue:
-
Check Dependencies: Ensure that all required dependencies for Microsoft.ML.OnnxRuntime are correctly installed on the server. Missing native libraries can cause initialization errors.
-
Environment Configuration: Verify that the server environment matches your local setup. Check the .NET version, specific configurations, and ensure that the server has the necessary permissions to access required resources.
-
Update Packages: Make sure you are using compatible versions of Aspose.OCR, Aspose.PDF, and Microsoft.ML.OnnxRuntime. Updating to the latest versions can resolve compatibility issues.
-
Thread Configuration: If you are using multi-threading, adjust the thread settings for ONNX runtime. Set the number of threads for intra- and inter-operation in the OnnxRuntimeSessionOptions
class. For example:
Aspose.OCR.OnnxRuntimeSessionOptions.IntraOpNumThreads = 4; // Adjust as needed
Aspose.OCR.OnnxRuntimeSessionOptions.InterOpNumThreads = 2; // Adjust as needed
-
Debugging: Enable logging or debugging options available in Aspose.OCR to get more detailed error messages. This can help pinpoint the exact cause of the issue.
-
Test with Simple Input: Run a simpler OCR task with a basic image or PDF to see if the issue persists. This can help determine if the problem is with the specific input file or the overall setup.
If you follow these steps and still encounter issues, please provide any additional error messages or logs you receive. I will be happy to assist you further.
Let me know if you need any more help!
I find the solution with an other ticket " OCR.AsposeOcr Issue"
Compilation the project visual studio with the platform target x64 (before it was AnyCPU).
Could you confirm that this choise is perfect ?
Thank you for your reply.
@pdidier_hyscad_fr
Yes, the solution adopted by you to resolve the issue is correct and recommended. This is a common problem with x86-x64 (sometimes the wrong version of onnxruntime is installed into the project).
ok thank you for your reply. You can close the ticket
1 Like