#pragma
once
#pragma
execution_character_set(
"utf-8"
)
#include
"svm_hog_test_opencv3.h"
#include
using
namespace
cv;
using
namespace
cv::ml;
using
namespace
std;
void
getFiles(
string
path
,
vector
<
string
>&
files
)
{
struct
_finddata_t
fileinfo;
string
str;
long
fileHandle =
_findfirst
(str.assign(
path
).append(
"/*"
).c_str(), &fileinfo);
if
(fileHandle == -1){
return
;
}
while
(!
_findnext
(fileHandle, &fileinfo)){
if
((fileinfo.attrib &
_A_SUBDIR
))
{
if
(strcmp(fileinfo.name,
"."
) != 0 && strcmp(fileinfo.name,
".."
) != 0)
getFiles(str.assign(
path
).append(
"/"
).append(fileinfo.name),
files
);
}
else
{
files
.push_back(str.assign(
path
).append(
"/"
).append(fileinfo.name));
}
}
_findclose(fileHandle);
}
int
main(
int
argc
,
char
**
argv
)
{
//cv::Mat input = cv::imread(argv[1]);
//批量测试图片
//加载分类器
char
* filePath_imgs =
argv
[1];
//分类器路径
vector
string
> files_img;
getFiles(filePath_imgs, files_img);
int
size_img = files_img.size();
//int flag_classes = 10;//测试集标签(属于哪一类)
ofstream
in;
in.open(
"corrct_~.txt"
,
ios
::trunc);
//储存测试结果
if
(size_img > 0){
int
num_corrct = 0;
int
sum_images = 0 ;
//测试图片总数
for
(
int
j = 0; j < size_img;j++){
cout << files_img[j].c_str() << endl;
cv::
Mat
input = cv::imread(files_img[j].c_str());
if
(input.empty()){
return
0;
}
else
{
sum_images++;
//加载svm分类器
char
* filePath =
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls"
;
//分类器路径
vector
string
> files;
getFiles(filePath, files);
int
size = files.size();
cv::
Mat
res, gray_img;
cv::resize(input, res, cv::
Size
(64, 64));
//cv::cvtColor(res, gray_img, cv::COLOR_BGR2GRAY);
string
num_class;
for
(
int
i = 0; i < size; i++)
{
cout <<
"get image: "
<< files[i].c_str() << endl;
cv::
Ptr
SVM
> svm =
SVM
::load<
SVM
>(files[i].c_str());
cv::
HOGDescriptor
d = cv::
HOGDescriptor
(cv::
Size
(64, 64), cv::
Size
(16, 16), cv::
Size
(16, 16), cv::
Size
(8, 8), 9);
std::
vector
<
float
> descriptors;
d.compute(res, descriptors);
float
result_lable = svm->predict(descriptors);
//float result_probability = svm->p
//printf("the character belongs to:%f_classes\n", result_lable);
if
(result_lable > -1){
num_class = files[i].c_str();
i = size;
}
}
//printf("the character belongs to:%s_classes\n", num_class);
//显示检测结果
cv::
Mat
img_show;
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svmdot_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm%_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svmx_20180206.xml"
){
/* img_show = cv::imread("E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/logo_charactor/12.jpg");
cv::imshow("result", img_show);
cv::waitKey(0);*/
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm+_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm-_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm(_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm)_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm=_20180206.xml"
){
}
else
if
(num_class ==
"E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/20180206_xmls/svm~_20180206.xml"
){
//svm~_20180206
/* img_show = cv::imread("E:/opencv_studydocumence/opencv_study/HOG+SVM/code/Release/logo_charactor/18.jpg");
cv::imshow("result", img_show);
cv::waitKey(0);*/
num_corrct++;
}
}
}
float
ratio = (
float
)num_corrct / (
float
)sum_images;
//计算正确率
in << ratio << endl;
}
else
{
std::cout <<
"can't load images!\n"
;
//printf("can't load images!\n");
}
//cv::Mat res, gray_img;
//cv::resize(input, res, cv::Size(64, 64));
////cv::cvtColor(res, gray_img, cv::COLOR_BGR2GRAY);
//string num_class;
//for (int i = 0; i < size; i++)
//{
// cout << "get image: " << files[i].c_str() << endl;
std::system(
"pause"
);
return
0;
}