1

android构建成功,但在运行时抛出错误。

so并且python文件路径是 app\src\main\python。

1.so文件路径更改为app\src\main\jniLibs\arm64-v8a或app\src\main\assets也有同样的错误。

2.在python文件更改so文件路径,使用/sdcard/FaceDetect/python/andoir.cpython-35m-x86_64-linux-gnu.so也有同样的错误。

我用chaquopy。如何在python文件中使用so文件?so文件应该设置在哪里?

谢谢!python文件detect.py:

from ctypes import cdll
import os

def detectImg(img_path):
    print("yxq-python::::" + img_path)
    print(os.path.abspath(__file__))
    print(os.path.dirname(os.path.abspath(__file__)))
    # cur = cdll.LoadLibrary('/sdcard/FaceDetect/python/andoir.cpython-35m-x86_64-linux-gnu.so')
    cur = cdll.LoadLibrary('./andoir.cpython-35m-x86_64-linux-gnu.so')
    model_path = "/sdcard/FaceDetect/python/sfd_face.pth"
    cur.diaoyong(img_path, model_path)

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.boyun.hisensetestheadcount, PID: 1642
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.boyun.hisensetestheadcount/com.boyun.hisensetestheadcount.MainActivity}: com.chaquo.python.PyException: OSError: dlopen failed: library "./andoir.cpython-35m-x86_64-linux-gnu.so" not found
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2671)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6141)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
    Caused by: com.chaquo.python.PyException: OSError: dlopen failed: library "./andoir.cpython-35m-x86_64-linux-gnu.so" not found
    at <python>.ctypes.__init__(__init__.py:348)
    at <python>.ctypes.LoadLibrary(__init__.py:426)
    at <python>.detect.detectImg(detect.py:9)
    at <python>.chaquopy_java.call(chaquopy_java.pyx:283)
    at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:255)
    at com.chaquo.python.PyObject.callAttrThrows(Native Method)
    at com.chaquo.python.PyObject.callAttr(PyObject.java:207)
    at com.boyun.hisensetestheadcount.MainActivity.detectImg(MainActivity.java:245)
    at com.boyun.hisensetestheadcount.MainActivity.onCreate(MainActivity.java:86)
    at android.app.Activity.performCreate(Activity.java:6709)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
4

1 回答 1

1

我使用以下代码成功获取so文件。

谢谢mhsmith回答我的问题。

我发现pytorch可以直接在android系统中使用,点击这个https://pytorch.org/mobile/android/

我将so文件放在assets包中,并使用以下代码中的方法在python文件中获取.so文件。

因为算法工程师给出的so文件不是为Android系统构建的,所以当我运行应用程序时出现如下错误:

Caused by: com.chaquo.python.PyException: OSError: dlopen failed: "/data/data/com.boyun.hisensetestheadcount/files/andoir.cpython-35m-x86_64-linux-gnu.so" has unexpected e_machine: 62

android系统中method获取so文件路径:

java代码调用python:

void detectImg(){
    Python py = Python.getInstance();
    String soPath = assetFilePath(this,"andoir.cpython-35m-x86_64-linux-gnu.so");
    py.getModule("detect").callAttr("detectImg2", "/storage/emulated/0/FaceImg/star/timg.jpg",soPath);
}

蟒蛇文件:

def detectImg2(img_path,so_path):
    print("yxq-python::::" + img_path)
    print(os.path.abspath(__file__))
    print(os.path.dirname(os.path.abspath(__file__)))
    # cur = cdll.LoadLibrary('/sdcard/FaceDetect/python/andoir.cpython-35m-x86_64-linux-gnu.so')
    cur = cdll.LoadLibrary(so_path)
    model_path = "/sdcard/FaceDetect/python/sfd_face.pth"
    cur.diaoyong(img_path, model_path)

在android系统中获取so文件路径。

import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Utils {
public static String assetFilePath(Context context, String assetName) {
    File file = new File(context.getFilesDir(), assetName);
    try (InputStream is = context.getAssets().open(assetName)) {
            try (OutputStream os = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024];
                int read;
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
            }
            return file.getAbsolutePath();
        } catch (IOException e) {
            Log.e("pytorchandroid", "Error process asset " + assetName + " to file path");
        }
        return null;
    }
}
于 2019-12-27T08:15:49.710 回答