gpt4 book ai didi

python - Chaquopy 如何在 python 文件中导入 .so

转载 作者:行者123 更新时间:2023-11-29 22:36:23 28 4
gpt4 key购买 nike

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

sopython文件路径为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)

最佳答案

我使用以下代码成功获取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系统获取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);
}

python 文件:

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;
}
}

关于python - Chaquopy 如何在 python 文件中导入 .so,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59474647/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com