上篇部落格寫了如何利用svm訓練自己的模型,用于識别數字,這片部落格就是加載模型,然後測試模型到底怎樣,正确率高不高。
識别的結果就在這句話中,這句代碼的意思是将檢測的圖檔的标簽傳回回來,結果儲存在response中,可以對response進行操作檢測自己的模型準确率
int response = (int)svm->predict(p);
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <io.h>
using namespace std;
using namespace cv;
void getFiles(string path, vector<string>& files);
int main()
{
int result = ; //
char * filePath = "E:\\SVM_train_data\\positive\\test";
vector<string> files;
getFiles(filePath, files);
int number = files.size();
cout <<"共有測試圖檔 " <<number <<" 張\n"<< endl;
Ptr<ml::SVM>svm = ml::SVM::load("svm.xml");
for (int i = ; i < number; i++)
{
Mat inMat = imread(files[i].c_str());
Mat p = inMat.reshape(, );
p.convertTo(p, CV_32FC1);
int response = (int)svm->predict(p);
cout << "識别的數字為:" << response << endl;
if (response > =)
{
result++;
}
}
cout << result << endl;
getchar();
return ;
}
void getFiles(string path, vector<string>& files)
{
intptr_t hFile = ;
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != && strcmp(fileinfo.name, "..") != )
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == );
_findclose(hFile);
}
}
檢測效果,蠻好的