【问题标题】:Face recognition using SVM使用 SVM 进行人脸识别
【发布时间】:2014-05-04 21:18:02
【问题描述】:

我已经尝试实现 SVM 进行人脸识别,下面是我用于训练和测试的代码。问题是我没有得到准确的结果,有时我总是将预测值设为“0”。

谁能帮我确定正确的 SVM 参数。

培训代码:

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& label, char separator = ';') {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
    string line, path, classlabel;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat testimage = imread(path, 0);
            imshow("testimage", testimage);
            waitKey(10);
            images.push_back(testimage);
            label.push_back(atoi(classlabel.c_str()));
        }
    }
}
int svm()
{
    // Data for visual representation

     string fn_csv = string("/home/resize.csv");
//     These vectors hold the images and corresponding labels.
    vector<Mat> images;
    vector<int> label;
//     Read in the data. This can fail if no valid
//     input filename is given.
    try {
        read_csv(fn_csv, images,label);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
//         nothing more we can do
        exit(1);
    }
    // Set up SVM's parameters
     Mat labelsMat(label.size(), 1, CV_32FC1);
      Mat trainingDataMat(images.size(),70*70, CV_32FC1);

                //iterating through the rows
      int c=0;int d;
               for (int j = 0; j <label.size(); j++) {
                    //iteration through the columns
                   d=0;
                   c++;
                    labelsMat.at<float>(j,0)=float(label[j]);
                  for (int r = 0;r<images[j].rows; r++) {
                //iterating through the rows
               for (int c = 0; c <images[j].cols; c++) {
                 trainingDataMat.at<float>(j,d++) = images[j].at<uchar>(r,c);

                }
                  }
               }

     //imshow("first",images[7]);
      cout<<"labels"<<labelsMat<<std::endl;
   cout<<"labels size"<<c<<std::endl;
   // CvSVMParams params;

SVMParams params = SVMParams();
params.svm_type = SVM::C_SVC;
params.kernel_type = SVM::LINEAR;
params.degree = 3.43; // for poly
params.gamma = 0.00225; // for poly / rbf / sigmoid
params.coef0 = 19.6; // for poly / sigmoid
params.C = 0.5; // for CV_SVM_C_SVC , CV_SVM_EPS_SVR and CV_SVM_NU_SVR
params.nu = 0.0; // for CV_SVM_NU_SVC , CV_SVM_ONE_CLASS , and CV_SVM_NU_SVR
params.p = 0.0; // for CV_SVM_EPS_SVR
params.class_weights = NULL; // for CV_SVM_C_SVC
params.term_crit.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
params.term_crit.max_iter = 1000;
params.term_crit.epsilon = 1e-6;

    // Train the SVM
    CvSVM SVM;
    SVM.train(trainingDataMat, labelsMat, Mat(), Mat(), params);
  // cout<<"train row" <<trainingDataMat.rowRange()<<"cols"<<trainingDataMat.cols<<std::endl;
 SVM.save("/home/test.yml");

    return 0;

}

预测代码:

int svm_test()
{

    // Train the SVM
    CvSVM SVM;

  Mat test=imread("/home/n2.jpg",0);
//  cout<<"image size"<<(float(test.size))<<std::endl;
Mat test_mat(1,test.cols*test.rows,CV_32FC1);
  int ii1=0;
 for (int i1 = 0; i1<test.rows; i1++) {
                //iterating through the rows

                for (int j1 = 0; j1 < test.cols; j1++) {
                    //iteration through the columns

                 test_mat.at<float>(0,ii1++) = test.at<uchar>(i1,j1);
               // labels.at<float>(filenum,1);//=float(filenum);
                }
            }
  // waitKey(0);
SVM.load("/home/smile.yml");

cout<<"preditction value"<<SVM.predict(test_mat)<<std::endl;


    return 0;

}

【问题讨论】:

    标签: opencv image-processing machine-learning computer-vision face-recognition


    【解决方案1】:

    好吧,您的代码看起来很脏,所以我不会重复它们,但我会尝试为您提供一些有关该过程的提示。

    首先,您需要训练集,并确保您拥有比正面更多的负面训练数据以获得良好的结果。另请注意,您将需要安静的大量面孔以获得良好的结果,对于平均或低于平均水平的简单分类器,可能需要 100-200 左右。

    第二次从人脸中提取特征,您可以在其中使用颜色、边缘直方图或像图案这样的二进制等。选择权在您手中。但是很少的卷积会产生更好的结果。

    使用准备好的数据训练 SVM 并进行预测。

    这是来自 MIT 学生 Roy 的链接,他使用 SVM 训练食物分类器。代码简单易懂,你可以跟着他们了解 SVM 分类器的要点。

    Food Classifier

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2021-07-27
      • 2014-04-15
      • 2021-03-12
      • 2014-01-20
      • 2012-11-01
      • 2012-12-10
      • 2014-05-21
      • 2019-04-29
      相关资源
      最近更新 更多