OpenCV人脸识别
OpenCV face recognition
我正在尝试使用 opencv 通过将静态图像与图像数据库进行比较并将预测打印到 txt 文件来对静态图像执行人脸识别,这是我整理的代码,但我收到了很多错误。任何关于我出错的地方的帮助将不胜感激。
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
static Mat norm_0_255(InputArray _src) {
Mat src = _src.getMat();
// Create and return normalized image:
Mat dst;
switch(src.channels()) {
case 1:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
break;
case 3:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
break;
default:
src.copyTo(dst);
break;
}
return dst;
}
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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()) {
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
}
//path to csv
string fn_csv = ("./at.csv");
//patch to haar cascade
string fn_haar = ("./haarcascade_frontalface_alt.xml");
// These vectors hold the images and corresponding labels.
vector<Mat> images;
vector<int> labels;
read_csv(fn_csv, images, labels);
int im_width = images[0].cols;
int im_height = images[0].rows;
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
//cv::createEigenFaceRecognizer(10);
//create the model
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
//load cascade
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
Mat Image
for(;;) {
Mat Image* img = cvLoadImage ("./Lena.png")
Mat gray;
cvtColor(original, gray, CV_BGR2GRAY);
// Find the face
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(int i = 0; i < faces.size(); i++) {
// Process face
Rect face_i = faces[i];
// Crop the face from the image.
Mat face = gray(face_i);
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// prediction
int prediction = model->predict(face_resized);
string text = format("Prediction = %d", prediction);
}
}
int send () {
std::string s= text;
std::ofstream os("prediction.txt");
if (!os) { std::cerr<<"Error writing to prediction"<<std::endl; } else {
os << s;
}
return 0;
}
你的代码完整了吗?有很多粗心的错误,只要阅读错误信息就可以轻松纠正。
- 无main()函数
- 第 83 和 85 行缺少分号
同一行 85:
Mat Image* img = cvLoadImage ("./Lena.png");
应该是
Image = imread("./Lena.png");
cvLoadImage() 是 C(返回 IplImage*
),而不是 C++。 (虽然我不明白你为什么要在无限循环中一遍又一遍地加载同一张图片。)
original
第 88 行未定义
第 90-94 行两次是相同的东西
undefined text
send() 函数中的变量第 113 行,可能是您应该作为参数传递的第 107 行。
我正在尝试使用 opencv 通过将静态图像与图像数据库进行比较并将预测打印到 txt 文件来对静态图像执行人脸识别,这是我整理的代码,但我收到了很多错误。任何关于我出错的地方的帮助将不胜感激。
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
using namespace std;
static Mat norm_0_255(InputArray _src) {
Mat src = _src.getMat();
// Create and return normalized image:
Mat dst;
switch(src.channels()) {
case 1:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
break;
case 3:
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3);
break;
default:
src.copyTo(dst);
break;
}
return dst;
}
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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()) {
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
}
//path to csv
string fn_csv = ("./at.csv");
//patch to haar cascade
string fn_haar = ("./haarcascade_frontalface_alt.xml");
// These vectors hold the images and corresponding labels.
vector<Mat> images;
vector<int> labels;
read_csv(fn_csv, images, labels);
int im_width = images[0].cols;
int im_height = images[0].rows;
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
//cv::createEigenFaceRecognizer(10);
//create the model
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
//load cascade
CascadeClassifier haar_cascade;
haar_cascade.load(fn_haar);
Mat Image
for(;;) {
Mat Image* img = cvLoadImage ("./Lena.png")
Mat gray;
cvtColor(original, gray, CV_BGR2GRAY);
// Find the face
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
vector< Rect_<int> > faces;
haar_cascade.detectMultiScale(gray, faces);
for(int i = 0; i < faces.size(); i++) {
// Process face
Rect face_i = faces[i];
// Crop the face from the image.
Mat face = gray(face_i);
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// prediction
int prediction = model->predict(face_resized);
string text = format("Prediction = %d", prediction);
}
}
int send () {
std::string s= text;
std::ofstream os("prediction.txt");
if (!os) { std::cerr<<"Error writing to prediction"<<std::endl; } else {
os << s;
}
return 0;
}
你的代码完整了吗?有很多粗心的错误,只要阅读错误信息就可以轻松纠正。
- 无main()函数
- 第 83 和 85 行缺少分号
同一行 85:
Mat Image* img = cvLoadImage ("./Lena.png");
应该是
Image = imread("./Lena.png");
cvLoadImage() 是 C(返回
IplImage*
),而不是 C++。 (虽然我不明白你为什么要在无限循环中一遍又一遍地加载同一张图片。)original
第 88 行未定义第 90-94 行两次是相同的东西
undefined
text
send() 函数中的变量第 113 行,可能是您应该作为参数传递的第 107 行。