Ask Your Question
0

problem while load trained svm android

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

manef gravatar image

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

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());
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

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

berak gravatar image

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

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.

edit flag offensive delete link more

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 ( 2019-07-30 06:56:52 -0600 )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 -0600 )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 -0600 )edit

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

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

Question Tools

1 follower

Stats

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

Seen: 445 times

Last updated: Jul 30 '19