How do I run onnxruntime on platformIO?

How do I run onnxruntime on platformIO?

Insufficient information to be able to help you.
Please provide more details.
Minimum requirement is that you show the content of your platformio.ini.

Tell us more about onxruntime. Is this a self-written function or from a library? If library: Please tell us which library.

Firstly, thank you for your reply.


The model.onnx file is a model converted from Python to C++.
But when compiling, it gives an error, which was possible before in STM32CubeIDE.

The lib folder is for Arduino libraries.

Your converted files are not a valid Arduino library.
Moving the implementation files (.cpp) into the src folder and header files (.h) into the include folder should fix your problem.

1 Like

I checked the installation files in the officially provided microsoft.ml.onnxruntime package and did not find any files in .cpp format.
And according to the available tutorials, only library files seem to be among the .lib and .dll format files.
Is there any way to replace the .cpp format files?

The same configuration method is compiling fine in STM32CubeIDE.

If there are no .cpp files then it seems it’s header only (just .h files)
Move them into the include folder as mentioned before.

.dll files won’t work on a microcontroller and seems to be some windows artifacts during the convert process?

PlatformIO is not STM32CubeIDE
Things might be handled differently.

In PlatformIO, the lib folder is reserved for valid Arduino libraries, which your converted code is not.

1 Like


Is there still a way to fix this?

Sure, there are 2 ways:

1: move all files direct into the include folder (without any subfolder!)

file structure:

+- include
|  +- cpu_provider_factory.h
|  +- onxruntime_c_api.h
|  +- ...
|
+- lib
+- src
   +- main.cpp

include statement:

#include "onxruntime_c_api.h"

2: keep the existing file structure and use the correct include statement:

file structure: (that’s what you have now)

+- include
|  +- onxruntime_c_api
|     +- cpu_provider_factory.h
|     +- onxruntime_c_api.h
|     +- ...
|
+- lib
+- src
   +- main.cpp

include statement:

#include "onxruntime_c_api/onxruntime_c_api.h"

Thank you for your reply.
The first method reports the error worse.
The second method reports the error as shown in the picture.

Please use the correct folder and file-names!

I have a typo in there (missing an “n”) because i typed from your screenshots

#include "onnxruntime_c_api/onnxruntime_c_api.h"
Hoping there isn’t another typo in it.

After typing the name of the folder correctly, the errors are even more serious :sweat_smile:

Maybe there are other files trying to include some of the header files. So you would have to edit them all (not just the main.cpp) and fix the include statement.

To avoid this, try method 1 and move all files directly into include folder !

If you have other error messages, please post them by using code blocks… no screenshots please!

src\main.cpp: In function 'void setup()':
src\main.cpp:27:5: error: 'OrtCreateEnv' was not declared in this scope
   27 |     OrtCreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env);
      |     ^~~~~~~~~~~~
src\main.cpp:31:5: error: 'OrtCreateSessionOptions' was not declared in this scope; did you mean 'OrtSessionOptions'?
   31 |     OrtCreateSessionOptions(&session_options);
      |     ^~~~~~~~~~~~~~~~~~~~~~~
      |     OrtSessionOptions
src\main.cpp:36:25: error: 'OrtCreateSession' was not declared in this scope; did you mean 'OrtSession'?
   36 |     OrtStatus* status = OrtCreateSession(env, model_path, session_options, &session);
      |                         ^~~~~~~~~~~~~~~~
      |                         OrtSession
src\main.cpp:38:27: error: 'OrtGetErrorMessage' was not declared in this scope
   38 |         const char* msg = OrtGetErrorMessage(status);
      |                           ^~~~~~~~~~~~~~~~~~
src\main.cpp:40:9: error: 'OrtReleaseStatus' was not declared in this scope
   40 |         OrtReleaseStatus(status);
      |         ^~~~~~~~~~~~~~~~
src\main.cpp:41:16: error: return-statement with a value, in function returning 'void' [-fpermissive]
   41 |         return 1;
      |                ^
src\main.cpp:52:16: error: return-statement with a value, in function returning 'void' [-fpermissive]
   52 |         return 1;
      |                ^
src\main.cpp:62:5: error: 'OrtCreateCpuMemoryInfo' was not declared in this scope
   62 |     OrtCreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory_info);
      |     ^~~~~~~~~~~~~~~~~~~~~~
src\main.cpp:67:99: error: 'ONNX_TENSOR_ELEMENT_FLOAT' was not declared in this scope; did you mean 'ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT'?
   67 |     OrtCreateTensorWithDataAsOrtValue(memory_info, input_data, input_tensor_size, input_shape, 3, ONNX_TENSOR_ELEMENT_FLOAT, &input_tensor);
      |                                                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                                                                   ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT
src\main.cpp:67:5: error: 'OrtCreateTensorWithDataAsOrtValue' was not declared in this scope
   67 |     OrtCreateTensorWithDataAsOrtValue(memory_info, input_data, input_tensor_size, input_shape, 3, ONNX_TENSOR_ELEMENT_FLOAT, &input_tensor);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\main.cpp:72:5: error: 'OrtRun' was not declared in this scope
   72 |     OrtRun(session, NULL, input_names, &input_tensor, 1, NULL, 0, &output_tensor);
      |     ^~~~~~
src\main.cpp:77:5: error: 'OrtReleaseValue' was not declared in this scope
   77 |     OrtReleaseValue(output_tensor);
      |     ^~~~~~~~~~~~~~~
src\main.cpp:80:5: error: 'OrtReleaseSession' was not declared in this scope
   80 |     OrtReleaseSession(session);
      |     ^~~~~~~~~~~~~~~~~
src\main.cpp:81:5: error: 'OrtReleaseSessionOptions' was not declared in this scope; did you mean 'OrtSessionOptions'?
   81 |     OrtReleaseSessionOptions(session_options);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
      |     OrtSessionOptions
src\main.cpp:82:5: error: 'OrtReleaseEnv' was not declared in this scope
   82 |     OrtReleaseEnv(env);
      |     ^~~~~~~~~~~~~
src\main.cpp:83:5: error: 'OrtReleaseMemoryInfo' was not declared in this scope; did you mean 'OrtMemoryInfo'?
   83 |     OrtReleaseMemoryInfo(memory_info);
      |     ^~~~~~~~~~~~~~~~~~~~
      |     OrtMemoryInfo
src\main.cpp:85:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
   85 |     return 0;
      |            ^
Archiving .pio\build\uno_r4_wifi\libFrameworkArduino.a
*** [.pio\build\uno_r4_wifi\src\main.cpp.o] Error 1
Indexing .pio\build\uno_r4_wifi\libFrameworkArduino.a
==================================================================================================== [FAILED] Took 10.00 seconds ====================================================================================================

 *  终端进程“C:\Users\xiang\.platformio\penv\Scripts\platformio.exe 'run'”已终止,退出代码: 1。 
 *  终端将被任务重用,按任意键关闭。 

Please share the project.
A GitHub repo would be great but a zip file hosted somewhere is okay, too.

Just to be clear: The project compiles in STM Ide without any issues?

Hello, are you working with platformio esp32, can we talk about it better. how can i have a converstion with you?

I’m not using platformio esp32.
I’m using the STM32CubeIDE and platformIO with Ariduino R4.