Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

SVM model fails to load in Android NDK

I have trained a SVM model for detecting handwritten characters. On my desktop, I am able to save and load the model and its working perfectly. This is what I've used.

svm.save("./activ_hog_svm");
svm.load("./activ_hog_svm");

It works with both relative and absolute paths. However when I try to load the saved model in my Android app, the load command doesn't seem to work.

JNIEXPORT void JNICALL Java_co_activ_exp_MainActivity_loadSvm(JNIEnv* env, jobject, jstring svm_location)
{
  const char *c_dir = env->GetStringUTFChars(svm_location, NULL);
  LOGD("SVM location %s", c_dir);
  // This prints /storage/emulated/0/Android/data/co.activ.exp/files/mounted/activ_hog_svm
  // I have ensured that the file exists and has right permissions 
  svm.load(c_dir);
  env->ReleaseStringUTFChars(svm_location, c_dir);
}

The model sits in assets folder. I also tried putting it directly at /sdcard but that didn't work either. The error that shows up in logcat when I run a svm.predict later is

OpenCV Error: Bad argument (The SVM should be trained first) in CvSVM::predict, file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/ml/src/svm.cpp, line 21

Any help on how to fix this? Thanks.

SVM model fails to load in Android NDK

I have trained a SVM model for detecting handwritten characters. On my desktop, I am able to save and load the model and its working perfectly. This is what I've used.

svm.save("./activ_hog_svm");
svm.load("./activ_hog_svm");

It works with both relative and absolute paths. However when I try to load the saved model in my Android app, the load command doesn't seem to work.

JNIEXPORT void JNICALL Java_co_activ_exp_MainActivity_loadSvm(JNIEnv* env, jobject, jstring svm_location)
{
  const char *c_dir = env->GetStringUTFChars(svm_location, NULL);
  LOGD("SVM location %s", c_dir);
  // This prints /storage/emulated/0/Android/data/co.activ.exp/files/mounted/activ_hog_svm
  // I have ensured that the file exists and has right permissions 
  svm.load(c_dir);
  env->ReleaseStringUTFChars(svm_location, c_dir);
}

The model sits in assets folder. I also tried putting it directly at /sdcard but that didn't work either. The error that shows up in logcat when I run a svm.predict later is

OpenCV Error: Bad argument (The SVM should be trained first) in CvSVM::predict, file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/ml/src/svm.cpp, line 21

Any help on how to fix this? Thanks.