【问题标题】:Store in hard disk a CvSVM object在硬盘中存储一个 CvSVM 对象
【发布时间】: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;

}

【问题讨论】:

    标签: c++ opencv svm


    【解决方案1】:

    也直截了当。这是一个CvStatModel,所以它带有saveload 方法。

    【讨论】:

    • 所以保存和加载它可以像这样 SVM.load(".. .xml");//save
    • 就是这样,是的。或 .yaml IIRC。
    • 当我在 svmpredict 计算后使用负载时,我得到一个 glibc 检测到的 mun_map_chunk() 无效指针:
    • 我假设您的意思是“使用预测 after 加载” - 这听起来像一个 OpenCV 错误。当然,如果您尝试使用未初始化的模型进行预测,那当然是您的错误。
    • 我得到了想要的结果,我的意思是 svmpredict 有效,我用 cout 看到了结果,但之后我收到了消息。
    猜你喜欢
    • 2013-02-26
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2019-05-24
    相关资源
    最近更新 更多