Reputation: 15
I'd like to implement Bayes
and SVM
classifiers in OpenCV 3.3
. I've written following code for SVM
:
int main()
{
// Load data
FileStorage fs("newStorageFile.yml", FileStorage::READ);
// Read data
Mat test_data, test_labels, enrol_data, enrol_labels;
Mat train_labels = Mat::zeros(650, 1, CV_32S);
Mat train_data = Mat::zeros(650, 600, CV_32S);
fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;
fs["test_data"] >> test_data;
fs["test_labels"] >> test_labels;
fs["enrol_data"] >> enrol_data;
fs["enrol_labels"] >> enrol_labels;
Ptr<ml::SVM> SVM_Model = ml::SVM::create();
SVM_Model->setType(ml::SVM::C_SVC);
SVM_Model->setKernel(ml::SVM::RBF);
Ptr<ml::TrainData> trainingData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels);
SVM_Model->trainAuto(trainingData);
return 0;
}
But I've got following exception error on SVM_Model->trainAuto(trainingData)
.
Unhandled exception at 0x755B5608 in SVMimplemantation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00D8DF58.
And also about Bayes
classifier, I've written following code:
int main()
{
// Load data
FileStorage fs("newStorageFile.yml", FileStorage::READ);
// Read data
Mat test_data, test_labels, enrol_data, enrol_labels;
Mat train_labels = Mat::zeros(650, 1, CV_32F);
Mat train_data = Mat::zeros(650, 600, CV_32F);
fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;
fs["test_data"] >> test_data;
fs["test_labels"] >> test_labels;
fs["enrol_data"] >> enrol_data;
fs["enrol_labels"] >> enrol_labels;
Ptr<ml::NormalBayesClassifier> bayes = ml::NormalBayesClassifier::create();
Ptr<ml::TrainData> trainData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels);
bayes->train(trainData);
Mat output, outputProb;
bayes->predictProb(test_data, output, outputProb);
return 0;
}
About this case, I've got following Exception on bayes->train(trainData)
too.
Unhandled exception at 0x755B5608 in BayesImplementation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00A5D79C.
To be able to compile the project, Ive uploaded my dataset here. What's the problem and how to fix it?
Upvotes: 1
Views: 56
Reputation: 558
Problem is the type of train_label
. Try with following:
Mat train_labels32S = Mat::zeros(train_labels.rows, 1, CV_32S);
for (int i = 0; i < train_labels.rows; i++)
train_labels32S.at<int>(i, 0) = train_labels.at<int>(i, 0);
//some code
Ptr<ml::TrainData> trainingData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels32S);
Upvotes: 1