【发布时间】:2014-01-30 18:51:49
【问题描述】:
我已经为 opencv 中的人脸创建了一个 SVM 检测器。我想将训练和预测的过程分开。因此,我想找到一种方法将 CvSVM SVM 对象存储在我的硬盘中,以便随时访问它而无需再次训练新模型。这样,我可以通过从硬盘加载 SVM 对象来使用 predict 方法。这在 Matlab 中非常简单,但我怎样才能在 opencv c++ 中成功呢?
使用加载和保存函数来加载和保存 SVM 模型会导致 munmap_chunk()::invalid pointer message(glibc detected) 消息。
CvSVM SVM = detect.SVMtrain("dbpath/");
SVM.save("svmTrain.xml", 0);
cout <<"Detection system in progress progress...\n";
string pathFile = databasePath+ imageFilename;
vector<float> confidence;
detections = detect.detection(pathFile);
CvSVM SVM;
SVM.load("svmTrain.xml",0);
confidence = detect.SVMpredict(detections.at(0), SVM);
//cout << "confidence is: " << confidence.at(0) << endl;
当我运行上面的代码时,我收到了上面的消息。问题在于 svmpredict 函数。和SVMpredict函数:
vector<float> Detection::SVMpredict(Mat image, CvSVM SVM){
vector<float> res;
//Mat image = imread(fileName,0); //Read as a gray image
//cvtColor(sampleMat, sampleMat, CV_BGR2GRAY);
image = eigenfacesExtraction(image);
image.convertTo(image, CV_32FC1); // <-- Convert to CV_32F for matrix mult
float response = SVM.predict(image); res.push_back(response);
float val = SVM.predict(image,true); res.push_back(val);
cout << "svm result: "<< response <<" val: "<< val << endl;
return res;
}
【问题讨论】: