How to use tensorflow lite + kotlin on Windows Android studio without launching the AVD?

Viewed 163

I am trying to load a model and test it on windows in android studio. I don't want to test in the AVD, because it takes more time. So tried to run this code without AVD (I use a different method, to load the mappedBuffer, when launching instrumented tests, because I have the model file in Android resources, but this loadFileToMappedBuffer(...) should also work):

import android.os.Build
import androidx.annotation.RequiresApi
import org.tensorflow.lite.Interpreter
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.util.*

@RequiresApi(Build.VERSION_CODES.O)
fun loadFileToMappedBuffer(fileName: String): MappedByteBuffer {
    val path = Paths.get(fileName)
    val fileChannel: FileChannel =
        Files.newByteChannel(path, EnumSet.of(StandardOpenOption.READ)) as FileChannel
    val mappedByteBuffer: MappedByteBuffer =
        fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size())
    return mappedByteBuffer
}

@RequiresApi(Build.VERSION_CODES.O)
fun main() {

    val interpreter: Interpreter = Interpreter(
        loadFileToMappedBuffer("C:\\Users\\PC\\Documents\\mnist.tflite")
    )
}

Unfortunetely I get this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to load native TensorFlow Lite methods. Check that the correct native libraries are present, and, if using a custom native library, have been properly loaded via System.loadLibrary():
  java.lang.UnsatisfiedLinkError: no tensorflowlite_jni in java.library.path
    at org.tensorflow.lite.TensorFlowLite.init(TensorFlowLite.java:80)
    at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:52)
    at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:277)
    at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:262)
    at com.ml.models.TestMnistModelKt.main(testMnistModel.kt:28)
    at com.ml.models.TestMnistModelKt.main(testMnistModel.kt)

What should I do to load the Kotlin tensorflow library and run the model without launching the AVD?

0 Answers
Related