Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android SVM.predict not work properly

Hello world! I am android developer and i am trying to classificate images. I successfully trained svm on pc and now i have xml model. Now i can predict is image bubbled or not. And its work like a charm in pc. But when i am trying to predict image in android, using android opencv sdk using xml model that was trained in pc, it is not work properly. So i don't understand what to do!

Here is my android code below:

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "model.xml"));
    final SVM svm = SVM.load(uri.getPath());
    final HOGDescriptor hog = new HOGDescriptor(new Size(16, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
    MatOfFloat descriptors = new MatOfFloat();
                        Imgproc.resize(answerROI, answerROI, new Size(16, 16));
                        hog.compute(answerROI, descriptors);
                        Mat mat = descriptors.reshape(1, 1);
                        if (svm.predict(mat) > 0.0) {
                            bubbledRect = rects.get(passedQuestionsCount);
                        }

Android SVM.predict not work properly

Hello world! I am android developer and i am trying to classificate images. I successfully trained svm on pc for detect painted and now i have xml model. Now i can predict unpainted bubbles in picture. Opencv version is image bubbled or not. And its 3.4.1. It is work like a charm. Prediction (svm.predict()) work correct, like a charm in pc. But when too. Here is my code below.

 private static String pathNeg = "E:\\Android\\data\\negativo";
private static String pathPos = "E:\\Android\\data\\positivo";
private static String XML = "E:\\Android\\data\\model.xml";

static {
    System.load("E:/Android/opencv/build/java/x64/opencv_java340.dll");
}

public static void main(String[] args) {
    final HOGDescriptor hog = new HOGDescriptor(new Size(16, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
    Mat trainLabel = new Mat();
    Mat trainData = new Mat();

    for (File file : new File(pathPos).listFiles()) {
        if (file.getAbsolutePath().endsWith("jpg")) {
            final Mat img = Imgcodecs.imread(file.getAbsolutePath());
            MatOfFloat descriptors = new MatOfFloat();
            Imgproc.resize(img, img, new Size(16, 16));
            hog.compute(img, descriptors);
            Mat mat = descriptors.reshape(1, 1);
            trainData.push_back(mat);
            trainLabel.push_back(Mat.ones(new Size(1, 1), CvType.CV_32S));
        }
    }

    for (File file : new File(pathNeg).listFiles()) {
        if (file.getAbsolutePath().endsWith("jpg")) {
            Mat img = Imgcodecs.imread(file.getAbsolutePath());
            MatOfFloat descriptors = new MatOfFloat();
            Imgproc.resize(img, img, new Size(16, 16));
            hog.compute(img, descriptors);
            Mat mat = descriptors.reshape(1, 1);
            trainData.push_back(mat);
            trainLabel.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32S));
        }
    }


    System.out.println("trainData rows:" + trainData.rows());
    System.out.println("trainLabel rows:" + trainLabel.rows());

    final SVM svm = SVM.create();
    svm.setKernel(SVM.RBF);
    trainData.convertTo(trainData, CvType.CV_32FC1);
    svm.trainAuto(trainData, Ml.ROW_SAMPLE, trainLabel);
    svm.save(XML);
}

Now i am trying want to predict image in android, using android opencv sdk using use trained xml model that was trained in pc, it is android app, but prediction svm.predict() not work properly. So i don't understand what to do!

Here Not give right prediction. I am using Opencv Android sdk 3.4.0. And here is my android code below:below

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "model.xml"));
    final SVM svm = SVM.load(uri.getPath());
    final HOGDescriptor hog = new HOGDescriptor(new Size(16, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
Mat answerROI = src.submat(rects.get(passedQuestionsCount));
                        MatOfFloat descriptors = new MatOfFloat();
                        Imgproc.resize(answerROI, answerROI, new Size(16, 16));
                        hog.compute(answerROI, descriptors);
                        Mat mat = descriptors.reshape(1, 1);
                        if (svm.predict(mat) > 0.0) {
                            bubbledRect = rects.get(passedQuestionsCount);
                        }

Android SVM.predict not work properly

I trained svm for detect painted and unpainted bubbles in picture. Opencv version is 3.4.1. It is work like a charm. Prediction (svm.predict()) work correct, like a charm too. Here is my code below.

 private static String pathNeg = "E:\\Android\\data\\negativo";
private static String pathPos = "E:\\Android\\data\\positivo";
private static String XML = "E:\\Android\\data\\model.xml";

static {
    System.load("E:/Android/opencv/build/java/x64/opencv_java340.dll");
}

public static void main(String[] args) {
    final HOGDescriptor hog = new HOGDescriptor(new Size(16, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
    Mat trainLabel = new Mat();
    Mat trainData = new Mat();

    for (File file : new File(pathPos).listFiles()) {
        if (file.getAbsolutePath().endsWith("jpg")) {
            final Mat img = Imgcodecs.imread(file.getAbsolutePath());
            MatOfFloat descriptors = new MatOfFloat();
            Imgproc.resize(img, img, new Size(16, 16));
            hog.compute(img, descriptors);
            Mat mat = descriptors.reshape(1, 1);
            trainData.push_back(mat);
            trainLabel.push_back(Mat.ones(new Size(1, 1), CvType.CV_32S));
        }
    }

    for (File file : new File(pathNeg).listFiles()) {
        if (file.getAbsolutePath().endsWith("jpg")) {
            Mat img = Imgcodecs.imread(file.getAbsolutePath());
            MatOfFloat descriptors = new MatOfFloat();
            Imgproc.resize(img, img, new Size(16, 16));
            hog.compute(img, descriptors);
            Mat mat = descriptors.reshape(1, 1);
            trainData.push_back(mat);
            trainLabel.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32S));
        }
    }


    System.out.println("trainData rows:" + trainData.rows());
    System.out.println("trainLabel rows:" + trainLabel.rows());

    final SVM svm = SVM.create();
    svm.setKernel(SVM.RBF);
    trainData.convertTo(trainData, CvType.CV_32FC1);
    svm.trainAuto(trainData, Ml.ROW_SAMPLE, trainLabel);
    svm.save(XML);
}

Now i want to use am using trained xml model in android app, but prediction svm.predict() not work properly. Not give right prediction. I am using Opencv Android sdk 3.4.0. And here is my code below

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "model.xml"));
    final SVM svm = SVM.load(uri.getPath());
    final HOGDescriptor hog = new HOGDescriptor(new Size(16, 16), new Size(8, 8), new Size(4, 4), new Size(4, 4), 9);
Mat answerROI = src.submat(rects.get(passedQuestionsCount));
                        MatOfFloat descriptors = new MatOfFloat();
                        Imgproc.resize(answerROI, answerROI, new Size(16, 16));
                        hog.compute(answerROI, descriptors);
                        Mat mat = descriptors.reshape(1, 1);
                        if (svm.predict(mat) > 0.0) {
                            bubbledRect = rects.get(passedQuestionsCount);
                        }