typeerror while converting Pyobject to java array

409 views Asked by At

The pythor script returns 2d 'float64' numpy array. When I try to assign it to 'double' array in java I am getting this error "java.lang.ClassCastException: TypeError: Cannot convert float64 object to double[]"


EDIT

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 code:

PyObject pyobj = py.getModule("featureExtraction");         
PyObject obj = pyobj.callAttr("build_feat","107_2b4_Pl_mc_AKGC417L_0.wav");
double[][] feat = obj.toJava(double[][].class);

for the same code I am getting a different error now: com.chaquo.python.PyException: ValueError: ndarray is not contiguous

1

There are 1 answers

1
mhsmith On BEST ANSWER

The problem is caused by the array being transposed and therefore non-contiguous.

This issue is fixed in Chaquopy 9.0.0. With older versions, you can work around it by either returning the original array (X_sample), or making a copy after transposing (X_sample.T.copy()).