Aspose ocr Recognize not working when called from CLI wrapper

I am writing a code to call recognize function of Aspose ocr .net code from C++ application using CLI implementation.
When i run as console application recognize function works fine, But when i call the same function from c++ code through CLI wrapper it is throwing me the below errors.
2/27/2025 5:35:45 PM: Aspose_Export()::Error during OCR recognition: System.Memory
2/27/2025 5:35:45 PM: Aspose_Export()::Stack Trace: at System.ReadOnlySpan1..ctor(T[] array) at .(Single[] , Byte[] ) at .(Byte[,][] ) at .(Byte[,][] ) at .(Byte[,][] ,  , Single& ) at .(Byte[,][] , String , RecognitionSettings ,  ) at .(ImageData , RecognitionSettings ,  , Action4 )
at .(ImageData , RecognitionSettings ,  , Action4 ) at .(OcrInput , RecognitionSettings ,  , Action4 , CancellationToken )
at .(OcrInput , RecognitionSettings ,  , Action`4 )
at Aspose.OCR.AsposeOcr.Recognize(OcrInput images, RecognitionSettings settings)
at AsposeOCRLib.AsposeOCRApi.Aspose_Export(String exportFileName, Int32 format, IntPtr exportParams) in C:\Current\AsposeOCRLib\AsposeOCRApi.cs:line 389
2/27/2025 5:35:45 PM: Aspose_Export()::Error during export: Could not load file or assembly ‘System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Below are my code snippets.
.cs file:
public bool Aspose_Export(string exportFileName, int format, IntPtr exportParams)
{
try
{
// Initialize the OCR engine
//AsposeOcr ocrEngine = new AsposeOcr();
string czImageFilePath = “C:\Aspose_DOC\JPG\GIF_SinglePageText_OCR_1.gif”;
MessageBox.Show("Path: " + czImageFilePath);

       // Check if the file exists
       if (!File.Exists(czImageFilePath))
       {
           throw new FileNotFoundException("The specified image file does not exist.", czImageFilePath);
       }
       MessageBox.Show("File is exist :  " + czImageFilePath);
       // Create an instance of OcrInput with the correct parameters
       OcrInput input = new OcrInput(InputType.SingleImage);
       input.Add("C:\\Aspose_DOC\\JPG\\JPG_SinglePage_3.jpg");
       
       MessageBox.Show("input added");
       // Log the input details
       //Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
       //Log($"Aspose_Export()::Processing file: {czImageFilePath}");

       if(ocrEngine == null)
       {
           MessageBox.Show("OCR Engine is null");
           ocrEngine = new AsposeOcr();
       }
       else
       {
           MessageBox.Show("OCR Engine is not null");
       }
       // Set recognition language (optional)
       RecognitionSettings recognitionSettings = new RecognitionSettings();
       //// Perform OCR recognition
       List<RecognitionResult> results = null;
       try
       {
           results = ocrEngine.Recognize(input, recognitionSettings);

           //results = ocrEngine.Recognize(input, new RecognitionSettings());
           MessageBox.Show("OCR recognition completed");

       }
       catch (Exception ex)
       {
           
           MessageBox.Show("Error during OCR recognition");
           Log($"Aspose_Export()::Error during OCR recognition: {ex.Source}");
           Log($"Aspose_Export()::Stack Trace: {ex.StackTrace}");
           throw;
       }
       MessageBox.Show("Recognize the input");
       if (results == null || results.Count == 0)
       {
           throw new InvalidOperationException("OCR recognition failed to produce results.");
       }

       // Determine the export format and save the result accordingly
       switch (format)
       {
           case 0:
               MessageBox.Show("Switch RTF");
               SaveAsRtf(exportFileName, results[0].RecognitionText);
               MessageBox.Show("Exit RTF switch");
               break;
           case 1:
               //SaveAsPdf(exportFileName, results[0].RecognitionText);
               break;
           default:
               throw new NotSupportedException($"Format {format} is not supported.");
       }
       MessageBox.Show("Returning TRUe");
       return true;
   }
   catch (FileNotFoundException ex)
   {
       Log($"Aspose_Export()::File not found: {ex.Message}");
       return false;
   }
   catch (UnauthorizedAccessException ex)
   {
       Log($"Aspose_Export()::Access denied: {ex.Message}");
       return false;
   }
   catch (Exception ex)
   {
       Log($"Aspose_Export()::Error during export: {ex.Message}");
       return false;
   }

}
none of the parameters sent from CLI is not used, as you can see in the above code we hardcoded the input image file path also the export file path. I have not added the CLI wrapper function, please let me know if required.
I tried to downgrade the System.Runtime.CompilerServices.Unsafe from 6.0.0 to 4.5.3 version, still error is exists.

After downgrading the System.Runtime.CompilerServices.Unsafe version from 6.0.0 to 4.5.3, I am encountering an error during OCR recognition with Aspose, specifically related to Microsoft.ML.OnnxRuntime. The error occurs in the Aspose_Export function, resulting in a runtime exception with a non-zero status code while running the Mul node, and the status message indicates a ‘bad allocation’. Below is the stack trace for further reference. Any insights or solutions to resolve this issue would be greatly appreciated.

2/27/2025 7:32:38 PM: Aspose_Export()::Error during OCR recognition: Microsoft.ML.OnnxRuntime
2/27/2025 7:32:38 PM: Aspose_Export()::Stack Trace: at Microsoft.ML.OnnxRuntime.NativeApiStatus.VerifySuccess(IntPtr nativeStatus)
at Microsoft.ML.OnnxRuntime.InferenceSession.RunImpl(RunOptions options, IntPtr[] inputNames, IntPtr[] inputValues, IntPtr[] outputNames)
at Microsoft.ML.OnnxRuntime.InferenceSession.Run(IReadOnlyCollection1 inputs, IReadOnlyCollection1 outputNames, RunOptions options)
at Microsoft.ML.OnnxRuntime.InferenceSession.Run(IReadOnlyCollection1 inputs, IReadOnlyCollection1 outputNames)
at Microsoft.ML.OnnxRuntime.InferenceSession.Run(IReadOnlyCollection1 inputs) at .(Byte[,][] , Boolean , Boolean , Language ) at .(RecognitionSettings , e ) at .(RecognitionSettings , e ) at .(Byte[,][] , String , RecognitionSettings , e ) at .(ImageData , RecognitionSettings , e , Action4 )
at .(ImageData , RecognitionSettings , e , Action4 ) at .(OcrInput , RecognitionSettings , e , Action4 )
at Aspose.OCR.AsposeOcr.Recognize(OcrInput images, RecognitionSettings settings)
at AsposeOCRLib.AsposeOCRApi.Aspose_Export(String exportFileName, Int32 format, IntPtr exportParams) in C:\Current\AsposeOCRLib\AsposeOCRApi.cs:line 398
2/27/2025 7:32:38 PM: Aspose_Export()::Error during export: [ErrorCode:RuntimeException] Non-zero status code returned while running Mul node. Name:‘Conv_156_quant_output_scale_mul’ Status Message: bad allocation

@Rama.M

The issue looks related to assembly loading in C++. Please ensure that if you’re using .NET Framework in your CLI wrapper, you are using correct .NET Framework version i.e. >=4.6.1. Also, please make sure that all assemblies and dependencies are being copied to the build output e.g. Microsoft.ML.OnnxRuntime. In case issue keeps persisting, please share some steps to reproduce it in our environment so that we can address it accordingly.

Hello asad.ali,

Thank you for your support and guidance; with the information you provided, I was able to resolve my issue related to OCR processing by making the following modifications.
Verify that all dependencies are set to be copied in the project properties to ensure proper functionality.

  1. Right-click on each dependency in the Solution Explorer under “References”.
  2. Select “Properties”.
  3. Ensure that “Copy Local” is set to True.
    Below are the updated lines of my code:

// Perform OCR recognition
List results = null;
RecognitionSettings recognitionSettings = new RecognitionSettings()
{
DetectAreasMode = DetectAreasMode.NONE
};
results = ocrEngine.Recognize(input, recognitionSettings);

@Rama.M

Its nice to know that your issue has been resolved. Please keep using our API and feel free to create a new topic in case you face any kind of issues.