我已经在我的 Android 应用程序中实现了 Chaquopy,以利用 Python 中预先训练的神经网络模型。
试图调用python代码,我遇到了;
“com.chaquo.python.PyException:ModuleNotFoundError:没有名为‘DataLoader’的模块
我不确定我是否错误地实现了 Chaquopy 的文件结构,或者是否有其他原因无法导入 DataLoader 模块。
DataLoader.py 与 main.py 位于同一位置,在 app/src/main/python/SimpleHRT/ 内,所以我不明白为什么它无法访问该模块。
从Android app调用python;
convertImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (! Python.isStarted()){
Python.start(new AndroidPlatform(getActivity()));
}
else{
Python py = Python.getInstance();
PyObject test = py.getModule("SimpleHRT/main");
}
}
});
从 python main.py
from __future__ import division
from __future__ import print_function
import sys
import argparse
import cv2
import editdistance
from DataLoader import DataLoader, Batch
from Model import Model, DecoderType
from SamplePreprocessor import preprocess
从错误日志
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teesside.yellowann, PID: 5735
com.chaquo.python.PyException: ModuleNotFoundError: No module named 'DataLoader'
at <python>.java.chaquopy.import_override(import.pxi:18)
at <python>.java._vendor.six.reraise(six.py:686)
at <python>.java.chaquopy.import_override(import.pxi:59)
at <python>.SimpleHRT/main.<module>(main.py:8)
at <python>.java.android.importer.load_module_impl(importer.py:435)
at <python>.java.android.importer.load_module(importer.py:353)
at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:994)
at <python>.importlib.import_module(__init__.py:126)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
at com.chaquo.python.Python.getModule(Native Method)
at com.teesside.yellowann.ImageFragment$3.onClick(ImageFragment.java:173)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 5735 SIG: 9
Application terminated.
编辑继续:
当这几乎是同一件事时,不想提出一个全新的问题。
现在尝试实施:
Android Java 调用
convertImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if (! Python.isStarted()){
Python.start(new AndroidPlatform(getActivity()));
}
else{
Python py = Python.getInstance();
PyObject test = py.getModule("SimpleHRT.main").get("main");
PyObject test2 = test.call();
}
}
});
Python 文件路径
class FilePaths:
"filenames and paths to data"
fnCharList = '../model/charList.txt'
fnAccuracy = '../model/accuracy.txt'
fnTrain = '../data/'
fnInfer = '../data/test.png'
fnCorpus = '../data/corpus.txt'
Python 调用
model = Model(open(FilePaths.fnCharList).read(), decoderType, mustRestore=True)
infer(model, FilePaths.fnInfer)
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.teesside.yellowann, PID: 9330
com.chaquo.python.PyException: FileNotFoundError: [Errno 2] No such file or directory: 'SimpleHRT/../model/charList.txt'
at <python>.SimpleHRT.main.main(main.py:138)
at <python>.chaquopy_java.call(chaquopy_java.pyx:283)
at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callThrows(chaquopy_java.pyx:243)
at com.chaquo.python.PyObject.callThrows(Native Method)
at com.chaquo.python.PyObject.call(PyObject.java:190)
at com.teesside.yellowann.ImageFragment$3.onClick(ImageFragment.java:176)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 9330 SIG: 9
Application terminated.
我试过“./model/charList.txt”、“SimpleHRT/model/charList.txt”、“SimpleHRT/../model/charList.txt”、“.../model/charList.txt”、“。 /../model/charList.txt"
基础 python 在“src/main/python/SimpleHRT/”中,而我需要读取的文档在“src/main/python/SimpleHRT/model/”中
我假设我没有掌握的文件路径有一些细微差别......