OpenCV 2.4.10 svm无法加载模型

时间:2016-05-27 04:33:10

标签: java opencv android-studio svm

我想以正面和负面的方式训练图像数据。我可以训练它并将模型保存在文件.xml中但是当我想加载我的模型时它无法加载。这是我的代码

package com.example.toonz.video;

/**
 * Created by toonz on 5/24/2016.
 */


import android.os.Environment;
import android.util.Log;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.ml.CvSVM;
import org.opencv.ml.CvSVMParams;


import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 * Created by toonz on 5/19/2016.
 * code สำหรับทำการ train svm classification
 * reference: http://stackoverflow.com/questions/31454644/java-opencv-svm-training-error
 */
public class linearsvmt{
    Mat classes = new Mat();
    Mat trainingData = new Mat();
    Mat trainingImages = new Mat();
    Mat trainingLabels = new Mat();

    CvSVM clasificador;
    CvSVM svmd;

    File filep = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Positive");
    File filen = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Negative");
    File XML = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator+"test.xml");
    File filet = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "data");
    File[] listp = filep.listFiles();
    File[] listn = filen.listFiles();
    File[] listt = filet.listFiles();
    ArrayList data = new ArrayList();

    public void traind() {
        trainPositive();
        trainNegative();
        train();
        test();
    }
    public void  test() { ///method ที่เอาภาพจาก folder ที่ตัดเฟรมมาทำการคัดเลือกภาพที่ใช้ได้
        int count = 0;
        for (File file : listt) {
            System.out.println( file.toString() );
            //Mat in = getMat( /*new File( file.toString() )*/file.toString()  /*, Highgui.CV_LOAD_IMAGE_GRAYSCALE*/ );
            Mat in = Highgui.imread( file.toString(), Highgui.CV_LOAD_IMAGE_GRAYSCALE );
            Log.d("Test", "Path:" + XML.toString());
            clasificador.load( XML.toString());
            Mat out = new Mat();
            in.convertTo( out, CvType.CV_32FC1 );
            out = out.reshape( 1, 1 );
            Float result = clasificador.predict( out );

        }
    }

    protected  void train() { //เอาภาพจาก positive และ negative มาทำการ train
        trainingImages.copyTo(trainingData);
        trainingData.convertTo( trainingData, CvType.CV_32FC1 );
        trainingLabels.copyTo( classes );
        CvSVMParams params = new CvSVMParams();
        params.set_kernel_type(CvSVM.LINEAR);
        clasificador = new CvSVM(trainingData, classes, new Mat(), new Mat(), params );
        clasificador.save( XML.toString() );
    }
    protected void trainPositive() {
        for (File file : listp) {
            //System.out.println(file.toString());
            Mat imgp = getMat(file.getAbsolutePath());
            trainingImages.push_back(imgp.reshape(1, 1));
            trainingLabels.push_back(Mat.ones(new Size(1, 1), CvType.CV_32FC1));
        }
    }
    protected void trainNegative() {
        for (File file : listn) {
            Mat imgn = getMat(file.toString());
            trainingImages.push_back(imgn.reshape(1, 1));
            trainingLabels.push_back(Mat.zeros(new Size(1, 1), CvType.CV_32FC1));
        }
    }
    protected static Mat getMat( String path ) {
        Mat img = new Mat();
        Mat con = Highgui.imread( path, Highgui.CV_LOAD_IMAGE_GRAYSCALE );
        con.convertTo( img, CvType.CV_32FC1, 1.0 / 255.0 );
        return img;
    }
}

其他方法工作正常,但是当我运行方法test()时,我调试每一行,它在clasificador.load(XML.toString())中有错误; 我不明白为什么我无法加载我的模型。有什么不对吗?请帮帮我。

0 个答案:

没有答案