我尝试在c ++中进行人脸识别
但是当您尝试这样做时会发生错误 我用过它" try-catch"看到问题,可能会离开这个文本
" OpenCV错误:错误的参数(给定矩阵的形状错误。大小(src)=(1, 30000),size(W)=(10000,1)。)在cv :: subspaceProject中,文件........ \ opencv \ mo dules \ contrib \ src \ lda.cpp,第187行"
代码:
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
#include "opencv/cv.h"
using namespace cv;
using namespace std;
string box_text;
int prediction;
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, CV_LOAD_IMAGE_GRAYSCALE));
labels.push_back(atoi(classlabel.c_str()));
}
}
}
string g_listname_t[]=
{
"Tran Dang Bao",
"Arnold Schwarzenegger"
};
// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;
size_t ic = 0; // ic is index of current element
int ac = 0; // ac is area of current element
size_t ib = 0; // ib is index of biggest element
int ab = 0; // ab is area of biggest element
Mat crop;
CvCapture* capture1;
IplImage* f1;
Mat imgtocrop;
int im_width ;
int im_height;
//*************
int main()
{
string fn_csv = "csv.ext";
vector<Mat> images;
vector<int> labels;
try {
read_csv(fn_csv, images, labels);
} catch (cv::Exception& e) {
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
exit(1);
}
im_width = images[0].cols;
im_height = images[0].rows;
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
model->train(images, labels);
CascadeClassifier face_cascade, eye_cascade,mouth_cascade,nose_cascade;
if(!face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml")) {
printf("Error loading cascade file for face");
return 1;
}
if(!eye_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml")) {
printf("Error loading cascade file for eye");
return 1;
}
if(!mouth_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_mcs_mouth.xml")) {
printf("Error loading cascade file for eye");
return 1;
}
if(!nose_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_mcs_nose.xml")) {
printf("Error loading cascade file for eye");
return 1;
}
VideoCapture capture(0); //-1, 0, 1 device id
if(!capture.isOpened())
{
printf("error to initialize camera");
return 1;
}
Mat cap_img,gray_img;
vector<Rect> faces, eyes ,mouth,nose;
while(1)
{
ic=0;
ib=0;
ab=0;
ac=0;
capture >> cap_img;
waitKey(10);
cvtColor(cap_img, gray_img, CV_BGR2GRAY);
cv::equalizeHist(gray_img,gray_img);
face_cascade.detectMultiScale(gray_img, faces, 1.1, 20, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(300,300));
for(int i=0; i < faces.size();i++)
{
Point pt1(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
Point pt2(faces[i].x,faces[i].y);
Mat faceROI = gray_img(faces[i]);
eye_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30,30));
rectangle(cap_img, pt1, pt2, cvScalar(0,255,0), 2, 8, 0);
//snapshot
capture1 = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture1 ) {
cerr << "Couldn't capture!\n";
return 1;
}
// Get one frame
f1 = cvQueryFrame( capture1 );
if ( !f1 ) {
cerr << "Couldn't capture!\n";
return 1;
}
cvSaveImage("test.jpg" ,f1);
//************snapshot***************
//**********crop face*****************
for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)
{
roi_c.x = faces[ic].x;
roi_c.y = faces[ic].y;
roi_c.width = (faces[ic].width);
roi_c.height = (faces[ic].height);
ac = roi_c.width * roi_c.height; // Get the area of current element (detected face)
roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);
ab = roi_b.width * roi_b.height; // Get the area of biggest element, at beginning it is same as "current" element
if (ac > ab)
{
ib = ic;
roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);
}
imgtocrop=imread("test.jpg",1);
roi_b.width+=25;
roi_b.height+=25;
crop = imgtocrop(roi_b);
// Form a filename
//putText(crop,"mohammad",pt2,0,1.0,cvScalar(200,200,200),1,8,false);
imwrite("mezo.png", crop);
try{
Mat imgr;
cv::resize(crop, imgr, Size(100, 100),1.0,1.0,INTER_CUBIC);
imshow("",imgr);
prediction = model->predict(imgr);
box_text = format( "Prediction = " );
// Get stringname
if ( prediction >= 0 && prediction <=1 )
{
box_text.append( g_listname_t[prediction] );
}
else box_text.append( "Unknown" );
putText(cap_img, box_text, Point(faces[i].x, faces[i].y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}
catch(Exception e){
cout<<e.err;
}
}
//*********crop face **********
}
imshow("Result", cap_img);
waitKey(3);
char c = waitKey(3);
if(c == 27)
break;
}
return 0;
}
此代码出错:
try{
Mat imgr;
cv::resize(crop, imgr, Size(100, 100),1.0,1.0,INTER_CUBIC);
imshow("",imgr);
prediction = model->predict(imgr);
box_text = format( "Prediction = " );
// Get stringname
if ( prediction >= 0 && prediction <=1 )
{
box_text.append( g_listname_t[prediction] );
}
else box_text.append( "Unknown" );
putText(cap_img, box_text, Point(faces[i].x, faces[i].y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}
catch(Exception e){
cout<<e.err;
}
请帮帮我吧