#include<iostream>
#include <opencv2\opencv.hpp>
#include <fstream>
#include <sstream>
#include <opencv2\face\facerec.hpp>
#include <windows.h>
#include <direct.h>
using namespace cv;
using namespace std;
using namespace face;
CascadeClassifier face_cascades;
void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';')
{
std::ifstream file(filename, ifstream::in);//c_str()函数可用可不用,无需返回一个标准C类型的字符串
if (!file)
{
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(CV_StsBadArg, error_message);
}
else
{
cout << "first ok !" << endl;
}
string line, path, classlabel;
int i = 1;
while (getline(file, line))//从文本文件中读取一行字符,未指定限定符默认限定符为“/n”
{
stringstream liness(line);//这里采用stringstream主要作用是做字符串的分割
getline(liness, path, separator);//读入图片文件路径以分好作为限定符
getline(liness, classlabel);//读入图片标签,默认限定符
if (!path.empty() && !classlabel.empty())//如果读取成功,则将图片和对应标签压入对应容器中
{
cout << "second ok!" << i << endl;
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
i++;
}
}
}
int main()
{
String csvPath = "E:\\ORL\\at.txt";
vector<Mat> images;
vector<int> labels;
read_csv(csvPath, images, labels);
//imshow("as", images);
imshow("fsed", labels);
cout << labels.size() << endl;
Ptr<EigenFaceRecognizer> model = EigenFaceRecognizer::create ();
//Ptr < BasicFaceRecognizer > model = EigenFaceRecognizer::create();
model->train(images, labels);
model->save("E:\\ORL\\PCA_Model.xml");
waitKey(0);
system("pause");
return 0;
}
运行后两个错误
第一个错误 LNK1120 1 个无法解析的外部命令 Project3 E:\OpenCV project\Project3\x64\Debug\Project3.exe 1
严重性 代码 说明 项目 文件 行 禁止显示状态
第二错误 LNK2019 无法解析的外部符号 "public: static struct cv::Ptr __cdecl cv::face::EigenFaceRecognizer::create(int,double)" (?create@EigenFaceRecognizer@face@cv@@SA?AU?$Ptr@VEigenFaceRecognizer@face@cv@@@3@HN@Z),该符号在函数 main 中被引用 Project3 E:\OpenCV project\Project3\Project3\源.obj 1