Ask Your Question
0

problem while load trained svm android

asked Jul 29 '19

manef gravatar image

updated Jul 30 '19

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.setType(SVM.C_SVC);
svm.setKernel(SVM.RBF);
svm.setC(10000);
svm.setGamma(0.00001);
svm.setTermCriteria(new TermCriteria(TermCriteria.MAX_ITER, 100, 1e-6));
svm.train(trainingDataMat, ROW_SAMPLE, labelsMat);

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

for loading the trained SVM i used this lines of code

 SVM svm = SVM.create();
 String path = "/storage/emulated/0/dataset.xml";
 svm.load(path());
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Jul 29 '19

berak gravatar image

updated Jul 29 '19

here's your problem:

svm.load(datasetFile.getAbsolutePath());

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.

Preview: (hide)

Comments

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 (Jul 30 '19)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 (Jul 30 '19)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 (Aug 2 '19)edit

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

berak gravatar imageberak (Aug 2 '19)edit

Question Tools

1 follower

Stats

Asked: Jul 29 '19

Seen: 455 times

Last updated: Jul 30 '19