1. IOS
自己用源代码编译非常麻烦,在mocos很难编译成功。
使用pod方式安装。
pod 'onnxruntime-c', '~> 1.14.0'#The ORT format model version [5] is not supported this build 1.11.0" 至少1.13.0
需要
#define ORT_API_MANUAL_INIT
#include <onnxruntime_cxx_api.h>
#undef ORT_API_MANUAL_INIT
这样后需要主动调用一次
Ort::InitApi();
不能直接
Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, "ONNX_DETECTION");
否则直接会crash或者memory问题。
2. 安卓方面
需要从onnx源代码的tools/android_custom_build开始编译
使用build_custom_android_package.py开始编译
需要在同目录的dockerfile修改第7行为
FROM --platform=linux/amd64 ubuntu:20.04
其实增加了--platform=linux/amd64。
然后把成功打包的aar文件命名为zip文件,解密有把.so文件放到你的JNI工程里,
在JNI工程里建立一个子目录增加一个Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := onnxruntime
LOCAL_SRC_FILES := ./$(TARGET_ARCH_ABI)/libonnxruntime.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/headers
include $(PREBUILT_SHARED_LIBRARY)
这样编译好后,和自己的.so文件一起加入安卓工程。
System.loadLibrary("onnxruntime"); System.loadLibrary("yourlib");
另外一个问题,最近发现编译JNI的时候,在cpp文件里不加
#ifdef __cplusplus
extern "C" {
#endif
会导致方法在Java那边出现
Caused by: java.lang.UnsatisfiedLinkError: No implementation found for
所以尽可能的在cpp文件里也加上。