Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android svm implementation error

I tried to implement svm in my android application but when i run my application i get errors.

        // Creating Training Data
            Mat trainData = new Mat();
        Mat train_labels = new Mat();


            for (int i = 0; i <549; i++) {
            String path = Environment.getExternalStorageDirectory().toString()
                    + "/Pictures/train/" + i + ".png";

            Mat img = Imgcodecs.imread(path);

            Log.i(TAG,"error"+i+img.empty());

            img.convertTo(img, CvType.CV_32FC1); // Convert to float
            Size dsize = new Size(25, 25);
            Imgproc.resize(img, img, dsize);

            img.convertTo(img, CvType.CV_32FC1);
            Mat imgResized = img.reshape(1, 1); // make continuous

            trainData.push_back(imgResized);
            // add 1 item
            train_labels
                    .push_back(new Mat(1, 1, CvType.CV_32FC1, new Scalar(i)));

        }

        Mat response = new Mat();
        Mat tmp;
        tmp = train_labels.reshape(1, 1); // make continuous
        tmp.convertTo(response, CvType.CV_32FC1); // Convert to float
         SVM svm = SVM.create();
            TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
            svm.setKernel(SVM.LINEAR);
            svm.setType(SVM.C_SVC);
            svm.setGamma(0.5);
            svm.setNu(0.5);
            svm.setC(1);
            svm.setTermCriteria(criteria);
            svm.train(trainData, Ml.ROW_SAMPLE,train_labels);

        // For Storing training data


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

And this is the error:

05-28 11:05:01.921: E/cv::error()(2252): OpenCV Error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in virtual bool cv::ml::SVMImpl::train(const cv::Ptr<cv::ml::traindata>&, int), file /home/maksim/workspace/android-pack/opencv/modules/ml/src/svm.cpp, line 1610 05-28 11:05:01.931: E/org.opencv.ml(2252): ml::train_10() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/ml/src/svm.cpp:1610: error: (-5) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function virtual bool cv::ml::SVMImpl::train(const cv::Ptr<cv::ml::traindata>&, int)