在 android (Java 8) 上运行 TensorFlow Lite 时出现 java.lang.NoSuchMethodError
Posted
技术标签:
【中文标题】在 android (Java 8) 上运行 TensorFlow Lite 时出现 java.lang.NoSuchMethodError【英文标题】:java.lang.NoSuchMethodError when running TensorFlow Lite on android (Java 8) 【发布时间】:2021-12-18 14:27:37 【问题描述】:我正在运行 android 的 Softbank Robotics 的 Pepper 1.8 (QiSDK 1.7.5)
上运行定制的 Tensorflow Lite 模型。
我成功捕获图像,将其转换为bitmap
,然后将其转换为TensorImage
。
之后我构建ObjectDetector
并运行它。下面你可以看到我的检测功能:
private void runDetection(Bitmap pictureBitmap) throws IOException
TensorImage image = TensorImage.fromBitmap(pictureBitmap);
ObjectDetector.ObjectDetectorOptions options = ObjectDetector
.ObjectDetectorOptions.builder()
.setMaxResults(5)
.setScoreThreshold(0.5F)
.build();
ObjectDetector detector = ObjectDetector
.createFromFileAndOptions(this, "detect_2_5.tflite", options);
List results = detector.detect(image);
Tnesorflow 被初始化,但之后我得到一个致命的异常。这是我的错误日志:
2021-11-04 11:57:04.465 11363-11406/com.example.test I/FOCUS: Gained
2021-11-04 11:57:05.052 11363-11406/com.example.test I/LOOK: LookAt action started.
2021-11-04 11:57:10.273 11363-11400/com.example.test D/GraphicBuffer: register, handle(0xab568140) (w:800 h:1280 s:800 f:0x1 u:0x000f02)
2021-11-04 11:57:10.303 11363-11363/com.example.test V/SettingsInterface: invalidate [system]: current 4 != cached 0
2021-11-04 11:57:10.382 11363-11397/com.example.test I/CAMERA: Take picture launched
2021-11-04 11:57:10.929 11363-11395/com.example.test I/CAMERA: Picture taken
2021-11-04 11:57:11.304 11363-11395/com.example.test I/CAMERA: Picture received from Pepper CPU
2021-11-04 11:57:11.306 11363-11395/com.example.test I/CAMERA: PICTURE RECEIVED! (290 KBytes)
2021-11-04 11:57:11.314 11363-11395/com.example.test D/skia: jpeg_decoder mode 1, colorType 4, w 1280, h 960, sample 1, bsLength 48bce!!
2021-11-04 11:57:11.370 11363-11395/com.example.test D/skia: jpeg_decoder finish successfully, L:1906!!!
2021-11-04 11:57:14.316 11363-11363/com.example.test D/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 0
2021-11-04 11:57:14.377 11363-11363/com.example.test I/tflite: Initialized TensorFlow Lite runtime.
2021-11-04 11:57:14.400 11363-11373/com.example.test I/System: FinalizerDaemon: finalize objects = 584
2021-11-04 11:57:14.642 11363-11363/com.example.test D/AndroidRuntime: Shutting down VM
2021-11-04 11:57:14.655 11363-11363/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 11363
java.lang.NoSuchMethodError: No virtual method getValue()I in class Lorg/tensorflow/lite/support/image/ColorSpaceType; or its super classes (declaration of 'org.tensorflow.lite.support.image.ColorSpaceType' appears in /data/app/com.example.test-1/base.apk)
at org.tensorflow.lite.task.vision.core.BaseVisionTaskApi.createFrameBufferFromByteBuffer(BaseVisionTaskApi.java:144)
at org.tensorflow.lite.task.vision.core.BaseVisionTaskApi.createFrameBuffer(BaseVisionTaskApi.java:69)
at org.tensorflow.lite.task.vision.core.BaseVisionTaskApi.run(BaseVisionTaskApi.java:48)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.detect(ObjectDetector.java:436)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.detect(ObjectDetector.java:412)
at com.example.test.MainActivity.runDetection(MainActivity.java:280)
at com.example.test.MainActivity.lambda$onCreate$1$com-example-test-MainActivity(MainActivity.java:115)
at com.example.test.MainActivity$$ExternalSyntheticLambda1.onClick(Unknown Source)
at android.view.View.performClick(View.java:5265)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5875)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
2021-11-04 11:57:15.236 11363-11406/com.example.test I/FOCUS: Lost
2021-11-04 11:57:15.341 11363-11397/com.example.test E/LOOK: LookAt action finished with error.
com.aldebaran.qi.QiException: Focus lost
at com.aldebaran.qi.Future.qiFutureCallGet(Native Method)
at com.aldebaran.qi.Future.get(Future.java:237)
at com.aldebaran.qi.Future.get(Future.java:272)
at com.aldebaran.qi.Future.getError(Future.java:307)
at com.example.test.MainActivity.lambda$onRobotFocusGained$4(MainActivity.java:179)
at com.example.test.MainActivity$$ExternalSyntheticLambda3.consume(Unknown Source)
2021-11-04 11:57:20.248 11363-11373/com.example.test W/BaseTaskApi: Closing an already closed native lib
2021-11-04 12:02:14.731 11363-11363/com.example.test I/Process: Sending signal. PID: 11363 SIG: 9
这是我的 build.gradle 文件
plugins
id 'com.android.application'
android
compileSdk 31
defaultConfig
applicationId "com.example.test"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
// Specify tflite file should not be compressed for the app apk
aaptOptions
noCompress "tflite"
buildFeatures
mlModelBinding true
dependencies
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0'
implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.aldebaran:qisdk:1.7.5'
implementation 'com.aldebaran:qisdk-design:1.7.5'
implementation 'org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT'
我将不胜感激。
【问题讨论】:
【参考方案1】:你需要删除以下依赖
'org.tensorflow:tensorflow-lite-support:0.1.0'
org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT
使用更新版本的支持库 (0.2.0 +),它作为 ColorSpaceType.getValue() 方法。但它会被support:0.1.0
覆盖。
还有org.tensorflow:tensorflow-lite-metadata:0.1.0
没用,可以去掉。
【讨论】:
谢谢王路,成功了,我更喜欢用org.tensorflow:tensorflow-lite-task-vision:0.2.0
而不是org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT
吗?
我实际上需要org.tensorflow:tensorflow-lite-metadata:0.1.0
,因为detect.java
正在使用它。
org.tensorflow:tensorflow-lite-task-vision:0.2.0
比nightly-SNAPSHOT
更稳定,我推荐它用于生产。以上是关于在 android (Java 8) 上运行 TensorFlow Lite 时出现 java.lang.NoSuchMethodError的主要内容,如果未能解决你的问题,请参考以下文章
通过 Appium 在 Android 操作系统上运行 Selenium 测试
运行 Android 8 的三星 S7 Edge 上的“FirebaseInstallations.java”后台崩溃大幅飙升