如何在OPENCV 2.4.13中导入训练有素的SVM检测器

How to import a trained SVM detector in OpenCV 2.4.13

本文关键字:训练有素 导入 SVM 检测器 OPENCV      更新时间:2023-10-16

所以我遵循本指南来训练自己的行人猪探测器。https://github.com/dahoc/trainhog/wiki/trainhog-tutorial

,它成功地生成了4个文件。

  • cvhogclassifier.yaml
  • disciptorvector.dat
  • 功能.dat
  • svmlightmodel.dat

有人知道如何将DexcriptorVector.dat文件加载为向量吗?我尝试过但失败了。

    vector<float> detector;
    std::ifstream file;
    file.open("descriptorvector.dat");
    file >> detector;
    file.close();

这是我最终想使用的东西。

    gpu::HOGDescriptor hog(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8),9);
    hog.setSVMDetector(detector);

预先感谢您!

如果您已经训练了SVM,则可以将权重和截距保存在TXT文件中,然后将其加载到数组/矢量中。然后,您将使用如下:

std::vector<float> descriptorsValues;   //A vector to store the computed HoG values
std::vector<cv::Point> locations;
hog.compute(image, descriptorsValues, cv::Size(0, 0), cv::Size(0, 0), locations);
double res = 0;
for (int i = 0; i < svmDimension - 1; i++)
{
    res += w[i] * descriptorsValues.at(i);
}
res = res + w[svmDimension - 1];            
return res;

其中 svmDimension是包含SVM权重的数组/向量,然后是SVM截距,而res是SVM响应

您可以读取文件并将每个值推回float的数组,因此首先声明它:

vector<float> myDescriptorVector;

然后推动每个值:

ifstream infile ("yourFile.dat".c_str());
float number;
while (infile >> number)
    myDescriptorVector.push_back(number);

return myDescriptorVector;

最终使用猪的标准初始化,然后将矢量传递给SVM检测器,如您已经猜到的:

hog.setSVMDetector(myDescriptorVector);