Ask Your Question

problem while load trained svm android

asked 2019-07-29 09:04:34 -0500

manef gravatar image

updated 2019-07-30 06:57:49 -0500

hello guys, i'm working on regions detection, so i'm using SVM with opencv 3.4 on Android studio, i tried to save the trained model of SVM and loaded after but for some reasons that i don't know the load function didn't work in my case ( I'm using Blue stack 4 emulator connected to android studio).The SVM is working fine if i trained him each time ( without using save and load), in my code i used the "getAbsolutePath()" function to get the xml file path and give it to the load function but it don't works. the problem is in the opencv android or in my code please help

SVM svm = SVM.create();
svm.setTermCriteria(new TermCriteria(TermCriteria.MAX_ITER, 100, 1e-6));
svm.train(trainingDataMat, ROW_SAMPLE, labelsMat);

File datasetFile = new File(Environment.getExternalStorageDirectory(), "dataset.xml");;

for loading the trained SVM i used this lines of code

 SVM svm = SVM.create();
 String path = "/storage/emulated/0/dataset.xml";
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-07-29 09:07:23 -0500

berak gravatar image

updated 2019-07-29 10:00:29 -0500

here's your problem:


load() does not work, like you expect it. it's not a member function, but a static one, returning a new object.

use it like:

SVM svm = SVM.load(some_path);

else you throw away everything, and leave your svm untrained.

edit flag offensive delete link more


thanks it worked, the problem was that i created the SVM using SVM.ceate() and then load my XML file. @berak instead of training SVM with android i changed to C++ and now i need to load the trained svm (xml file), but i couldn't find a tuto that show me where to put the xml file in my android project and how to loaded him. need your help if possible.

manef gravatar imagemanef ( 2019-07-30 06:56:52 -0500 )edit

i don't have android around, but opencv can only read from uncompressed storage (like the SDCARD)

if you add your xml to your project resources, and it will get "compiled" into your (zipped !) apk, you'll have to manually copy it to sdcard, tmp, or such, before you can use it..

you can look at how this done in the android examples, e.g. here

berak gravatar imageberak ( 2019-07-30 10:37:33 -0500 )edit

thanks @berak for your answear. i got that problem but a big problem is that the Loading the trained SVM RBF don't work correctly ( a bug for everyone) only the Linear svm is work fine :(

manef gravatar imagemanef ( 2019-08-02 05:14:20 -0500 )edit

indeed, you cannot use an RBF kernel here, only a LINEAR one.

berak gravatar imageberak ( 2019-08-02 05:31:51 -0500 )edit

Question Tools

1 follower


Asked: 2019-07-29 09:04:34 -0500

Seen: 121 times

Last updated: Jul 30 '19