我能够训练系统但是当我尝试预测时,会引发Bad参数异常。
OpenCV错误:cvPreparePredictData中的错误参数(示例不是有效向量),文件........ \ opencv \ modules \ ml \ src \ inner_functions.cpp,第1099行 线程“main”中的异常CvException [org.opencv.core.CvException:cv :: Exception:........ \ opencv \ modules \ ml \ src \ inner_functions.cpp:1099:错误:( - 5)该示例不是函数cvPreparePredictData中的有效向量 ]
这是我的代码:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat classes = new Mat();
Mat trainingData = new Mat();
Mat trainingImages = new Mat();
Mat trainingLabels = new Mat();
CvSVM clasificador;
String path="C:\\java workspace\\ora\\images\\Color_Happy_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat con = Highgui.imread(path+"\\"+file.getName(),Highgui.CV_LOAD_IMAGE_GRAYSCALE);
con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(Mat.ones(new Size(1, 75), CvType.CV_32FC1));
}
System.out.println("divide");
path="C:\\java workspace\\ora\\images\\Color_Sad_jpg";
for (File file : new File(path).listFiles()) {
Mat img=new Mat();
Mat m=new Mat(new Size(640,480),CvType.CV_32FC1);
Mat con = Highgui.imread(file.getAbsolutePath(),Highgui.CV_LOAD_IMAGE_GRAYSCALE);
con.convertTo(img, CvType.CV_32FC1,1.0/255.0);
img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(Mat.zeros(new Size(1, 75), CvType.CV_32FC1));
}
trainingLabels.copyTo(classes);
CvSVMParams params = new CvSVMParams();
params.set_kernel_type(CvSVM.LINEAR);
CvType.typeToString(trainingImages.type());
CvSVM svm=new CvSVM();
clasificador = new CvSVM(trainingImages,classes, new Mat(), new Mat(), params);
clasificador.save("C:\\java workspace\\ora\\images\\svm.xml");
Mat out=new Mat();
clasificador.load("C:\\java workspace\\ora\\images\\svm.xml");
Mat sample=Highgui.imread("C:\\java workspace\\ora\\images\\Color_Sad_jpg\\EMBfemale20-2happy.jpg",Highgui.CV_LOAD_IMAGE_GRAYSCALE);
sample.convertTo(out, CvType.CV_32FC1,1.0/255.0);
out.reshape(1, 75);
System.out.println(clasificador.predict(out));
答案 0 :(得分:1)
你的火车标签仍然是错误的。
你需要一个带有numrows == numimages和1 col的浮动垫。所以,每张图片标签为1个。
所以你的悲伤面孔应该有:
trainingLabels.push_back(-1.0);
你的快乐应该有:
trainingLabels.push_back(1.0);
必须以与训练相同的方式处理预测样本。
sample.convertTo(out, CvType.CV_32FC1,1.0/255.0);
out.reshape(1, 1);