Ask Your Question

Android App don't load SVM trained (on C++) file for recognition

asked 2017-06-07 04:27:42 -0500

sLeo gravatar image

I've trained a svm using OpenCV 3.2 on C++. Just like that

// Set up SVM's parameters
Ptr<SVM> svm = SVM::create();
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));

// Train the SVM with given parameters
Ptr<TrainData> td = TrainData::create(training_mat, ROW_SAMPLE, labelsMat);

Now I try to load the produced xml file on Android, I use the OpenCV4Android (OpenCV 3.2). But the load function seems not to work. the load function SVM svm = SVM.load("svm_filename.xml"); just throws an exception (the path is correct because I included the xml file in the App).

cv::Exception: /build/master_pack-android/opencv/modules/ml/src/svm.cpp:2123: error: (-212) Missing or invalid SVM type in function void cv::ml::SVMImpl::read_params(const cv::FileNode&)

On C++ everything's fine, the load function works fine


I don't understand why the svm type is mentioned as missing. When I take a look a the XML file the svm type is C_SVC. here is a part of the xmlimage description

I don't know what I miss.

can someone please help me?

edit retag flag offensive close merge delete


svm->load("images/svm_filename.xml"); this is wrong, and won't do anything useful. please use:

Ptr<SVM> svm = StatModel::load>SVM>("my.xml");

(it's a static function, which returns a new object)

berak gravatar imageberak ( 2017-06-07 23:37:26 -0500 )edit

@berak I'm the one who post the question but I can't sign in with sleo, that's why I create a new user account.


the function is working fine and gives satisfying result in C++. but I want to load the xml file in Android like this

SVM svm = SVM.load("svm_filename.xml")

using the OpenCV4Android library (OpenCV 3.2)

sleo5 gravatar imagesleo5 ( 2017-06-08 17:19:23 -0500 )edit

if you think, we can help with your account problem: issue here

can it be, your c++ and java versions are slightly "out-of-sync" and disagree, where to look for e.g. the svm type in the xml ? (there were bugs/changes in early 3.2 there)

would updating the src code, and building the android wrappers locally be an option for you ?

berak gravatar imageberak ( 2017-06-08 22:49:03 -0500 )edit

@sleo5 finally how you load xml?

jikolp95 gravatar imagejikolp95 ( 2018-03-07 06:24:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-24 02:39:31 -0500

Ramir gravatar image


It might be the path. The path should be




if you are using external directory.

edit flag offensive delete link more


imho, you misread it. the svm was trained (and saved) on a desktop pc, not android

berak gravatar imageberak ( 2018-02-24 02:47:07 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-06-07 04:27:42 -0500

Seen: 954 times

Last updated: Feb 24 '18