dkurt / openvino_java

Build, package and publish OpenVINO for Java
Apache License 2.0
12 stars 10 forks source link

Can't get absolute file path libopenvino.so #18

Closed tamnguyenvan closed 1 year ago

tamnguyenvan commented 1 year ago

I got this error while running my Android app. image

Could you help me please, @dkurt MainActivity.java

package com.example.wav2vec2openvino;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.intel.openvino.*;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try{
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        } catch (UnsatisfiedLinkError e) {
            Log.e("UnsatisfiedLinkError",
                    "Failed to load native OpenVINO libraries\n" + e.toString());
            System.exit(1);
        }

        Log.i("APP", "Loading model...");
        Core core = new Core("/data/local/tmp/plugins.xml");
        core.read_model("/data/local/tmp/face-detection-adas-0001.xml");
        Log.i("APP", "Loaded model successfully!");
    }
}

build.gradle

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.wav2vec2openvino'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.wav2vec2openvino"
        minSdk 23
        targetSdk 32
        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
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation files('libs/openvino-2022.3-android-arm64-v8a.aar')

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

jniLibs/arm64-v8a image

dkurt commented 1 year ago

@tamnguyenvan, can you check which CPU is used? OpenVINO is built for 64bit CPU. Also, please replace a screenshot to text with full error message.

dkurt commented 1 year ago

May you also try 2022.2 binaries?

tamnguyenvan commented 1 year ago

@tamnguyenvan, can you check which CPU is used? OpenVINO is built for 64bit CPU. Also, please replace a screenshot to text with full error message.

Here is the logs.

$ adb shell am start -n "com.example.wav2vecopenvino/com.example.wav2vecopenvino.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 24642 on device 'vsmart-active_3-BAD00134802'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/wav2vecopenvin: Late-enabling -Xcheck:jni
E/wav2vecopenvin: Unknown bits set in runtime_flags: 0x8000
W/wav2vecopenvin: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/wav2vecopenvin: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
W/linker: Warning: "/data/app/com.example.wav2vecopenvino-mnBU2RaLEAGFsZ0EDD7h6g==/base.apk!/lib/arm64-v8a/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
W/linker: Warning: "/data/app/com.example.wav2vecopenvino-mnBU2RaLEAGFsZ0EDD7h6g==/base.apk!/lib/arm64-v8a/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wav2vecopenvino, PID: 24642
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wav2vecopenvino/com.example.wav2vecopenvino.MainActivity}: java.lang.Exception: 
    InferenceEngineException: 
        GetCore_1: Can't get absolute file path for [/data/app/com.example.wav2vecopenvino-mnBU2RaLEAGFsZ0EDD7h6g==/base.apk!/lib/arm64-v8a/libopenvino.so], err = No such file or directory
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3300)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3439)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7447)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
     Caused by: java.lang.Exception: 
    InferenceEngineException: 
        GetCore_1: Can't get absolute file path for [/data/app/com.example.wav2vecopenvino-mnBU2RaLEAGFsZ0EDD7h6g==/base.apk!/lib/arm64-v8a/libopenvino.so], err = No such file or directory
        at org.intel.openvino.compatibility.IECore.GetCore1(Native Method)
        at org.intel.openvino.compatibility.IECore.<init>(IECore.java:25)
        at com.example.wav2vecopenvino.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3275)
            ... 11 more
I/Process: Sending signal. PID: 24642 SIG: 9

And this is my phone info. photo_2023-01-09_13-56-22

tamnguyenvan commented 1 year ago

May you also try 2022.2 binaries?

Same error :(

dkurt commented 1 year ago

In the shell, uname -m also reports 64bit ARM? Have you tried run application using Android Studio?

tamnguyenvan commented 1 year ago

In the shell, uname -m also reports 64bit ARM? Have you tried run application using Android Studio?

Yes. image

That error from Android Studio.

tamnguyenvan commented 1 year ago

@dkurt Can you kindly help me with this?

dkurt commented 1 year ago

@tamnguyenvan, in progress. Need some time to reproduce.

dkurt commented 1 year ago

@tamnguyenvan, I just tried reproduce https://medium.com/@dmitry.kurtaev/audio-recognition-in-android-with-openvino-5d51e71f1426 demo (complete code: https://github.com/dkurt/audio_recognition_android) on Redmi 9S and everything works fine with 2022.3 OpenVINO and Android Studio Dolphin (2021.3.1 Patch 1).

Can you check that there is enough memory on you device? Wav2Vec must be a huge model and there should be a free space for native libraries.

tamnguyenvan commented 1 year ago

@tamnguyenvan, I just tried reproduce https://medium.com/@dmitry.kurtaev/audio-recognition-in-android-with-openvino-5d51e71f1426 demo (complete code: https://github.com/dkurt/audio_recognition_android) on Redmi 9S and everything works fine with 2022.3 OpenVINO and Android Studio Dolphin (2021.3.1 Patch 1).

Can you check that there is enough memory on you device? Wav2Vec must be a huge model and there should be a free space for native libraries.

It also failed on face-detection-adas-0001.xml. I don't think the issue is about memory space.

dkurt commented 1 year ago

Please share more details about Android Studio version, NDK (SDK) version, API, Device, Java version.

tamnguyenvan commented 1 year ago
Android Studio Dolphin | 2021.3.1 Patch 1
NDK 25.1.8937393
SDK 33.0.1
Device VSmart Active 3
Java 8

MainActivity.java

package com.example.wav2vecopenvino;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import org.intel.openvino.Core;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize OpenVINO
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // Create OpenVINO Core object using temporal plugins.xml
        Core core = new Core("/data/local/tmp/plugins.xml");
    }
}

build.gradle

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.wav2vecopenvino'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.wav2vecopenvino"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            debuggable true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation files('libs/openvino-2022.2-android-arm64-v8a.aar')

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
tamnguyenvan commented 1 year ago

@dkurt , Could you reproduce it? If not, you can remote my laptop to check if it's an OpenVINO's bug.

dkurt commented 1 year ago

@tamnguyenvan, yep, that works for me. At least System.loadLibrary(Core.NATIVE_LIBRARY_NAME); passed.

Not sure that this is a bug with OpenVINO. May you try Android x86_64 binaries using emulator?

tamnguyenvan commented 1 year ago

@tamnguyenvan you may contact me via Telegram: tamnvcc

tamnguyenvan commented 1 year ago

Temporary solution as @dkurt has suggested.

private static String getResourcePath(InputStream in, String name, String ext) {
    String path = "";
    try {
        Path plugins = Files.createTempFile(name, ext);
        Files.copy(in, plugins, StandardCopyOption.REPLACE_EXISTING);
        path = plugins.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return path;
}
String libPath = null;
try {
    logger.info("load OV");
    libPath = getResourcePath(getAssets().open("libopenvino.so"), "libopenvino", "so");
    System.load(libPath);

    logger.info("load JAVA API");
    libPath = getResourcePath(getAssets().open("libinference_engine_java_api.so"), "libinference_engine_java_api", "so");
    System.load(libPath);
} catch (IOException e) {
    e.printStackTrace();
}

// Create OpenVINO Core object using temporal plugins.xml
InputStream in = Core.class.getClassLoader().getResourceAsStream("plugins.xml");
String pluginsXml = getResourcePath(in, "plugins", "xml");

Core core = new Core(pluginsXml);
logger.info("DONE");
dkurt commented 1 year ago

@tamnguyenvan , please try this:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import org.intel.openvino.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {

    private static final Logger logger = Logger.getLogger(Core.class.getName());

    void loadOpenVINO() throws IOException {
        // Create a temporal folder
        File tmpDir = Files.createTempDirectory("openvino-native").toFile();
        tmpDir.deleteOnExit();

        // Copy native resources to a temporal folder
        String[] nativeFiles = {
                "plugins.xml",
                "libc++_shared.so",
                "libtbb.so",
                "libtbbmalloc.so",
                "libopenvino.so",
                "libinference_engine_java_api.so",
                "libopenvino_ir_frontend.so",
                "libopenvino_arm_cpu_plugin.so"
        };
        for (String file : nativeFiles) {
            logger.info("Copy " + file);
            InputStream in = getAssets().open(file);
            Path nativeFileTmpFile = new File(tmpDir, file).toPath();
            Files.copy(in, nativeFileTmpFile, StandardCopyOption.REPLACE_EXISTING);
        }

        String[] nativeLibs = {
                "libc++_shared.so",
                "libtbb.so",
                "libtbbmalloc.so",
                "libopenvino.so",
                "libinference_engine_java_api.so"
        };
        for (String lib : nativeLibs) {
            logger.info("Load " + lib);
            File nativeLibTmpFile = new File(tmpDir, lib);
            System.load(nativeLibTmpFile.getAbsolutePath());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            loadOpenVINO();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Core core = new Core();

        Path modelXml = null, modelBin = null;
        try {
            modelXml = Files.createTempFile("ov_model", "xml");
            Files.copy(getAssets().open("ov_model.xml"), modelXml, StandardCopyOption.REPLACE_EXISTING);
            modelBin = Files.createTempFile("ov_model", "bin");
            Files.copy(getAssets().open("ov_model.bin"), modelBin, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.info("Read model");
        Model model = core.read_model(modelXml.toString(), modelBin.toString());
        logger.info("Compile model");
        CompiledModel compiled = core.compile_model(model, "CPU");
        logger.info("Done");
    }
}
dkurt commented 1 year ago

Were able reproduce the following error on Android 7.1.2 with API level 25:

CompileModel: Cannot load library '/data/data/com.example.testov/cache/libopenvino_arm_cpu_plugin.so: dlopen failed: cannot locate symbol "pthread_cond_clockwait" referenced by "/data/data/com.example.testov/cache/libopenvino_arm_cpu_plugin.so"...

Reproduced with the binaries with ANDROID_PLATFORM=32. When I change to ANDROID_PLATFORM=26, everything is fine. Let's wait for @tamnguyenvan experiment on 26 API level binaries.