将 Pyobject 转换为 java 数组时出现类型错误
typeerror while converting Pyobject to java array
pythor 脚本 returns 2d 'float64' numpy 数组。当我尝试将它分配给 java 中的 'double' 数组时,出现此错误“java.lang.ClassCastException:TypeError:无法将 float64 对象转换为 double[]”
编辑
import librosa as lb
import numpy as np
from os.path import dirname, join
def build_feat(fpath):
filename = join(dirname(__file__), fpath)
wav, rate = lb.load(filename, sr=4000)
X_sample = lb.feature.mfcc(wav, sr=rate, n_fft=512, win_length=400, n_mfcc=20,
hop_length = 256, n_mels = 128, fmin = 100, fmax = 1800)
X = X_sample.T
return X
Java代码:
PyObject pyobj = py.getModule("featureExtraction");
PyObject obj = pyobj.callAttr("build_feat","107_2b4_Pl_mc_AKGC417L_0.wav");
double[][] feat = obj.toJava(double[][].class);
对于相同的代码,我现在收到不同的错误:com.chaquo.python.PyException: ValueError: ndarray is not contiguous
问题是由数组被转置引起的,因此 non-contiguous。
此问题已在 Chaquopy 9.0.0 中修复。对于旧版本,您可以通过返回原始数组 (X_sample
) 或使转置后的副本 (X_sample.T.copy()
).
pythor 脚本 returns 2d 'float64' numpy 数组。当我尝试将它分配给 java 中的 'double' 数组时,出现此错误“java.lang.ClassCastException:TypeError:无法将 float64 对象转换为 double[]”
编辑
import librosa as lb
import numpy as np
from os.path import dirname, join
def build_feat(fpath):
filename = join(dirname(__file__), fpath)
wav, rate = lb.load(filename, sr=4000)
X_sample = lb.feature.mfcc(wav, sr=rate, n_fft=512, win_length=400, n_mfcc=20,
hop_length = 256, n_mels = 128, fmin = 100, fmax = 1800)
X = X_sample.T
return X
Java代码:
PyObject pyobj = py.getModule("featureExtraction");
PyObject obj = pyobj.callAttr("build_feat","107_2b4_Pl_mc_AKGC417L_0.wav");
double[][] feat = obj.toJava(double[][].class);
对于相同的代码,我现在收到不同的错误:com.chaquo.python.PyException: ValueError: ndarray is not contiguous
问题是由数组被转置引起的,因此 non-contiguous。
此问题已在 Chaquopy 9.0.0 中修复。对于旧版本,您可以通过返回原始数组 (X_sample
) 或使转置后的副本 (X_sample.T.copy()
).