4

I need some help in training a SVM for an Android app. I have a set of images in different classes (12 classes) and got all descriptors from them. I managed to get the same amount of descriptors for each image. What I need is to train a SVM for my android application with those descriptors. I'm not sure if I should train it in the Android emulator or write a C++ program to train the SVM and then load it in my app (if I use the OpenCV's lib for windows to train the SVM and then save it, will the lib I'm using for Android recognize the saved SVM file?). I guess I shouldn't train the SVM with such big dataset in the emulator. I've already tested my descriptor's dataset on the SMO of Weka (http://www.cs.waikato.ac.nz/ml/weka/) and got good results, but I need to implement (or use the openCV's) SVM and save it trained for future classification.

Cœur
  • 37,241
  • 25
  • 195
  • 267
mtndg
  • 73
  • 1
  • 4
  • I'm new to OpenCV and I would like to ask you a question. What does it mean that you have 12 different classes? – dephinera Dec 23 '14 at 21:19

1 Answers1

6

Here is an example for training your SVM in OpenCV4Android. trainData is a MatOfFloat, the form of which will depend on the method you're using to get feature vectors. To make trainData, I used Core.hconcat() to concatenate the feature vectors for each element of the dataset into a single Mat.

Mat responses = new Mat(1, sizeOfDataset, CvType.CV_32F);
responses.put(0, 0, labelArray); // labelArray is a float[] of labels for the data

CvSVM svm = new CvSVM();
CvSVMParams params = new CvSVMParams();
params.set_svm_type(CvSVM.C_SVC);
params.set_kernel_type(CvSVM.LINEAR);
params.set_term_crit(new TermCriteria(TermCriteria.EPS, 100, 1e-6)); // use TermCriteria.COUNT for speed

svm.train_auto(trainData, responses, new Mat(), new Mat(), params);

I'm fairly sure OpenCV uses the same format to save SVMs in both the Android and C++ interfaces. Of course, you can always train the SVM in Android and save the XML file to your emulator's SD card using something like

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

then pull it from the SD card and store it in your app's /res/raw folder.

1''
  • 26,823
  • 32
  • 143
  • 200
  • Thank you very much for the answer! I had a little trouble setting the trainData but after reading [this](http://stackoverflow.com/questions/14694810/using-opencv-and-svm-with-images) post and the [documentation](http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html) I figured it out. – mtndg Apr 28 '13 at 19:29
  • @1'' : can you please provide some suggestions on [this question](http://stackoverflow.com/questions/29072000/opencv4android-template-matching-using-camera) ? – Mehul Joisar Mar 16 '15 at 08:03