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

Time:11-06

I am running a custom made Tensorflow Lite model on Softbank Robotics' Pepper 1.8 (QiSDK 1.7.5) which is running android .

I capture the image successfully, I convert it to bitmap and the I convert it to TensorImage. After that I build the ObjectDetector and run it. Below you can see my detection function:

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 gets initialized but after that I get a FATAL EXCEPTION. This is my error log:

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

And this is my build.gradle file

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'

}

I would appreciate any help.

CodePudding user response:

You need to remove the following dependency

'org.tensorflow:tensorflow-lite-support:0.1.0'

org.tensorflow:tensorflow-lite-task-vision:0.0.0-nightly-SNAPSHOT uses a newer version of the Support library (0.2.0 ), which as the ColorSpaceType.getValue() method. But it gets overridden by support:0.1.0.

Also org.tensorflow:tensorflow-lite-metadata:0.1.0 is useless, which can be removed.

  • Related