I am trying to recognize facial emotion using SVM, however every time I run the program I get an error stating "assertion failed (samples.cols== var_count && samples.type == CV_32F) in cv::ml::SVMImpl::predict in file....... line 1919"
For now I am trying to predict only 2 emotion- Happy & Sad. The code is given below:- int main(int argc, const char* argv) {
cout << "OpenCV Training SVM Emotion Recognition\n";
cout << "\n";
const char* path_Happy;
const char* path_Sad;
int numHappy;
int numSad;
int imageWidth=144;
int imageHeight=33;
argc=5;
argv[1]= "10";
argv[2]= "10";
argv[3]= "C:/Users/akash garg/Documents/Visual Studio 2012/Projects/ExpressionSVM/ExpressionSVM/Happy";
argv[4]= "C:/Users/akash garg/Documents/Visual Studio 2012/Projects/ExpressionSVM/ExpressionSVM/Sad";
//Check if user specify image to process
if(argc >= 5 )
{
numHappy= atoi(argv[1]);
numSad= atoi(argv[2]);
path_Happy= argv[3];
path_Sad= argv[4];
}else{
return 0;
}
Mat classes;
Mat trainingData;
Mat trainingImages;
vector<int> trainingLabels;
for(int i=0; i< numHappy; i++)
{
stringstream ss(stringstream::in | stringstream::out);
ss << path_Happy <<"/" <<i+1 << ".png";
cout<<ss.str()<<endl;
//Mat img=imread(ss.str(), 0);
Mat m = imread(ss.str(), 1);
Mat img;
cvtColor(m,img,CV_BGR2GRAY);
img= img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(1);
}
for(int i=0; i< numSad; i++)
{
stringstream ss(stringstream::in | stringstream::out);
ss << path_Sad <<"/" << i+1 << ".png";
Mat img=imread(ss.str(), 0);
img= img.reshape(1, 1);
trainingImages.push_back(img);
trainingLabels.push_back(0);
}
Mat(trainingImages).copyTo(trainingData);
trainingData.convertTo(trainingData, CV_32FC1);
Mat(trainingLabels).copyTo(classes);
FileStorage fs("SVM.xml", FileStorage::WRITE);
fs << "TrainingData" << trainingData;
fs << "classes" << classes;
fs.release();
Ptr<ml::SVM> svm= ml::SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::CHI2);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(trainingData, ml::ROW_SAMPLE, classes);
svm->save("Trained.xml");
cout<<"\n SVM classifier is trained and saved.";
//svm= cv::Algorithm::load<ml::SVM>("Trained.xml");
//svm->load("Trained.xml");
//cv::Ptr<cv::ml::SVM> svm2= ml::SVM::create();
//svm2 = cv::ml::SVM::load<cv::ml::SVM>("Trained.xml");
svm= cv::ml::SVM::load<cv::ml::SVM>("Trained.xml");
Mat TestData= imread("C:/Users/akash garg/Documents/Visual Studio 2012/Projects/ExpressionSVM/ExpressionSVM/Test.png",0);
imshow("Test_Image", TestData);
Mat TestDataMat(1, 1, CV_32FC1, &TestData);
//TestData.convertTo(TestData, CV_32FC1);
//TestData= TestData.reshape(1,1);
//float response = svm->predict(TestData);
float response = svm->predict(TestDataMat);
if(response==1)
cout<<"\n Happy Image Detected";
else if(response==0)
cout<<"\n Sad Image Detected";
else
cout<<"\n No emotion detected";
waitKey(0);
return 0;
}*
I have made the test image of same type as of training dataset but still I am getting the same error. Please help! Thanks in advance.