opencv3.2 java svm.save(path) error

asked 2017-06-28 06:47:13 -0600

  public static final String SVM_MODEL_FILE_PATH = "C:\\Users\\tikkat3\\IdeaProjects\\VehicleLicenseOCR\\resources\\b_process\\svm/model";

void train(){
    //init SVM
    SVM svm = SVM.create();
    svm.setType(SVM.C_SVC);
    svm.setKernel(SVM.RBF);
    svm.setDegree(0.1);
    // 1.4 bug fix: old 1.4 ver gamma is 1
    svm.setGamma(0.1);
    svm.setCoef0(0.1);
    svm.setC(1);
    svm.setNu(0.1);
    svm.setP(0.1);
    svm.setTermCriteria(new TermCriteria(TermCriteria.EPS, 100, 1));

    System.out.println("load data...");
    //load train data
    TrainData trainData = loadTrainData();

    //train
    long star = System.currentTimeMillis();
    System.out.println("start train...");
    svm.train(trainData.getSamples(),Ml.ROW_SAMPLE,trainData.getResponses());//correct
    //svm.train(trainData); wrong
    System.out.println("end train...total time : " +(System.currentTimeMillis()-star) + "ms");

    //save svm.xml
    svm.save(SVM_MODEL_FILE_PATH);
    System.out.println("save the train model...");

}

svm.save(SVM_MODEL_FILE_PATH); error:

Exception in thread "main" java.lang.Exception: unknown exception at org.opencv.core.Algorithm.save_0(Native Method) at org.opencv.core.Algorithm.save(Algorithm.java:53) at svm.SvmTrain.train(SvmTrain.java:62) at svm.SvmTrain.main(SvmTrain.java:31)

edit retag flag offensive close merge delete

Comments

1

@berak I fixed the svm_model_save_path and confirmed that svm.train() returned true

xiaoxiu gravatar imagexiaoxiu ( 2017-06-28 07:25:46 -0600 )edit

so, solved ?

berak gravatar imageberak ( 2017-06-28 08:10:58 -0600 )edit

@berak No, not yet resolved. The current situation is

    1. Svm.train () returns true
    1. svm.save (svm_model_pth) failed ! the message isException in thread "main" java.lang.Exception: unknown exception at org.opencv.core.Algorithm.save_0(Native Method) at org.opencv.core.Algorithm.save(Algorithm.java:53) at svm.SvmTrain.train(SvmTrain.java:63) at svm.SvmTrain.main(SvmTrain.java:31)
    1. If I skip the error svm.save, run directly test (), being given! The error message is OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in cv::ml::SVMImpl::predict, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\src\svm.cpp, line 1930 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master
xiaoxiu gravatar imagexiaoxiu ( 2017-06-28 20:17:16 -0600 )edit

for the prediction, you have to treat your data in the very same way as for the training, that is:

  • make sure, it has the exact same size
  • convert to float
  • reshape(1,1) it to a single row.
berak gravatar imageberak ( 2017-06-29 01:38:37 -0600 )edit