java.lang.NoSuchMethodError 当 运行 TensorFlow Lite 在 android (Java 8)

java.lang.NoSuchMethodError when running TensorFlow Lite on android (Java 8)

我正在 运行在 Softbank Robotics 的 Pepper 1.8 (QiSDK 1.7.5) 上使用定制的 Tensorflow Lite 模型,它是 运行正在 android。

我成功捕获图像,将其转换为 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$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(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'

}

如有任何帮助,我将不胜感激。

您需要移除以下依赖

'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没用,可以去掉