gpt4 book ai didi

android - Android 上使用 Tensorflow 的 C++ 库

转载 作者:行者123 更新时间:2023-12-02 13:46:31 25 4
gpt4 key购买 nike

我正在尝试为 Android 应用程序构建一个 native C++ 库。该库内部使用 Tensorflow C++ API(版本 1.9.0 和 1.10.0)和 OpenCV(3.3.0)。为了生成包装器,我使用 Swig。我正在使用 Android NDK 构建 *.so 文件(我尝试过不同版本的 NDK 版本 10、14、15、17)。我已经从源代码构建了 *.a(libtensorflow-core.a、nsync.a、libprotobuf.a)文件。

defaultConfig {
minSdkVersion 21
targetSdkVersion 28
}

我有以下文件:Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libnsync
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/nsync/builds/armeabi-v7a.android.c++11/nsync.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libprotobuf
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/lib/libprotobuf.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libtensorflow
LOCAL_SRC_FILES := $(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a
LOCAL_EXPORT_C_INCLUDES := \
$(PATH_TO_TENSORFLOW) \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/host_obj \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/eigen \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/gen/protobuf_android/armeabi-v7a/include \
$(PATH_TO_TENSORFLOW)/tensorflow/core/public \
$(PATH_TO_TENSORFLOW)/tensorflow/contrib/makefile/downloads/nsync/public
LOCAL_STATIC_LIBRARIES += libprotobuf libnsync
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_ARM_NEON := true
OPENCV_CAMERA_MODULES := off
include $(OPENCV_PACKAGE_DIR)/native/jni/OpenCV.mk

LOCAL_MODULE := libOcr
LOCAL_SRC_FILES := ocr/Ocr_wrap.cxx
LOCAL_LDLIBS += -landroid -llog -ldl
LOCAL_CPPFLAGS += -std=c++11
LOCAL_STATIC_LIBRARIES += libprotobuf libtensorflow

include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-21 #updated from 19 to 21
APP_OPTIM := release
LOCAL_ARM_NEON := true

在构建过程中,我遇到了很多错误,最后一个是:

[armeabi-v7a] SharedLibrary  : libOcr.so
[armeabi-v7a] Install : liblept.so => libs/armeabi-v7a/liblept.so
[armeabi-v7a] Install : libtess.so => libs/armeabi-v7a/libtess.so
[armeabi-v7a] Install : libgnustl_shared.so => libs/armeabi-v7a/libgnustl_shared.so
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(str_util.o):str_util.cc:function tensorflow::str_util::CUnescape(tensorflow::StringPiece, std::string*, std::string*): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordStep(long long, std::string const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordTensorAllocation(std::string const&, long long, tensorflow::Tensor const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
/Users/tensorflow/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a(log_memory.o):log_memory.cc:function tensorflow::LogMemory::RecordTensorDeallocation(long long, std::string const&): error: undefined reference to 'std::__throw_out_of_range_fmt(char const*, ...)'
collect2: error: ld returned 1 exit status
make: *** [/Users/app/app/src/main/obj/local/armeabi-v7a/libOcr.so] Error 1
make: Leaving directory `/Users/app/app/src/main/jni'
:sdk:buildNative FAILED

FAILURE: Build failed with an exception.

为了构建 *.so 文件,我在 build.gradle 文件中使用下一个任务:

commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath,
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_LOG=1'

亲爱的社区,请帮助我解决这些问题。谢谢。

更新:APP_PLATFORM更改为android-21,结果更好,但仍然有错误

最佳答案

我以前也遇到过这个问题,是 poop 库的问题

关于android - Android 上使用 Tensorflow 的 C++ 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59604623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com